Here is the question: # Write a method that takes an array of numbers in. Your m
ID: 3734281 • Letter: H
Question
Here is the question:
# 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.
I am having trouble understanding this answer, could you please explain it to me?
The language is Ruby. And the answer is correct. It has been tested.
Thank you
def third_greatest (nums) first -nil secondnil thirdnil while idx first third second second - first first - value elsif second - nil || value > second third - second secondvalue elsif thirdnil || value > third end idx +- 1 third - value end return third endExplanation / Answer
//Explanation:
Example : [5, 4, 3, 8, 7] //since the length of list is 5, there will be 5 runs:
First run: value = 5
if first == nil || value > first //this condition gets executed
third = nil
second = nil
first = 5
Second run: value = 4
elsif second == nil || value > second //this condition gets executed
third = nil
second = 4
Third run : value = 3
elsif third == nil || value > third //this condition gets executed
third = 3
Fourth run: value = 8
if first == nil || value > first //this condition gets executed
third = 4
second = 5
first = 8
Fifth run: value = 7
elsif second == nil || value > second //this condition gets executed
third = 5
second = 7
return third (5)