forked from atyant-yadav/Hacktoberfest2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
equalize weights try shikhar.py
55 lines (42 loc) · 1.18 KB
/
equalize weights try shikhar.py
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
def fillBag(l,eachBagWeight):
bag=[]
while sum(bag)!=eachBagWeight:
print("value of l: ",l)
bag.append(l[0])
l.remove(l[0])
nextWeight=eachBagWeight-sum(bag)
if(nextWeight in l):
bag.append(nextWeight)
l.remove(nextWeight)
return bag
else:
if(nextWeight<bag[-1] and nextWeight!=0):
bag.pop()
else:
continue
return bag
noOfBags=int(input())
bagsInitialSituation=[]
for i in range(noOfBags):
l=[]
noOfGoods=int(input())
bagsInitialSituation.append(list(map(int,input().strip().split())))
bagsFinalSituation=[]
totalWeight=0
temp=[]
for i in bagsInitialSituation:
for j in i:
totalWeight=totalWeight+j
temp.append(j)
eachBagWeight=totalWeight//noOfBags
temp=sorted(temp)
for i in range(noOfBags):
l=temp.copy()
bag=fillBag(l,eachBagWeight)
for j in bag:
temp.remove(j)
bagsFinalSituation.append(bag)
for i in bagsFinalSituation:
for j in i:
print(j,end=' ')
print()