Created
December 9, 2018 14:06
-
-
Save wilcoln/1073a95888eff9a0af7f93eb939344d6 to your computer and use it in GitHub Desktop.
Partitioning souvenirs into three equal parts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# >> Code for quick sort | |
def partition(a,p,r): | |
pivot = p | |
for i in range(p+1,r+1): | |
if(a[i]<a[pivot]): | |
tmp = a[i] | |
a[i] = a[pivot+1] | |
a[pivot+1] = a[pivot] | |
a[pivot] = tmp | |
pivot = pivot + 1 | |
return pivot | |
def quickSort(a,p,r): | |
if(r>p): | |
q = partition(a, p, r) | |
quickSort(a,p,q-1) | |
quickSort(a,q+1,r) | |
# << End of code for quick sort | |
def partition3(A): | |
if(sum(A) % 3 == 0): | |
quickSort(A,0,len(A) - 1) | |
sums = [[],[],[]] | |
i = len(A) - 1 | |
while(i > - 1): | |
k = 0 | |
pacted = False | |
while(not pacted and k < len(sums)): | |
if(sum(sums[k]) + A[i] <= sum(A)//3): | |
sums[k].append(A[i]) | |
pacted = True | |
k+=1 | |
i -=1 | |
return int(sum(sums[0]) == sum(sums[1]) == sum(sums[2]) == sum(A)//3) | |
return 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment