Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Numbered Triangles by Kurush
import itertools
import copy
def checkio(chips):
FromList = copy.deepcopy(chips)
ToList = []
MaxSum, MaxFinalList = FindSolution(FromList, ToList)
return MaxSum
def FindSolution(FromList, ToList):
MaxSum = 0
MaxFinalList = []
A = len(FromList)
for i in range(A):
for PermutationList in itertools.permutations(FromList[i]):
NewFromList = copy.deepcopy(FromList)
NewToList = copy.deepcopy(ToList)
if NewFromList != []:
if NewToList == []:
NewFromList.remove(NewFromList[i])
NewToList.append(PermutationList)
Sum, FinalList = FindSolution(NewFromList, NewToList)
if Sum > MaxSum:
MaxFinalList = copy.deepcopy(FinalList)
MaxSum = Sum
else:
if ToList[len(ToList) - 1][2] == PermutationList[0]:
NewFromList.remove(NewFromList[i])
NewToList.append(PermutationList)
Sum, FinalList = FindSolution(NewFromList, NewToList)
if Sum > MaxSum:
MaxFinalList = copy.deepcopy(FinalList)
MaxSum = Sum
if FromList == []:
if ToList[0][0] == ToList[5][2]:
Sum = 0
for i in range(len(ToList)):
Sum += ToList[i][1]
FinalList = copy.deepcopy(ToList)
return Sum, FinalList
if MaxSum == 0:
FinalList = []
return MaxSum, MaxFinalList
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
#assert checkio([[13,1,3],[13,5,2],[2,1,13],[13,5,5],[13,2,5],[1,3,13]]) == 19, 'FailedTest'
#(1, 13, 2), (2, 13, 5), (5, 13, 5), (5, 2, 13), (13, 1, 3), (3, 13, 1)
assert checkio(
[[1, 4, 20], [3, 1, 5], [50, 2, 3],
[5, 2, 7], [7, 5, 20], [4, 7, 50]]) == 152, "First"
assert checkio(
[[1, 10, 2], [2, 20, 3], [3, 30, 4],
[4, 40, 5], [5, 50, 6], [6, 60, 1]]) == 210, "Second"
assert checkio(
[[1, 2, 3], [2, 1, 3], [4, 5, 6],
[6, 5, 4], [5, 1, 2], [6, 4, 3]]) == 21, "Third"
assert checkio(
[[5, 9, 5], [9, 6, 9], [6, 7, 6],
[7, 8, 7], [8, 1, 8], [1, 2, 1]]) == 0, "Fourth"
Oct. 8, 2013