473,545 Members | 2,688 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2291
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.c omwrote:
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.c omwrote:
>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.c omwrote:
>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
4120
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
5559
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 complexity as I go. Here's a simple one I tried out: #include<stdio.h> /* To compare the the time and space cost of iteration against
18
3705
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 python is generally more time-efficient than recursion. Is that true? Here is my algorithm as it stands. Any suggestions appreciated!
10
9077
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
5809
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 asterisk is in a blob, an asterisk that is contiguous to it is in the same blob. If a blob has more than two asterisks, then each asterisk in the blob...
7
6849
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 possibles situations. Prefer to get something on c++ or java, but other languages are welcome also. Tnanks
1
3305
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 euler circuit it will output an euler circuit. can any1 give me some starting point or hints ? thanks for viewing...
13
2891
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
4689
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
7502
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7434
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7946
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7791
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
3491
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3470
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1921
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1045
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
744
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.