When n integers are given as an array arr, I want to create a method that determines if there are two numbers that add up to 0 and answers them.
The output is true if it adds up to 0, otherwise it is false.Error message
irb (main): 021: 0>has_duplicates? [9, 5, 2, -6, 2, -8, 1, 10, -5, -2]
=>[9, 5, 2, -6, 2, -8, 1, 10, -5, -2]
A lot of true is output, and I don't know what to do. Thanks for your cooperation as it may be wrong overall.
The actual output is as follows.
irb>has_duplicates? [9, 5, 2, -6, 2, -8, 1, 10, -5, -2]
def has_duplicates? (arr) arr.each_with_index do | i, j | if i = -j puts 'true' else puts 'false' end end end
As a result of trying something, I have reached this point.Supplemental information (FW/tool version etc.)
Answer # 1
if i = -jis an assignment of
i, so it is always true.
If there is no distinction between
==, writing a program is difficult.
The policy isThe method
・ True if there is even one such pair
・ If you don't have such a pair after looking for everything, it's false
I have to think about it.
each_with_indexis misunderstood and used.
Find and use the methods that extract all the combinations of two from the Array methods.
Also, methods whose names end with
?usually have the meaning of returning
false. It is not displayed inside the method.
Answer # 2
each_with_indexis used incorrectly,
=is used incorrectly,
putsis strange, even if they are correct, the logic is probably wrong (no matter how you misunderstand the meaning of
We recommend that you start with a simpler problem.
Answer # 3
Two people point out that studying a program and studying ruby are necessary.
Studying a program is difficult if there is no specific target.
Is that the puzzle?
Checking module Enumerable is an additional advice to otn's answer
The policy is
"Check all? Or any? With brute force" is the easiest to understand, but the false result requires n! Iterations, so consult with the actual number of data. In the example, it is only 3628800 times because it is 10, but when it becomes 20, it will be a considerable number of times. It will not reach the pass, but Kyo will be exceeded.
It is only n times that you can compare it in order of size by dividing it into two groups of positive and negative, but the program is troublesome
First of all to try to brute force, be interesting to try to see what number it should look it Once more
Answer # 4
Note that other respondents are giving good advice, so this answer first gives a code example.
def has_inverted_pair? (arr) arr.each_with_index do | e, i | return true if arr [i + 1 ... arr.length] .include? (-e) end false end
p has_inverted_pair? [9, 5, 2, -6, 2, -8, 1, 10, -5, -2] # =>true p has_inverted_pair? [9, 5, 2, -6, 2, -8, 1, 10, -50, -20] # =>false p has_inverted_pair? [5, 0, 1, 2, 0, -3, -4] # =>true (contains two 0s)
Repl.it for operation check:https://repl.it/@jun68ykt/Q224458
Next, what I'm interested in the code listed in the question is the method name
has_duplicates?. In this case, the argument array seems to be a judgment of "Does it contain elements with the same value?" In the above answer, "The element with the plus/minus inverted"
has_inverted_pair?to make it easier to understand if there is a pair. I don't know if this is the best name, but here are some tips on the importance of sticking to names of methods, variables, classes, etc.
Reference: 97 things programmers should know-name important (Matz)
Thank you for your reference.
- ruby on rails 5 - rails: create an administrator screen using activeadmin colors of other pages change
- ruby - i can't create a custom account with rails stripe
- ruby on rails - about rails create action
- ruby - [rails] i want to create an image link using the url of the image output using active storage
- ruby - rails even if i create a new model (user), it is not displayed
- ruby on rails - i want to use yield in a method chain
- ruby - i want to transition from create to show with rails
- ruby - no method error rails
- ruby - i can't "rails db: create rails_env = production" on aws
- ruby on rails - i can't create a model with rails
- ruby on rails - undefined method `merge'for
- ruby on rails - [rails] i want to use each statement in a helper method
- ruby on rails - when calling the create action, the name attribute changes depending on whether it is indexhtmlerb or newhtmlerb
- ruby on rails - i get an error with rails db: create
- ruby on rails: undefined method ʻuser'for nil: about nomethoderror of nilclass
- the ruby on rails each method does not repeat well
- ruby on rails 6 - undefined method `id'for: article: symbol resolution in rails
- ruby on rails - [rails] i want to make it a helper method
- ruby - undefind method chart kick in rails production environment
- ruby on rails 5 - undefined method `tomorrow'for 00: i would like to know how to solve bigdecimal
- creating a two-dimensional array using malloc in c language
- i want to display an associative array as a table in php
- ruby on rails uninitialized constant error
- in python, i want to extract an array from a number reference
- ruby on rails - i want to associate a column based on the id of the array fetched by group method
- php - i want to create an array by extracting a multidimensional array with regular expressions
- ruby - how to put and get the hash value in checkbox
- ruby - how to use the delete_if method
- python - extracting required elements from the dictionary by conditional branch differs from the expected result