views:

407

answers:

8

I am preparing for my interview tomorrow -- I need the answer to this question:

How can you print 1 to 10 & 10 to 1 by using recursion with single variable

+7  A: 

You should be able to figure this out yourself.

Hint: Make a method that takes a 10 as a parameter, then prints the parameter and calls itself with 9.

SLaks
+6  A: 

I'm going to get downvoted I just know it but here is (a) solution. Not the best one but you should be able to make it better yourself.

class Program
{
    static void Main(string[] args)
    {
        printNumDown(10);
    }

    static void printNumDown(int num)
    {
        Console.WriteLine(num.ToString());
        if (num > 1)
            printNumDown(num - 1);
        else
            printNumUp(num + 1);
    }

    static void printNumUp(int num)
    {
        Console.WriteLine(num.ToString());
        if (num < 10)
            printNumUp(num + 1);
    }
}
griegs
"I need the answer to this question". Not "I need help with this" but very specifically "I need the answer". So you shouldn't be downvoted in my opinion.
paxdiablo
+3  A: 

Here's a sneaky way:

#include <stdio.h>

static void recur_up (int n) {
    if (n > 1)
        recur_up (n - 1);
    printf ("%d\n", n);
}

static void recur_down (int n) {
    printf ("%d\n", n);
    if (n > 1)
        recur_down (n - 1);
}

int main (void) {
    recur_up (10);
    recur_down (10);
    return 0;
}

which generates:

1
2
3
4
5
6
7
8
9
10
10
9
8
7
6
5
4
3
2
1

It would have been a lot more elegant going down then up since you could do that with a single function:

static void recur_both (int n) {
    printf ("%d\n", n);
    if (n > 1)
        recur_down (n - 1);
    printf ("%d\n", n);
}
paxdiablo
well its still possible to do it with a single function
Phong
Why, yes it is, at the cost of hardcoding the upper limit (not _overly_ flexible). But the specs didn't call for a single function, just recursion and a single variable :-)
paxdiablo
+11  A: 
void print_recursive(int n) { 
    printf("%d\n", n);
    if (n < 10)
        print_recursive(n+1);
    printf("%d\n", n);
}
Jerry Coffin
+7  A: 

With one function and one variable only:

void recurs(int num) {
    printf("%d\n", num);
    if (num < 10) {
        recurs(num + 1);
    }
    printf("%d\n", num);
}

int main() {
    recurs(1);
    return 0;
}
Phong
Unless I'm completely blind, this doesn't count down again.
Alastair Pitts
Note that he has printf() twice. It is using the symmetry of counting up and down again so only needs to loop 1 - 10;
David
@Alastair You could just try running it. The concept does work, although he used `%n` instead of `%d` and forgot the newline; Jerry [got it right](http://stackoverflow.com/questions/2879496/how-can-you-print-1-to-10-10-to-1-by-using-recursion-with-single-variable/2879619#2879619)
Michael Mrozek
Ooooo, I see now. Very nice. My apologies for the lack of faith :)
Alastair Pitts
@Michael: Thanks for noticing the problem in the printf. It is now corrected.
Phong
+2  A: 

Javascript version:

printNumber(1);

function printNumber(num){
  document.write(num);
  if (num < 10) 
     printNumber(num + 1);
  document.write(num);
}
Jason
code golfed: `f(1);function f(x) {d=document;d.write(x);if(x<10)f(x+1);d.write(x);}`
Jason
A: 

Here's one in Ruby:

puts (r = ->n=1 { if n<=10 then [n] + r.(n+1) + [n] else [] end }).()

Note: before you conclude that Ruby is an unreadable mess even worse than Perl, let me assure you: this is not idiomatic Ruby. Idiomatic Ruby would be more like

def recursive_count_up_and_down(n=1)
  return [] unless n<=10
  [n] +
  recursive_count_up_and_down(n + 1) +
  [n]
end

puts recursive_count_up_and_down

Of course, Ruby is an imperative language, so doing it really idiomatically would not use recursion:

1.upto(10) do |i| puts i end
10.downto(1) do |i| puts i end

Here's another neat one that unfortunately doesn't use recursion, either:

puts Array.new(20) {|i| if i < 10 then i+1 else 20-i end }

BTW: all the solutions so far, including mine, are actually cheating, because technically they use two variables, because

function foo {}

is equivalent to

var foo = λ{}

So, in my example above, there are two variables: recursive_count_up_and_down and n. We could eliminate both of those by writing in a tacit point-free style (for example in the SK-calculus), but I'll leave that as an exercise to the reader. (Meaning I can't figure it out :-) )

Jörg W Mittag
+1  A: 

Why are you guys all being so difficult? In Pseudocode:

function recurfunc(n) {
    if (n < 10) {
    echo (-1 * (floor(abs(n)) - 10));
        recurfunc(n+1);
    }
}

Then call recurfunc with -9.5 as its start.

Seems kind of obvious to me that the answer is using absolute value.

Lajla