/solutions/extra_credit_solutions/Lab04_4.py

http://github.com/aiti-ghana-2012/Lab_Python_04 · Python · 157 lines · 55 code · 32 blank · 70 comment · 8 complexity · aa41754ef3632c0bc43f14c4648dcfe3 MD5 · raw file

  1. """
  2. Lab_Python_04
  3. Extra Credit
  4. Solution to Extra Credit problem 4
  5. """
  6. ## Part A
  7. # The right data structure to store the different market prices
  8. # would be a dictionary whose keys are food items, and values
  9. # either a list of prices, or another dictionary that maps
  10. # the names of different stores to the price that they offer
  11. # for example
  12. multi_price_with_store_names = {
  13. 'bread' : {
  14. 'shoprite' : 4.5,
  15. 'maxi-mart' : 5.0,
  16. 'r-link' : 0.5
  17. },
  18. 'water' : {
  19. 'shoprite' : 0.9,
  20. 'maxi-mart' : 0.8,
  21. 'r-link' : 0.5
  22. }
  23. }
  24. # or...
  25. multi_price_without_store_names = {
  26. 'bread' : [
  27. 4.5,
  28. 5.0,
  29. 0.5
  30. ],
  31. 'water' : [
  32. 0.9,
  33. 0.8,
  34. 0.5
  35. ]
  36. }
  37. # ultimately, what you need to do with the data will determine which data structure
  38. # is better to use.
  39. ## Part B
  40. # Binary Search
  41. # Binary search insertion is very difficult to get right
  42. # and it was awesome that some of you did it!
  43. def binary_insert(new_float, some_list_of_floats):
  44. """
  45. Binary Search works when inserting or searching through
  46. a sorted list. It works by repeatedly cutting the search
  47. space in half, until you find the place for which you look.
  48. It is similar to looking up a word in a dictionary.
  49. First, you open up half way, then check if the word you seek
  50. is before or after a word on that page. If it is after, you
  51. go to the middle of the second half of the dictionary,
  52. and if it is before, you go to the middle of the first
  53. half of the dictionary. This process repeats until you have
  54. found the definition.
  55. If you have questions about this algorithm or algorithms in
  56. general, I love to talk about them, and would definitely
  57. like to chat about algorithms, searching, sorts, runtime,
  58. et cetera!
  59. Also, this problem is notoriously hard to do correctly,
  60. so if you notice a bug in the code please let me know!!
  61. """
  62. # upper and lower are variables that bound the range
  63. # of the list in which we are searching
  64. # initially, we are searching the whole list,
  65. # so upper is the last element, and lower is 0, the first
  66. upper = len(some_list_of_floats) - 1
  67. lower = 0
  68. # mid is the middle of the range
  69. mid = (upper + lower) / 2
  70. # checking for corner solutions
  71. # if the input list is empty
  72. if upper == -1:
  73. some_list_of_floats.append(new_float)
  74. return
  75. # (when the new element is bigger or smaller than every other one)
  76. if new_float <= some_list_of_floats[lower]:
  77. # then we want to insert it before that element
  78. some_list_of_floats.insert(lower, new_float)
  79. return
  80. elif new_float >= some_list_of_floats[upper]:
  81. #then we want to insert it after that element
  82. some_list_of_floats.insert(upper + 1, new_float)
  83. return
  84. #while our range contains elements...
  85. while upper - lower > 0:
  86. if new_float < some_list_of_floats[mid]:
  87. # then we want to look at the left half
  88. upper = mid
  89. elif new_float > some_list_of_floats[mid]:
  90. # then we want to look at the right half
  91. # the plus one is important, because when
  92. # we find out mid, we round down
  93. lower = mid + 1
  94. else:
  95. # then new_float is equal to the middle one,
  96. # so we can insert it on either side
  97. some_list_of_floats.insert(mid,new_float)
  98. return
  99. mid = (upper + lower) / 2
  100. #ok, when we are here, we have found the spot!
  101. some_list_of_floats.insert(lower,new_float)
  102. return
  103. ## Part C
  104. # finding the min cost
  105. # this problem is not so tricky once you see
  106. # the the minimum cost is the sum of the minimum costs
  107. # for each item. The hard thing is understanding
  108. # and working with the given data structures
  109. def min_cost(grocery_list, item_to_price_list_dict):
  110. total_cost = 0
  111. for item in grocery_list:
  112. # for each item, we want to get the minimum
  113. # price out of all of them
  114. # first, get the list of prices for this item
  115. list_of_prices = item_to_price_list_dict[item]
  116. # then, find the minimum
  117. # python has a convenient min() function which
  118. # takes a list and returns the smallest element
  119. lowest_price_for_item = min(list_of_prices)
  120. # now we add it to our running total of cost
  121. total_cost += lowest_price_for_item
  122. return total_cost