ruby - Why do I get false in both cases? -
i trying check if 2 integers in array in sum equal n. however, false first case, though think should true.
def sum_to_n?(array, n) in array s = n - return true if array.include? s return false if != n - end end puts sum_to_n?([1,2,3,4,5], 9) puts sum_to_n?([1,2,3,4,5], 12)
you can in not many lines :-)
ary = [1,2,3,4,5] n=9 ary.combination(2).detect { |a, b| + b == n }
Comments
Post a Comment