Skip to content

Instantly share code, notes, and snippets.

@simnalamburt

simnalamburt/run

Last active Feb 20, 2019
Embed
What would you like to do?
#!/usr/bin/env python3
# 심심해서 8percent 면접문제 풀어봄. 푸는데에 20분 걸림.
#
# Reference:
# https://web.archive.org/web/20170226231428/https://brunch.co.kr/@sunghokimnxag/5
def pingpong(target: int) -> int:
return pingpong_internal(target, 1, 1, True)
def pingpong_internal(target: int, n: int, state: int, is_plus_direction: bool) -> int:
if n == target:
return state
return (lambda new_direction: pingpong_internal(
target,
n + 1,
state + (1 if new_direction else -1),
new_direction,
))(
not is_plus_direction
if n % 7 == 0 or check_for_7(n) else
is_plus_direction
)
def check_for_7(number: int) -> bool:
if number == 0:
return False
if number % 10 == 7:
return True
return check_for_7(number // 10)
assert check_for_7(17)
assert check_for_7(1710)
assert not check_for_7(16)
assert not check_for_7(1610)
assert pingpong(1) == 1
assert pingpong(2) == 2
assert pingpong(3) == 3
assert pingpong(4) == 4
assert pingpong(5) == 5
assert pingpong(6) == 6
assert pingpong(7) == 7
assert pingpong(8) == 6
assert pingpong(9) == 5
assert pingpong(10) == 4
assert pingpong(11) == 3
assert pingpong(12) == 2
assert pingpong(13) == 1
assert pingpong(14) == 0
assert pingpong(15) == 1
assert pingpong(16) == 2
assert pingpong(17) == 3
assert pingpong(18) == 2
assert pingpong(19) == 1
assert pingpong(20) == 0
assert pingpong(21) == -1
assert pingpong(22) == 0
assert pingpong(23) == 1
assert pingpong(8) == 6
assert pingpong(22) == 0
assert pingpong(68) == 2
assert pingpong(100) == 2
test = lambda x: print(f'pingpong({x}) == {pingpong(x)}')
for x in range(1, 24):
test(x)
print()
for x in [8, 22, 68, 100]:
test(x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment