Esempio n. 1
0
#!/usr/bin/env python

import unittest
import collections
import utils
import copy
import os

fn = os.path.join(os.path.dirname(__file__), 'data/p18-triangle.txt')
f = open(fn, 'r')
p18Tri = utils.readTriangle(f)
f.close()

def euler18(triParam):
   # make a copy
   tri = copy.deepcopy(triParam)

   # iterate over rows, from bottom up
   for row in reversed(range(0,len(tri))):
      # base case
      if row == 0:
         return tri[row][0]

      # iterater over adjacent pairs of values in this row
      for col in range(0,len(tri[row])-1):
         col2 = col + 1

         # add the largest value to the parent from the next row up
         tri[row-1][col] += max(tri[row][col],tri[row][col2])

class Test(unittest.TestCase):
Esempio n. 2
0
#!/usr/bin/env python

import unittest
import utils
import os
from p18 import euler18

fn = os.path.join(os.path.dirname(__file__), 'data/p67-triangle.txt')
f = open(fn, 'r')
p67Tri = utils.readTriangle(f)
f.close()

class Test(unittest.TestCase):

   def testEuler67(self):
      self.assertEquals(euler18(p67Tri), 7273)

if __name__ == '__main__':
   unittest.main(exit=False)

   print "Problem 67: The maximum total from top to bottom is", euler18(p67Tri)