algorithm - programming challenge help (python)? -


this question has answer here:

i'm trying solve project euler problem 18/67 . have an attempt isn't correct.

tri = '''\     75     95 64     17 47 82     18 35 87 10     20 04 82 47 65     19 01 23 75 03 34     88 02 77 73 07 63 67     99 65 04 28 06 16 70 92     41 41 26 56 83 40 80 70 33     41 48 72 33 47 32 37 16 94 29     53 71 44 65 25 43 91 52 97 51 14     70 11 33 28 77 73 17 78 39 68 17 57     91 71 52 38 17 14 91 43 58 50 27 29 48     63 66 04 68 89 53 67 30 73 16 69 87 40 31     04 62 98 27 23 09 70 98 73 93 38 53 60 04 23''' sum = 0 spot_index = 0  triarr = list(filter(lambda e: len(e) > 0, [[int(nm) nm in ln.split()] ln in tri.split('\n')])) in triarr:     if len(i) == 1:         sum += i[0]     elif len(i) == 2:         spot_index = i.index(max(i))         sum += i[spot_index]     else:         spot_index = i.index(max(i[spot_index],i[spot_index+1]))         sum += i[spot_index]  print(sum) 

when run program, little bit off of correct sum/output should be. i'm pretty sure it's algorithm problem, don't know how fix or best approach original problem might be.

here's algorithm. i'll let figure out way code it.

start 2 bottom rows. @ each element of next-to-bottom row, figure out sum if reach element adding maximum of 2 elements of bottom row correspond current element of next-to-bottom row. instance, given sample above, left-most element of next-to-bottom row 63, , if ever reach element, choose right child 62. can replace 63 on next-to-bottom row 63 + 62 = 125. same each element of next-to-bottom row; 125, 164, 102, 95, 112, 123, 165, 128, 166, 109, 112, 147, 100, 54. delete bottom row , repeat on reduced triangle.

there top-down algorithm dual 1 given above. i'll let figure out, too.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -