Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
20 changes: 20 additions & 0 deletions hw9.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import csv
from itertools import accumulate
from itertools import takewhile


max_weight = 400
itemlist = []

with open('items.csv') as csvfile:
csvfile.readline()
csvreader = csv.reader(csvfile)
for row in csvreader:
itemlist.append(row)

sorted_list = list(reversed(sorted(itemlist,
key=(lambda item:
int(item[2]) / int(item[1])))))
running_add = takewhile(lambda z: z < max_weight,
(accumulate([int(i[1]) for i in sorted_list])))
print(sorted_list[0: len(list(running_add))])
23 changes: 23 additions & 0 deletions items.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
item,weight(dag),value
map,9,150
compass,13,35
water,153,200
sandwich,50,160
glucose,15,60
tin,68,45
banana,27,60
apple,39,40
cheese,23,30
beer,52,10
suntan cream,11,70
camera,32,30
T-shirt,24,15
trousers,48,10
umbrella,73,40
waterproof trousers,42,70
waterproof overclothes,43,75
note-case,22,80
sunglasses,7,20
towel,18,12
socks,4,50
book,30,10