Skip to content

Instantly share code, notes, and snippets.

@muik
Created July 21, 2016 16:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save muik/9f0fbe631060c506c6fc8304481af390 to your computer and use it in GitHub Desktop.
Save muik/9f0fbe631060c506c6fc8304481af390 to your computer and use it in GitHub Desktop.
Pingpong with some constraints
# 8퍼센트 개발 면접문제를 풀어보자
# https://brunch.co.kr/@sunghokimnxag/5
#
# 핑퐁 게임 (반드시 Python으로 작성할 것)
# 일련의 숫자가 있고, 이 숫자는 1씩 증가, 또는 감소한다. n번째의 숫자가 있을 시에,
# 이 숫자가 7의 배수(7, 14, 21,...)거나 7이란 숫자를 포함할 시에 (7, 17, 27,...) 방향을 바꾼다.
# 즉, 1, 2, 3, 4, 5, 6, [7], 6, 5, 4, 3, 2, 1, [0], 1, 2, [3], 2, 1, 0, [-1], 0, 1
# 과 같이 숫자는 진행한다. (첫 번째 7은 7번째, 두 번째 0은 14번째, 세 번째 3은 17번째, 네 번째 -1은 21번째)
# 다음의 제약 하에 pingpong(x)의 함수를 작성하라. 예시의 인풋과 아웃풋은 다음과 같다.
# pingpong(8) = 6
# pingpong(22) = 0
# pingpong(68) = 2
# pingpong(100) = 2
# For Loop 또는 Array 를 쓰지 말 것.
# Assignment 를 쓰지 말 것. 즉, 변수 할당을 하지 말 것.
# String 을 쓰지 말 것.
def direction(x, direction):
if str(x).find('7') > -1 or x % 7 == 0:
return direction * -1
return direction
def func(y, x):
""" y = [sum, direction] """
return [y[0]+y[1], direction(x, y[1])]
def pingpong(n):
return reduce(func, xrange(1, n+1), [0,1])[0]
print pingpong(10000)
@kylelee-js
Copy link

string을 쓰지마라는 것은 그냥 스트링 자료형을 사용하지 마라는 뜻인가요 아니면 스트링을 다루는 함수도 사용 못하는 것인가요?
str() 함수가 있어서 여쭤봅니다

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment