Easy programming problem

Kisze

Spy
Joined
Jun 3, 2009
Messages
210
Reaction score
1
So I'm using python in this beginners programming course on uni and I looked through some problems that the students last year had to solve (We might get the same problem later, not sure really) and one of them was to define a function using recursion that given an integer as an argument counts down to zero then back up again. So for example if I write:
>>> bounce(5)
the function bounce should return "5 4 3 2 1 0 1 2 3 4 5"

So I wrote this:

Code:
def bounce(n):
     print abs(n),
     bounce(n-1)

What I can't figure out is how to make it stop after n = -5 if the original input is 5? Or -10 if the original input is 10 etc etc. Right now it just counts down to infinity and beyond. Whether you want to post a solution or just give hints is up to you! I've been pulling my hair trying to figure it out since it's probably really easy and I'm not a even a beginner :/
 
I think the easiest solutions would need some way of remembering the initial number. There are a couple of ways of doing that, one requiring two functions, one with more arguments, perhaps.
 
Does python have while loops? I'm sure it does and normally that's how you would deal with the problem since a while loop will run until a certain condition is true (n == -5 for example)
 
Does python have while loops? I'm sure it does and normally that's how you would deal with the problem since a while loop will run until a certain condition is true (n == -5 for example)

define a function using recursion

A while loop does not fit the specification.

Although iteration is a specific form of recursion.
 
This is a really interesting problem , i just want to get a few things straight :

- You can only use one function

- the function signature takes one parameter i.e : function foo(int arg) { //do something }

is that right ?.
 
Done , You dont need to store anything . you just need to understand how the function calls are made.

public static void printRecur(int num) {
System.out.println(num);
if(num==0) return;
printRecur(num-1);
System.out.println(num);
}

not sure if you can translate that into python , i've never used python before.
 
Done , You dont need to store anything . you just need to understand how the function calls are made.

public static void printRecur(int num) {
System.out.println(num);
if(num==0) return;
printRecur(num-1);
System.out.println(num);
}

not sure if you can translate that into python , i've never used python before.

:O that's nice. Thanks! Never thought of that. I understand how it works with all the nesting but it took me a while to get it even after seeing it. Also your code translated to python would be:
Code:
def printRecur(num):
     print num,
     if(num==0):
          return
     printRecur(num-1)
     print n,
 
The solution's pretty simple. Remember that you're using recursive function calls. Simply print the value before recursing down the tree and print it again afterwards (apart from the terminating case of 0).

Code:
def bounce( n ):
	print abs( n ),
	if ( n > 0 ):
		bounce( n - 1 )
		print abs( n ),
this will (i think) print 0 twice.
 
maybe not , does the indenting imply that the second line is part of the IF ?. python looks weird.
 
I am thinking of switching from C to Python or Matlab for my masters thesis. How do they compare. How steep would you say the learning curve is?
 
The learning curve on Python's pretty straight forward. It's obviously got all the same constructs, but it's laid out a lot more simply. Less worrying about things like pointers, braces and typically confusing syntax.

You should have no problem picking it up.

Matlab on the other hand is bloated, and I personally wouldn't recommend unless you were using arrays heavily. It does some nice things, but it doesn't seem worth it most of the time.

Again, straight-forward to pick up, but I'd recommend Python over it.
 
I like reading code-golfs, as in: shortest programs that accomplish a given challenge.

You can find a lot of cool ones here:

http://stackoverflow.com/questions/tagged/code-golf

(spoiler: J, Golfscript or Perl always win)

For example, the following is a Perl program that can perform Conway's Game of Life:

Code:
$/=pop;@b=split'',<>;map{$n=-1;@b=map{++$n;/
/?$_:($t=grep/X/,@b[map{$n+$_,$n-$_}1,80..82])==3|$t+/X/==3?X:'.'}@b}1..$/;print@b

Seriously Perl, what the fuck?

This one is really nice, though technically not a code-golf:

http://stackoverflow.com/questions/891643/twitter-image-encoding-challenge
 
Back
Top