forked from aiti-ghana-2012/Lab_Python_04
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab04_4.py
More file actions
62 lines (57 loc) · 1.75 KB
/
Copy pathLab04_4.py
File metadata and controls
62 lines (57 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#a
"""
The data structure that could be used is a dictionary
"""
#b
def binary_insert(new_float,some_list_of_floats):
lower=0
upper=len(some_list_of_floats)
mid=(lower+upper)/2
count=0
if new_float<=some_list_of_floats[lower]:
return lower
elif new_float >= some_list_of_floats[upper-1]:
return upper
else:
# print upper
# print lower
while mid!=lower:
#print upper
#print lower
if new_float>some_list_of_floats[mid]:
lower=mid
mid=(lower+upper)/2
elif new_float<some_list_of_floats[mid]:
upper=mid
mid=(lower+upper)/2
elif new_float==some_list_of_floats[mid]:
return mid
return mid+1
pricelist=[0.0,0.10,0.20,0.30,0.50,1.25,1.50,2.0]
invalue=2.0
inloc=binary_insert(invalue,pricelist)
print pricelist
print 'inserting ',invalue
pricelist.insert(inloc,invalue)
print pricelist
print '\n'
#c
print
def min_cost(groc_lst,groc_price_lst):
#grocery_list is a list of strings (item names)
#item_to_price_list_dict is a dictionary with key-value
# pairs as follows: the item name (strings) is the key
# and the list of prices (floats) at different grocery is
# the value
mincost=0
result=[]
for item in groc_lst:
groc_price_lst[item].sort()
mincost=mincost+ groc_price_lst[item][0]
print 'minimum cost for '+ item +' is ', groc_price_lst[item][0]
return mincost
groc_lst= ['bananas','strawberries','apples','bread']
groc_price_lst= {'bananas':[3.5,8.8,4.4],'strawberries':[0.30,0.20,0.50],'apples':[1.25,1.50,0.50,2.0],'bread':[5.0,8.0,3.0]}
mincost=min_cost(groc_lst,groc_price_lst)
print
print 'Total minimum cost is ', mincost