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]

Applicable source code
def has_duplicates? (arr)
  arr.each_with_index do | i, j |
    if i = -j
      puts 'true'
      puts 'false'

As a result of trying something, I have reached this point.

Supplemental information (FW/tool version etc.)


  • Answer # 1

    First,if i = -jis an assignment of-jtoi, so it is always true.
    If there is no distinction between=and==, writing a program is difficult.

    The policy is
    ・ 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.

    The method

    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 returningtrueorfalse. 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 ofeach_with_index).

    Wrong overall.
    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)
    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 namehas_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.