Advertisement
Guest User

Untitled

a guest
May 26th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. # This way uses .sort and will return the 3rd greatest number in an array
  2.  
  3. def thirdgreatest(nums)
  4. i = 0
  5. nums.sort
  6. nums[2]
  7. end
  8. p thirdgreatest([4,5,6,7])
  9.  
  10. # This way just uses while/if etc...
  11.  
  12. def third_greatest(nums)
  13. first = nil
  14. second = nil
  15. third = nil
  16. idx = 0
  17. while idx < nums.length
  18. value = nums[idx]
  19. if first == nil || value > first
  20. third = second
  21. second = first
  22. first = value
  23. elsif second == nil || value > second
  24. third = second
  25. second = value
  26. elsif third == nil || value > third
  27. third = value
  28. end
  29. idx += 1
  30. end
  31. return third
  32. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement