Skip to content

Instantly share code, notes, and snippets.

@jwchang0206
Forked from wfwef/gist:6789848
Last active December 24, 2015 11:19
Show Gist options
  • Save jwchang0206/6790129 to your computer and use it in GitHub Desktop.
Save jwchang0206/6790129 to your computer and use it in GitHub Desktop.
# Q3.
"1 2 3 4 5 6 [7] 6 5 4 3 2 1 [0] 1 2 [3] 2 1 0 [-1] 0 1 2 3 4 [5] [4] 5 6"
def pingpong(n):
"""Return the nth element of the ping-pong sequence.
>>> pingpong(7)
7
>>> pingpong(8)
6
>>> pingpong(15)
1
>>> pingpong(21)
-1
>>> pingpong(22)
0
>>> pingpong(30)
6
>>> pingpong(68)
2
>>> pingpong(69)
1
>>> pingpong(70)
0
>>> pingpong(71)
1
>>> pingpong(72)
0
>>> pingpong(100)
2
"""
"*** YOUR CODE HERE ***"
def goDirection(value, end, order, nth):
if nth == end:
return value
else:
if (nth + 1) % 7 == 0 or (value + order) % 7 == 0:
return goDirection(value + order, end, -order, nth + 1)
else:
return goDirection(value + order, end, order, nth + 1)
return goDirection(0, n, 1, 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment