-
-
Save jwchang0206/6790129 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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