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 - about rails create action
- ruby on rails - ndefined method `name'for nil: nilclass error is output
- ruby - not saved with rails new save method
- ruby on rails: undefined method ʻuser'for nil: about nomethoderror of nilclass
- ruby on rails - undefined method `if'for ~
- ruby on rails 6 - undefined method `id'for: article: symbol resolution in rails
- ruby - no method error rails
- ruby on rails - undefined method `merge'for
- ruby on rails - i'm trying to create a container in docker but it doesn't work i want to create a container
- ruby - i can't create a custom account with rails stripe
- ruby on rails - not saved by create action
- ruby on rails - unable to create controller
- ruby on rails 5 - undefined method `tomorrow'for 00: i would like to know how to solve bigdecimal
- ruby on rails - create a chat feature with action cable on ec2 on rails aws
- ruby on rails - i want to create an image posting function with react (typescript), formik, rails api, activestorage
- ruby on rails - i get an error with rails db: create
- ruby - [rails] i want to create an image link using the url of the image output using active storage
- ruby - [rails] i want to create a link including the data attribute with the link_to helper
- ruby on rails 5 - rails: i want to create a timetable app
- ruby on rails - i don't know where to check the method
- 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