DEV Community

Discussion on: Daily Challenge #35 - Find the Outlier

Collapse
 
tanguyandreani profile image
Tanguy Andreani • Edited

Bad solution! See comments below!

Ruby solution, I tried to do something different.

def findOutlier numbers
  sum = numbers.sum
  if sum.odd?
    numbers.find(&:odd?)
  else
    numbers.find(&:even?)
  end
end

puts findOutlier([2, 4, 0, 100, 4, 11, 2602, 36])
puts findOutlier([160, 3, 1719, 19, 11, 13, -21])
puts findOutlier([4, 8, 15, 16, 24, 42])
puts findOutlier([16, 6, 40, 66, 68, 28])
puts findOutlier([5, 9, 4, 155, 7])

When the list has no exception, the first element is returned; except when numbers looks like [3,3] (bug that you can fix by adding .uniq when computing the sum.)

Collapse
 
easyaspython profile image
Dane Hillard

I believe this would fail if the outlier is even and the rest are an odd number of odd numbers. The examples all have an even number of odd numbers. Can you check that?

Collapse
 
tanguyandreani profile image
Tanguy Andreani

It seems that I can’t make it right and as simple as it was. So I’ll stick with this solution:

def findOutlier numbers
  if numbers.first(3).count(&:even?) > 1
    numbers.find(&:odd?)
  else
    numbers.find(&:even?)
  end
end

puts findOutlier([2, 4, 0, 100, 4, 11, 2602, 36])
puts findOutlier([160, 3, 1719, 19, 11, 13, -21, 33])
puts findOutlier([160, 3, 1719, 19, 11, 13, -21])
puts findOutlier([4, 8, 15, 16, 24, 42])
puts findOutlier([16, 6, 40, 66, 68, 28])
puts findOutlier([3,3])