Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. """Module containing some methods for finding the largest k
  2. items in a list.
  3. """
  4. from basic_priority_queue import BasicPriorityQueue
  5. # import the following when you are ready to use it
  6. from dheap_priority_queue import DheapPriorityQueue
  7.  
  8.  
  9.  
  10. def top_k_select(items, k):
  11. """Uses a modified max-selection sort to find the max k items of a list.
  12. This sort terminates once the max k values are found. Note that
  13. this function does not modify the list items.
  14. This function returns a tuple containing the following:
  15. 1: a descending list of the top k items, and
  16. 2: the number of item comparisons made
  17. That is, it returns result_list, comparisons
  18. """
  19. items = list(items) # Makes a copy of items.
  20. comparisons = 0
  21. # ---start student section---
  22.  
  23. out_list = []
  24. i = 0
  25. while i != k:
  26. holder = 0
  27. for index in range(0,len(items) - 1):
  28. comparisons += 1
  29. if items[index] > items[holder]:
  30. holder = index
  31. out_list.append(items[holder])
  32. items.pop(holder)
  33. i += 1
  34. out_tup = (out_list, comparisons)
  35. return out_tup
  36. # ===end student section===
  37.  
  38.  
  39. def top_k_heap(items, k):
  40. """Uses a BasicPriorityQueue to find the max k items of a list. Note this
  41. function does not modify the list items.
  42. This function returns a tuple containing the following:
  43. 1: a descending list of the top k items, and
  44. 2: the number of item comparisons made, ie, it returns result_list, comparisons
  45. That is, it returns result_list, comparisons
  46. """
  47. comparisons = 0
  48. items = list(items) # Makes a copy of items.
  49. p_queue = BasicPriorityQueue(items)
  50. out_list = []
  51. # ---start student section---
  52. for i in range(0, k):
  53. out_list.append(p_queue.pop_max())
  54. out_tup = (out_list, p_queue.get_comparisons())
  55. return out_tup
  56. # ===end student section===
  57.  
  58.  
  59. def top_k_dheap(items, k, branch_factor):
  60. """Uses a DheapPriorityQueue to find the max k items of a list. Note this
  61. function does not modify the list items.
  62. This function returns a tuple containing the following:
  63. 1: a descending list of the top k items, and
  64. 2: the number of item comparisons made, ie, it returns result_list, comparisons
  65. That is, it returns result_list, comparisons
  66.  
  67. NOTE: You can complete this function once you have your dheap working.
  68. """
  69. # ---start student section---
  70. pass
  71. # ===end student section===
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement