Skip to content

Instantly share code, notes, and snippets.

@barbchoy
Created October 2, 2017 20:42
Show Gist options
  • Save barbchoy/3efba8d4b7cbc71831ae1bfb6b2920d6 to your computer and use it in GitHub Desktop.
Save barbchoy/3efba8d4b7cbc71831ae1bfb6b2920d6 to your computer and use it in GitHub Desktop.
null created by barbchoy - https://repl.it/Br7X/2665
# Write a method that takes an array of numbers in. Your method should
# return the third greatest number in the array. You may assume that
# the array has at least three numbers in it.
#
# Difficulty: medium.
def third_greatest(nums)
sorted_nums=nums.sort
return sorted_nums[nums.length-3]
end
# These are tests to check that your code is working. After writing
# your solution, they should all print true.
puts("\nTests for #third_greatest")
puts("===============================================")
puts(
'third_greatest([5, 3, 7]) == 3: ' +
(third_greatest([5, 3, 7]) == 3).to_s
)
puts(
'third_greatest([5, 3, 7, 4]) == 4: ' +
(third_greatest([5, 3, 7, 4]) == 4).to_s
)
puts(
'third_greatest([2, 3, 7, 4]) == 3: ' +
(third_greatest([2, 3, 7, 4]) == 3).to_s
)
puts("===============================================")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment