473,396 Members | 1,918 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Pr. Euler 18, recursion problem

I am trying to solve project euler problem 18 with brute force(I will
move on to a better solution after I have done that for problem 67).
http://projecteuler.net/index.php?se...problems&id=18

However I can't get the recursive function right.

I always have to use return right? Unless I am printing? So I canät
stack to diffferent recursive calls after each other like so:
recur_left(t, p)
recur_right(t,p+1)
Some stuff I have tried:

def recur(tree, pos):
if not tree:
return []
else:
return [[tree[0][pos]] + recur(tree[1:], pos)] + \
[[tree[0][pos]] + recur(tree[1:], pos+1)]

>>recur([[1],[2,3],[4,5,6]],0)
[[1, [2, [4], [4]], [2, [5], [5]]], [1, [3, [5], [5]], [3, [6], [6]]]]
SO it is kind of working, just not exactly like I want.
A more easily parseable/readable result would be nice, I want to be
able to sum() over each path preferrably.

So the result should be:
[[1,2,4],[1,2,5],[1,3,5],[1,3,6]]

I know conceptually what has to be done.
Base case: empty tree, return []
Else: recur to the left and to the right.
Oct 6 '08 #1
4 2284
process wrote:
I am trying to solve project euler problem 18 with brute force(I will
move on to a better solution after I have done that for problem 67).
http://projecteuler.net/index.php?se...problems&id=18

However I can't get the recursive function right.

I always have to use return right? Unless I am printing? So I canät
stack to diffferent recursive calls after each other like so:
recur_left(t, p)
recur_right(t,p+1)
Some stuff I have tried:

def recur(tree, pos):
if not tree:
return []
else:
return [[tree[0][pos]] + recur(tree[1:], pos)] + \
[[tree[0][pos]] + recur(tree[1:], pos+1)]

>>>recur([[1],[2,3],[4,5,6]],0)
[[1, [2, [4], [4]], [2, [5], [5]]], [1, [3, [5], [5]], [3, [6], [6]]]]
SO it is kind of working, just not exactly like I want.
A more easily parseable/readable result would be nice, I want to be
able to sum() over each path preferrably.

So the result should be:
[[1,2,4],[1,2,5],[1,3,5],[1,3,6]]

I know conceptually what has to be done.
Base case: empty tree, return []
Else: recur to the left and to the right.
This is just my opinion, but I felt the non-brute force solution to this
problem was actually simpler than trying to define a brute force
recursive solution.... I tried to implement a brute force algorithm at
first, until I had an epiphany with regard to how simple the problem
actually was. Then I faced palmed.
Oct 6 '08 #2
On Oct 6, 8:13*am, Aidan <awe...@gmail.comwrote:
process wrote:
I am trying to solve project euler problem 18 with brute force(I will
move on to a better solution after I have done that for problem 67).
http://projecteuler.net/index.php?se...problems&id=18
However I can't get the recursive function right.
I always have to use return right? Unless I am printing? So I canät
stack to diffferent recursive calls after each other like so:
recur_left(t, p)
recur_right(t,p+1)
Some stuff I have tried:
def recur(tree, pos):
* * if not tree:
* * * * return []
* * else:
* * * * return [[tree[0][pos]] + recur(tree[1:], pos)] + \
* * * * * * * *[[tree[0][pos]] + recur(tree[1:], pos+1)]
>>recur([[1],[2,3],[4,5,6]],0)
[[1, [2, [4], [4]], [2, [5], [5]]], [1, [3, [5], [5]], [3, [6], [6]]]]
SO it is kind of working, just not exactly like I want.
A more easily parseable/readable result would be nice, I want to be
able to sum() over each path preferrably.
So the result should be:
[[1,2,4],[1,2,5],[1,3,5],[1,3,6]]
I know conceptually what has to be done.
Base case: empty tree, return []
Else: recur to the left and to the right.

This is just my opinion, but I felt the non-brute force solution to this
problem was actually simpler than trying to define a brute force
recursive solution.... I tried to implement a brute force algorithm at
first, until I had an epiphany with regard to how simple the problem
actually was. *Then I faced palmed.


But let's say you have [[1],[1,10],[1,2,300],[100000,1,1,1]].

you must check all solutions right? there is no pattern. if you start
from the bottom and eliminate paths that seem to be losing can you
regain that later up in the pyramid if it turns out one side gets bigg
again?
Oct 6 '08 #3
process wrote:
On Oct 6, 8:13 am, Aidan <awe...@gmail.comwrote:
>process wrote:
>>I am trying to solve project euler problem 18 with brute force(I will
move on to a better solution after I have done that for problem 67).
http://projecteuler.net/index.php?se...problems&id=18
However I can't get the recursive function right.
I always have to use return right? Unless I am printing? So I canät
stack to diffferent recursive calls after each other like so:
recur_left(t, p)
recur_right(t,p+1)
Some stuff I have tried:
def recur(tree, pos):
if not tree:
return []
else:
return [[tree[0][pos]] + recur(tree[1:], pos)] + \
[[tree[0][pos]] + recur(tree[1:], pos+1)]
>recur([[1],[2,3],[4,5,6]],0)
[[1, [2, [4], [4]], [2, [5], [5]]], [1, [3, [5], [5]], [3, [6], [6]]]]
SO it is kind of working, just not exactly like I want.
A more easily parseable/readable result would be nice, I want to be
able to sum() over each path preferrably.
So the result should be:
[[1,2,4],[1,2,5],[1,3,5],[1,3,6]]
I know conceptually what has to be done.
Base case: empty tree, return []
Else: recur to the left and to the right.
This is just my opinion, but I felt the non-brute force solution to this
problem was actually simpler than trying to define a brute force
recursive solution.... I tried to implement a brute force algorithm at
first, until I had an epiphany with regard to how simple the problem
actually was. Then I faced palmed.

But let's say you have [[1],[1,10],[1,2,300],[100000,1,1,1]].

you must check all solutions right? there is no pattern. if you start
from the bottom and eliminate paths that seem to be losing can you
regain that later up in the pyramid if it turns out one side gets bigg
again?
It's difficult to say much here without giving the answer away... but,
yes, you need to check all solutions - it's just that there's a very
easy way to do that without having to recurse from the top of the tree
to the bottom.

Hope that gives you a clue while not fully revealing the answer.
Oct 6 '08 #4
On Mon, 06 Oct 2008 00:14:37 -0700, process wrote:
On Oct 6, 8:13Â*am, Aidan <awe...@gmail.comwrote:
>process wrote:
I am trying to solve project euler problem 18 with brute force(I will
move on to a better solution after I have done that for problem 67).
http://projecteuler.net/index.php?se...problems&id=18
However I can't get the recursive function right.
I always have to use return right? Unless I am printing? So I canät
stack to diffferent recursive calls after each other like so:
recur_left(t, p)
recur_right(t,p+1)
Some stuff I have tried:
def recur(tree, pos):
Â* Â* if not tree:
Â* Â* Â* Â* return []
Â* Â* else:
Â* Â* Â* Â* return [[tree[0][pos]] + recur(tree[1:], pos)] + \
Â* Â* Â* Â* Â* Â* Â* Â*[[tree[0][pos]] + recur(tree[1:], pos+1)]
>>>recur([[1],[2,3],[4,5,6]],0)
[[1, [2, [4], [4]], [2, [5], [5]]], [1, [3, [5], [5]], [3, [6],
[6]]]]
SO it is kind of working, just not exactly like I want. A more easily
parseable/readable result would be nice, I want to be able to sum()
over each path preferrably.
So the result should be:
[[1,2,4],[1,2,5],[1,3,5],[1,3,6]]
I know conceptually what has to be done. Base case: empty tree,
return []
Else: recur to the left and to the right.

This is just my opinion, but I felt the non-brute force solution to
this problem was actually simpler than trying to define a brute force
recursive solution.... I tried to implement a brute force algorithm at
first, until I had an epiphany with regard to how simple the problem
actually was. Â*Then I faced palmed.

But let's say you have [[1],[1,10],[1,2,300],[100000,1,1,1]].

you must check all solutions right? there is no pattern. if you start
from the bottom and eliminate paths that seem to be losing can you
regain that later up in the pyramid if it turns out one side gets bigg
again?
A Wise Man once says: "When you're hopelessly stuck with a problem,
reverse the problem"

Oct 9 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

43
by: Lorenzo Villari | last post by:
I've tried to transform this into a not recursive version but without luck... #include <stdio.h> void countdown(int p) { int x;
75
by: Sathyaish | last post by:
Can every problem that has an iterative solution also be expressed in terms of a recursive solution? I tried one example, and am in the process of trying out more examples, increasing their...
18
by: MTD | last post by:
Hello all, I've been messing about for fun creating a trial division factorizing function and I'm naturally interested in optimising it as much as possible. I've been told that iteration in...
10
by: Protoman | last post by:
Hi! I'm trying to compute Euler's totient function for an extremely simple RSA program I'm writing. How, exactly, is it calculated? Thanks!
12
by: NOO Recursion | last post by:
Hi everyone! I am trying to write a program that will search a 12x12 for a thing called a "blob". A blob in the grid is made up of asterisks. A blob contains at least one asterisk. If an...
7
by: dkultasev | last post by:
Hello, could anyone help with euler circuit ? I tried to find it, found some, but they are not working. Tried to write my own one, but it supposed to be slow with big graphs because it trying all...
1
by: luna18 | last post by:
i like to do programming but i am not a computer student.. =) i m trying to write a program to determine a euler circuit.but end up all stuck... i tyr to take the input as graph and if it is a...
13
by: Mumia W. | last post by:
Hello all. I have a C++ program that can count the YOYOs that are in a grid of Y's and O's. For example, this Y O Y O O Y O Y O Y O O Y O Y Y O Y O Y O O Y O O Y Y O Y O
35
by: Muzammil | last post by:
int harmonic(int n) { if (n=1) { return 1; } else { return harmonic(n-1)+1/n; } } can any help me ??
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.