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
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
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
.
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);
}
}
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);
}
void print_recursive(int n) {
printf("%d\n", n);
if (n < 10)
print_recursive(n+1);
printf("%d\n", n);
}
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;
}
Javascript version:
printNumber(1);
function printNumber(num){
document.write(num);
if (num < 10)
printNumber(num + 1);
document.write(num);
}
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 :-) )
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.