Edit responsibly!

1
2
3
4
5
n = 20
i = 0
while i < n:
   print "-",
   i -= 1

In how many ways can you make the above program output - exactly 20 times by adding/changing at most one character?

  • Changing a space to something else is considered equivalent to adding a character.


The answer is 5.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

1 solution

  1. Change i to -i in line 3
1
2
3
4
5
n = 20
i = 0
while -i < n:
   print "-",
   i -= 1

  1. Change i to n in line 5
1
2
3
4
5
n = 20
i = 0
while i < n:
   print "-",
   n -= 1

  1. Change < to + in line 3
1
2
3
4
5
n = 20
i = 0
while i + n:
   print "-",
   i -= 1

  1. Change 1 to -1 on line 5 (due to Ivan)
1
2
3
4
5
n = 20
i = 0
while i < n:
   print "-",
   i -= -1

  1. Change -= to += on line 5 (due to Samarth)
1
2
3
4
5
n = 20
i = 0
while i < n:
   print "-",
   i += 1

In case you find any more ways let me know!

Source

Changing - sign to + sign in line 5

Samarth Agarwal - 5 years, 4 months ago

Log in to reply

Okay, updated. This is escalating too fast!

Agnishom Chattopadhyay - 5 years, 4 months ago

Log in to reply

That's exactly why you should verify this problem.

Ivan Koswara - 5 years, 4 months ago

Oh, I thought that was listed in one of the three initial solutions. Must have misread.

Ivan Koswara - 5 years, 4 months ago

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...