-
Notifications
You must be signed in to change notification settings - Fork 0
/
SortThePlates.py
54 lines (51 loc) · 1.2 KB
/
SortThePlates.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
def solution(l):
l.sort(reverse=True)
lList = largestList(l)
lList.sort(reverse=True)
if len(lList) < 1:
return 0
for i in range(len(lList)):
sNum = lList[i]
nNums = lList
nNums.remove(sNum)
totalNum = sNum
for j in range(len(nNums)):
num = nNums[j]
totalNum = int(str(totalNum) + str(num))
if totalNum % 3 == 0:
return totalNum
return 0
def largestList(l):
newList = l
newList.sort()
for n in range(len(newList)):
total = 0
if n % 3 == 0:
continue
else:
for num in newList:
total += num
if total % 3 != 0:
removableNumbers(total, newList, n=0)
if total % 3 == 0:
return newList
return []
def removableNumbers(total, list, n=0):
nList = list
nList.sort()
if nList[n] % 3 == 0:
if len(nList) > n+1:
removableNumbers(total, nList, n+1)
elif (total - nList[n]) % 3 == 0:
nList.remove(nList[n])
return
else:
for i in range(len(nList)):
if (total - nList[i]) % 3 == 0:
nList.remove(nList[i])
return
if len(nList) > n:
total -= nList[n]
nList.remove(nList[n])
removableNumbers(total, nList, n)
print(solution([9, 9, 4, 4, 3, 3, 1]))