Created
October 3, 2017 19:18
-
-
Save barbchoy/fd2e7f4a6d4e779b8b5281c14b34f147 to your computer and use it in GitHub Desktop.
third_greatest_alt created by barbchoy - https://repl.it/Br7X/2749
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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=[] | |
new_nums=[] | |
i=0 | |
j=0 | |
biggest_i=0 | |
biggest_num=0 | |
while j< nums.length | |
while i<nums.length | |
if nums[i]>=biggest_num | |
biggest_i=i | |
biggest_num=nums[biggest_i] | |
end | |
i+=1 | |
end | |
j+=1 | |
if j==nums.length | |
sorted_nums.push(nums[biggest_i]) | |
nums.delete(biggest_num) | |
biggest_num=0 | |
j=0 | |
i=0 | |
end | |
end | |
return sorted_nums[2] | |
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