# 풀이 (1)
import sys
# money: 준현이와 성민이에게 주어지는 현금
money = int(sys.stdin.readline().strip())
# stocks: 2주간의 주가
stocks = list(map(int, sys.stdin.readline().strip().split()))
# 준현이의 1월 14일 자산 계산
jh_seed = money
jh_stock = 0
for i in range(14):
if jh_seed >= stocks[i]:
buy = jh_seed // stocks[i]
jh_seed -= stocks[i] * buy
jh_stock += buy
if jh_seed == 0:
break
jh_result = jh_seed + (stocks[-1] * jh_stock)
# 성민이의 1월 14일 자산 계산
sm_seed = money
sm_stock = 0
buy_timing = 0
sell_timing = 0
for i in range(1, 14):
if stocks[i] < stocks[i - 1]:
buy_timing += 1
sell_timing = 0
elif stocks[i] > stocks[i - 1]:
sell_timing += 1
buy_timing = 0
if buy_timing == 3:
if sm_seed >= stocks[i]:
buy = sm_seed // stocks[i]
sm_seed -= stocks[i] * buy
sm_stock += buy
buy_timing -= 1
elif sell_timing == 3:
sm_seed += stocks[i] * sm_stock
sm_stock = 0
sell_timing -= 1
sm_result = sm_seed + (stocks[-1] * sm_stock)
# 결과 출력
# print("jh", jh_result)
# print("sm", sm_result)
if jh_result > sm_result:
print("BNP")
elif jh_result < sm_result:
print("TIMING")
else:
print("SAMESAME")
준현이와 성민이의 계산에서 % 연산자를 쓰면 buy 변수를 사용하지 않아도 된다.
추가로 성민이의 계산에서는 비교 연산자 체이닝(Comparison operator chaining)을 쓰면 분기 처리를 간소화할 수 있고, buy_timing이나 sell_timing과 같은 불필요한 변수의 사용을 줄일 수 있다.
참고로 비교 연산자 체이닝은 아래와 같은 규칙을 따른다.
리팩토링한 코드는 다음과 같다.
# 풀이 (2)
import sys
# budget: 준현이와 성민이에게 주어지는 현금
budget = int(sys.stdin.readline().strip())
# prices: 2주간의 주가
prices = list(map(int, sys.stdin.readline().strip().split()))
# 준현이의 1월 14일 자산 계산: BNP
jh_budget = budget
jh_stocks = 0
for i in range(14):
if jh_budget >= prices[i]:
jh_stocks += jh_budget // prices[i]
jh_budget %= prices[i]
jh_total = jh_budget + (jh_stocks * prices[-1])
# 성민이의 1월 14일 자산 계산: TIMING
sm_budget = budget
sm_stocks = 0
for i in range(3, 14):
# 3일 연속 주가가 상승하는 경우(전량 매도)
if prices[i - 3] < prices[i - 2] < prices[i - 1] < prices[i]:
sm_budget += sm_stocks * prices[i]
sm_stocks = 0
# 3일 연속 주가가 하락하는 경우(전량 매수)
elif prices[i - 3] > prices[i - 2] > prices[i - 1] > prices[i]:
sm_stocks += sm_budget // prices[i]
sm_budget %= prices[i]
sm_total = sm_budget + (sm_stocks * prices[-1])
# 결과 출력
# print("jh: ", jh_total)
# print("sm: ", sm_total)
if jh_total > sm_total:
print("BNP")
elif jh_total < sm_total:
print("TIMING")
else:
print("SAMESAME")