3

I have a small problem with my code. I can't seem to figure out how to do this. I can get it to work with two for loops. But in the exercise it says that i only can use one loop to get the result. The code is supposed to execute this:

bounce2(4):

  • 4
  • 3
  • 2
  • 1
  • 0
  • 1
  • 2
  • 3
  • 4

What I have come up with:

def bounce2(n):
    for x in range(n,-1,-1):
    print(x)

Which prints out 4,3,2,1,0 But now i dont know what to do.. I have tried different if statements such as:

def bounce2(n):
   for x in range(n,-1,-1):
   print(x)
   if n == 0:
   x = x + 1
   print(x)

But they only print one integer because they are out of the loop. Same thing goes if i try to make the if-statement inside the loop, then it prints out something like 433221100. I dont know how to get the numbers to switch places. The print statement should also be an integer and not a string. So i can't use replaced.

Really need help to figure out the logic. All help is appreciated.

teslagosu
  • 65
  • 5

5 Answers5

3

So, a little bit of my thought process before showing you the code. Clearly there are nine lines, or more generally n * 2 + 1 lines. Because we need to count down to 0 and back up. That's how many times you need to call print.

Now, if you add line numbers to the expected output and think of it as a table describing a function f(i, n) where i is the line number, and n is the starting and ending value. what is f? Can you write down the formula? e.g.

i f(i, 4)
0  4
1  3
2  2
3  1
4  0
5  1
6  2
7  3
8  4

We can write down the basic structure of the code, we still don't know what f look like but assume we have it:

for i in range(2*n+1):
    print f(i)

And, what is f? Now you need to be a little creative and maybe experiment a bit. What I did was to try basic arithmetic combinations of i and n to match f(i, n), and I quickly noticed that n - i works until we reach the second half of the output, which only differs by a - sign.

i f(i, 4) n - i
0  4       4
1  3       3
2  2       2
3  1       1
4  0       0
5  1       -1
6  2       -2
7  3       -3
8  4       -4

Soooo, take the absolute value of n - i or i - n, whatever.

def f(i, n):
    return abs(n-i)
xiaofeng.li
  • 8,237
  • 2
  • 23
  • 30
2

Here is what I believe to be a pretty elegant solution:

def bounce(n):
    for x in range(-n, n+1):
        print(abs(x))

Our loop goes from the negative of n to the positive of n, printing the absolute value.

Blake Lockley
  • 2,931
  • 1
  • 17
  • 30
  • Yeah was very east and neat. Thank you for the solution. I tried earlier: for x in range(n,-n-1,-1): print(abs(x)) but didnt know I could use the abs() function to get the negative integers to turn positive :) – teslagosu Sep 14 '18 at 22:18
1

Since you need to count n times downwards, and another n times upwards, and 1 comes from counting 0, instead of actually counting downwards and then upwards in two separate loops, we can use one loop to count upwards 2 * n + 1 times, which effectively is like counting towards n and then bouncing off n, so we can simply calculate the "distance" to n instead, which is the absolute value of n - x:

def bounce2(n):
    for x in range(2 * n + 1):
        print(abs(n - x))

so that bounce2(4) would output:

4
3
2
1
0
1
2
3
4
blhsing
  • 91,368
  • 6
  • 71
  • 106
  • Thank you for your answer. Im still a little confused how u came up with 2 * n + 1. I know that it prints out 0,1,2,3,4,5,6,7,8 if u dont use the abs(n-x)) and only print out the x. How do you know it should be abs(n - x))? – teslagosu Sep 14 '18 at 01:23
  • 1
    `2 * n` comes from the fact that you need to count `n` times downwards, and another `n` times upwards, and `1` comes from counting `0`. So instead of actually counting downwards and then upwards in two separate loops, we can use one loop to count upwards `2 * n + 1` times, which effectively is like counting towards `n` and then bouncing off `n`, so we can simply calculate the "distance" to `n` instead, which `abs(n - x)` returns. – blhsing Sep 14 '18 at 01:32
1

a very simple solution will be:

for i in range(n, -(n+1), -1):
    print(abs(i))

this like mirroring numbers around some point.
in your case that point is zero and to have identical mirroring use abs

m9mhmdy
  • 56
  • 3
0

Try the below, have a list l with a element of str(n) iterate trough the range of n times 2, then check x is bigger than n+2 if it is add 1 to n, otherwise subtract 1 from n, both cases append to l, then at the end, do str.join to join '\n' (newline) with l:

def bounce2(n):
   l=[str(n)]
   for x in range(n*2):
      if x>n+2:
         n+=1
         l.append(str(n))         
      else:
         n-=1
         l.append(str(n))
   return '\n'.join(l)
print(bounce2(4))

Output:

4
3
2
1
0
1
2
3
4
U13-Forward
  • 69,221
  • 14
  • 89
  • 114