Project Euler #18 - how to brute force all possible paths in tree-like structure using Python?

2024/5/19 21:29:56

Am trying to learn Python the Atlantic way and am stuck on Project Euler #18.

All of the stuff I can find on the web (and there's a LOT more googling that happened beyond that) is some variation on 'well you COULD brute force it, but here's a more elegant solution'...

I get it, I totally do. There are really neat solutions out there, and I look forward to the day where the phrase 'acyclic graph' conjures up something more than a hazy, 1 megapixel resolution in my head. But I need to walk before I run here, see the state, and toy around with the brute force answer.

So, question: how do I generate (enumerate?) all valid paths for the triangle in Project Euler #18 and store them in an appropriate python data structure? (A list of lists is my initial inclination?). I don't want the answer - I want to know how to brute force all the paths and store them into a data structure.

Here's what I've got. I'm definitely looping over the data set wrong. The desired behavior would be to go 'depth first(?)' rather than just looping over each row ineffectually.. I read ch. 3 of Norvig's book but couldn't translate the psuedo-code. Tried reading over the AIMA python library for ch. 3 but it makes too many leaps.

triangle = [[75],[95, 64],[17, 47, 82],[18, 35, 87, 10],[20,  4, 82, 47, 65],[19,  1, 23, 75,  3, 34],[88,  2, 77, 73,  7, 63, 67],[99, 65,  4, 28,  6, 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,  4, 68, 89, 53, 67, 30, 73, 16, 69, 87, 40, 31],[04, 62, 98, 27, 23,  9, 70, 98, 73, 93, 38, 53, 60,  4, 23],
]def expand_node(r, c):return [[r+1,c+0],[r+1,c+1]]all_paths = []
my_path = []for i in xrange(0, len(triangle)):for j in xrange(0, len(triangle[i])):print 'row ', i, ' and col ', j, ' value is ', triangle[i][j]??my_path = somehow chain these together???if my_path not in all_pathsall_paths.append(my_path)

Answers that avoid external libraries (like itertools) preferred.

Answer

It's easier to use recursion here:

def all_paths(r, c):current = triangle[r][c]if r < len(triangle) - 1:below_paths = all_paths(r+1, c) + all_paths(r+1, c+1)return [[current] + path for path in below_paths]else:return [[current]]

Here, the idea is that all_paths(r, c) return all paths starting at row r, column c, which is usually obtained by recursively taking all paths from both nodes below it, and prepending the current element to all of them.

If we're at the last row, we just return the path consisting of a single element.

To get all paths starting from the top, call all_paths(0, 0).

This function can then easily be modified to return the sums of each path rather than the paths themselves, and with further modification it can return only the largest sum rather than all of them. The "proper way" to solve this problem is essentially just a memoized version of that.

https://en.xdnf.cn/q/73275.html

Related Q&A

Is it possible to sniff the Character encoding?

I have a webpage that accepts CSV files. These files may be created in a variety of places. (I think) there is no way to specify the encoding in a CSV file - so I can not reliably treat all of them as …

numpy.empty giving nonempty array

When I create an empty numpy array using foo = np.empty(1) the resulting array contains a float64:>>> foo = np.empty(1) >>> foo array([ 0.]) >>> type(foo[0]) <type numpy.f…

Accessing password protected url from python script

In python, I want to send a request to a url which will return some information to me. The problem is if I try to access the url from the browser, a popup box appears and asks for a username and passwo…

Solving multiple linear sparse matrix equations: numpy.linalg.solve vs. scipy.sparse.linalg.spsolve

I have to solve a large amount of linear matrix equations of the type "Ax=B" for x where A is a sparse matrix with mainly the main diagonal populated and B is a vector. My first approach was …

I want to return html in a flask route [duplicate]

This question already has answers here:Python Flask Render Text from Variable like render_template(4 answers)Closed 6 years ago.Instead of using send_static_file, I want to use something like html(<…

Why doesnt cv2 dilate actually affect my image?

So, Im generating a binary (well, really gray scale, 8bit, used as binary) image with python and opencv2, writing a small number of polygons to the image, and then dilating the image using a kernel. Ho…

How to plot text clusters?

I have started to learn clustering with Python and sklearn library. I have wrote a simple code for clustering text data. My goal is to find groups / clusters of similar sentences. I have tried to plot…

Selenium - Unresponsive Script Error (Firefox)

This question has been asked before, but the answer given does not seem to work for me. The problem is, when opening a page using Selenium, I get numerous "Unresponsive Script" pop ups, refe…

Fail to validate URL in Facebook webhook subscription with python flask on the back end and ssl

Im trying to start using new messenger platform from FB. So i have server with name (i.e.) www.mysite.com I got a valid SSL certificate for that domain and apache is setup correctly - all good.I have …

What is a proper way to test SQLAlchemy code that throw IntegrityError?

I have read this Q&A, and already try to catch exception on my code that raise an IntegrityError exception, this way :self.assertRaises(IntegrityError, db.session.commit())But somehow my unit test …