-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_convert.py
More file actions
93 lines (75 loc) · 2.79 KB
/
json_convert.py
File metadata and controls
93 lines (75 loc) · 2.79 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import json
from pprint import pprint
def json_convert ():
with open('products.json') as data_local:
raw_data = json.load(data_local)
data = raw_data['products']
#all variants
lstVariants = {}
#list of lists; product name concat, price, weight
prodInfo = []
#uses list of titles for comparison
dictKeys = []
for d in data:
for key, value in d.items():
if key == 'title' and ('Computer' in value or 'Keyboard' in value):
lstVariants[value] = d['variants']
dictKeys.append(value)
for dkey in dictKeys:
for d in lstVariants[dkey]:
for key, value in d.items():
if key == 'title':
titleValue = value + ' ' + dkey
elif key == 'price':
priceValue = value
elif key == 'grams':
weightValue = value
prodInfo.append([titleValue, priceValue, weightValue])
return prodInfo
#finds smallest product in terms of weight
def find_smallest(lstProducts):
smallest = lstProducts[0]
for title, price, weight in prodInfo:
if weight < smallest[2]:
smallest = [title,price,weight]
return smallest
#finds the total weight and price without the smallest item; returns list of price and weight subtotals
def NoSmallestTot (lstProducts):
smallestItem = find_smallest(lstProducts)
subTotWeight = 0
subTotPrice = 0.00
for item in lstProducts:
if smallestItem[0] == item[0]:
lstProducts.remove(item)
else:
subTotWeight = subTotWeight + item[2]
subTotPrice = subTotPrice + float(item[1])
subTot = [subTotPrice,subTotWeight,lstProducts]
return subTot
#outputs list of total price, total weight, how many additional of the smallest item
def checkoutTotal (price, weight, smallestItem):
totPrice = price
totWeight = weight
additionalSmall = 0
while True:
totPrice = totPrice + float(smallestItem[1])
totWeight = totWeight + int(smallestItem[2])
additionalSmall = additionalSmall + 1
if (totWeight + int(smallestItem[2])) > 100000:
break
totals = [totPrice,totWeight,additionalSmall]
return (totals)
#MAIN
#create list of all computers and keyboards with their variants
prodInfo = json_convert()
#find smallest weight
smallestItem = find_smallest(prodInfo)
lstPriceWeight = NoSmallestTot(prodInfo)
noSmallList = lstPriceWeight[2]
subTotPrice = lstPriceWeight[0]
subTotWeight = lstPriceWeight[1]
checkout = checkoutTotal(subTotPrice,subTotWeight,smallestItem)
for item in noSmallList:
print ("1 " + item[0] )
print (str(checkout[2]) + " " + smallestItem[0] + "s")
print ("Total Price: {0:.2f}".format(checkout[0]) +" Total Weight: " + str(checkout[1]))