1

The two Ruby versions are: 1.8.7 (which the school uses) vs. 1.9.3 (current version, that I have on my system).

Just curious on what is different in 1.9.3 that makes the following not work properly. The function outputs true if all the elements in the list is the same, false if it is not all the same.

e.g.
[1,1,1] => true
[1,2,1] => false

In Ruby 1.9.4,

odd_one_out_in_list?([1,1,1])
=> false  #which is should output 'true'

while in Ruby 1.8.7,

odd_one_out_in_list?([1,1,1])
=> true  #which is good

The logic in the following looks ok to me. What is different in 1.9.4? I have checked out: What is the difference between Ruby 1.8 and Ruby 1.9 but I am unable to find the answer there.

Here's my function:

def odd_one_out_in_list?(list)
  sorted_list = list.sort
  if sorted_list[0] == sorted_list[list.length-1]
    return true
  else
    return false
  end
end
Community
  • 1
  • 1
Luigi
  • 45
  • 1
  • 8

1 Answers1

0

Try def odd_one_out_in_list?(list); list.uniq.size == 1; end. Is simpler and probably faster

Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265