Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. """
  2. Hi, here's your problem today. This problem was recently asked by Facebook:
  3.  
  4. Given a list, find the k-th largest element in the list.
  5. Input: list = [3, 5, 2, 4, 6, 8], k = 3
  6. Output: 5
  7. Here is a starting point:
  8.  
  9. def findKthLargest(nums, k):
  10. # Fill this in.
  11.  
  12. print findKthLargest([3, 5, 2, 4, 6, 8], 3)
  13. # 5
  14. """
  15.  
  16. def findKthLargest(nums, k):
  17. if nums:
  18. try:
  19. nums.sort()
  20. return nums[-k]
  21.  
  22. except IndexError:
  23. return "There are not " + str(k) + " numbers in this list."
  24.  
  25. else:
  26. return "This list is empty."
  27.  
  28. print(findKthLargest([3, 5, 2, 4, 6, 8], 3))
  29.  
  30. """
  31. Copyright 2019 Eno Gerguri
  32. Permission is hereby granted, free of charge, to any person obtaining a copy of this software
  33. and associated documentation files (the "Software"), to deal in the Software without restriction,
  34. including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
  35. and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
  36. subject to the following conditions:
  37. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  38. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  39. TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  40. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  41. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  42. OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  43. """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement