3rd Greatest Number

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.

Jimmy suggested figuring out how to return the greatest number first.

Screen Shot 2016-08-15 at 5.13.20 PM

..then the second greatest:

Screen Shot 2016-08-16 at 12.12.32 PM

then the third:

Screen Shot 2016-08-16 at 12.14.04 PM

Provided solution:

I had this line of thinking going but I didn’t understand how to use nil very well.  In this solution first , second, and third are being initialized with ‘nil’ and the current number (incremented by idx) being compared is stored in ‘value’.

compare each number against the rest of the array, once.

if the current value is greater than the first, second, or third, replace it and move other numbers up by 1 respectively.

 

Screen Shot 2016-08-16 at 12.14.51 PM

Second time solving this by myself:

My solution is a bit long but it’s pretty intuitive I think.  I basically broke the problem into three pieces – greatest, second greatest, and third greatest.  First, I wrote a function to find the first greatest number.  Then I use the result of that function to write another function to find the second greatest number.  Finally, I used the results of both first greatest and second greatest to write a function to find third greatest number.

screen-shot-2016-09-12-at-3-31-36-pmscreen-shot-2016-09-12-at-3-31-53-pm

 

Advertisements
3rd Greatest Number

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s