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