Skip to content

Instantly share code, notes, and snippets.

@overnew
Last active September 23, 2024 05:08
Show Gist options
  • Save overnew/e68a388be29c4328910e46ab8683c809 to your computer and use it in GitHub Desktop.
Save overnew/e68a388be29c4328910e46ab8683c809 to your computer and use it in GitHub Desktop.
[PCCP 기출문제] 4번 / 수식 복원하기
explicit_list = []
unknown_list = []
def parse(expressions: []):
for exp in expressions:
arr = exp.split(" ")
if arr[4] == "X":
unknown_list.append(arr)
else:
explicit_list.append(arr)
def find_min_notation():
global explicit_list, unknown_list
max_number = 1
for arr in list(explicit_list):
for idx, element in enumerate(arr):
#print(idx)
if idx == 1 or idx == 3:
continue
#print(element)
for i in range(0, len(element)):
max_number = max(max_number, int(element[i]))
for arr in list(unknown_list):
for idx, element in enumerate(arr):
if idx != 0 and idx != 2:
continue
#print(element)
for i in range(0, len(element)):
max_number = max(max_number, int(element[i]))
return max_number + 1
def calc_possible_notation(min_notation: int):
global explicit_list
possible_notation_list = []
for notation in range (min_notation, 10):
break_flag = False
for arr in explicit_list:
if notation_calc(arr[0], arr[2], arr[1], notation) != int(arr[4],notation):
break_flag = True
break
if not break_flag:
possible_notation_list.append(notation)
return possible_notation_list
# c가 4자리가 되면 불가?
def notation_calc(a: str, b: str, symbol: str, notation: int):
a = int(a,notation)
b = int(b,notation)
c = 0
if symbol == "+":
c = a + b
else:
c = a - b
return c
def make_return(min_notation: int, possible_notation_list):
global unknown_list
if len(possible_notation_list) == 1: #정답이 한 개인 경우
notation = possible_notation_list[0]
for arr in unknown_list:
answer_decimal = notation_calc(arr[0], arr[2], arr[1], notation)
#print("as:" + str(answer_decimal))
arr[4] = ten2n(answer_decimal, notation)
else: #정답을 모르는 경우
notation = min_notation
for arr in unknown_list:
answer_decimal = notation_calc(arr[0], arr[2], arr[1], notation)
ten_sum = int(arr[0]) + int(arr[2])
if arr[1] == "-":
ten_sum = int(arr[0]) - int(arr[2])
if ten_sum == int(ten2n(answer_decimal,notation)):
arr[4] = str(ten2n(answer_decimal,notation))
else:
arr[4] = "?"
answer = []
for arr in unknown_list:
temp = ""
end = len(arr) -1
for i, ele in enumerate(arr):
temp += ele
if i < end:
temp +=" "
answer.append(temp)
return answer
def ten2n(number: int, notation: int):
answer = ""
n = number
while n > 0:
mod = n % notation
n = int(n / notation)
answer += str(mod)
if answer == "":
answer = "0"
return answer[::-1]
def solution(expressions):
parse(expressions)
min_notation = find_min_notation()
#print(min_notation)
possible_notation_list = calc_possible_notation(min_notation)
#print(possible_notation_list)
return make_return(min_notation,possible_notation_list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment