Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. global HighestSum
  2. global gPath
  3.  
  4. def IsEven(num):
  5. return num % 2 == 0
  6.  
  7. matrix=[[215],
  8. [192,124],
  9. [117,269,442],
  10. [218,836,347,235],
  11. [320,805,522,417,345],
  12. [229,601,728,835,133,124],
  13. [248,202,277,433,207,263,257],
  14. [359,464,504,528,516,716,871,182],
  15. [461,441,426,656,863,560,380,171,923],
  16. [381,348,573,533,448,632,387,176,975,449],
  17. [223,711,445,645,245,543,931,532,937,541,444],
  18. [330,131,333,928,376,733,17,778,839,168,197,197],
  19. [131,171,522,137,217,224,291,413,528,520,227,229,928],
  20. [223,626,34,683,839,52,627,310,713,999,629,817,410,121],
  21. [924,622,911,233,325,139,721,218,253,223,107,233,230,124,233]]
  22.  
  23. def FindHighestSum(RowIndex, ColumnIndex, PreviousEven, CurrentSum, path, direction):
  24.  
  25. if(RowIndex >= len(matrix) or ColumnIndex >= len(matrix[RowIndex])):
  26. return
  27. else:
  28. global HighestSum
  29. global gPath
  30.  
  31. CurrentValue = matrix[RowIndex][ColumnIndex]
  32.  
  33. path += ColumnIndex*(' ') + (direction) + '\n' + \
  34. ColumnIndex*(' ') + str(CurrentValue) + '\n'
  35.  
  36. if((PreviousEven and IsEven(CurrentValue)) \
  37. or (not PreviousEven and not IsEven(CurrentValue)) \
  38. and RowIndex != 0):
  39. print('Invalid path\n' + path)
  40. return
  41.  
  42. CurrentSum += CurrentValue
  43. if(CurrentSum > HighestSum):
  44. HighestSum = CurrentSum
  45. gPath = path
  46.  
  47. FindHighestSum(RowIndex + 1, ColumnIndex,
  48. IsEven(CurrentValue), CurrentSum, path, '↓')
  49. FindHighestSum(RowIndex + 1, ColumnIndex + 1,
  50. IsEven(CurrentValue), CurrentSum, path, '↘')
  51.  
  52.  
  53. HighestSum = 0
  54. FindHighestSum(0, 0, False, 0, '', '')
  55.  
  56. print(HighestSum)
  57. print('Best path')
  58. print(gPath)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement