백준 9814 Coding of Permutations (Python, Pypy)

Joowan Park·2023년 9월 26일
0

코딩

목록 보기
28/28

대충, 이렇게 이루어진 트리가 있을때 주어진 튜플이 몇번째 튜플인지 맞춰보세요

라는 문제입니다.

아이디어는 굉장히 간단한데 중간 과정에서 제가 현명하지 못하게 코드를 짠 것 같습니다.

import math
N = []
graph = []
while True:
    a = input()
    if a == "-1":
        break
    else:
        x = list(a)
        B = []
        y = [False for _ in range (0,len(x))]
        
        for i in range (0,len(x)):
            if x[i] == "(" or x[i] == ")" or x[i] == ",":
                continue
            else:
                if y[i] == False:
                    c = []
                    j = i
                    while x[j] != "(" and x[j] != ")" and x[j] != ",":
                        c.append(x[j])
                        j += 1
                        y[j] = True
                    s = "".join(str(t) for t in c)
                    B.append(s)
                else:
                    continue
        n = int(B[0])
        route = B
        route.remove(route[0])
        N.append(n)
        graph.append(route)
A = []
for i in range (0,len(N)):
    num = []
    summary = 0
    for j in range (0,N[i]):
        num.append(j+1)
    for k in range (1,N[i]+1):
        summary += math.factorial(N[i] - k) * (num.index(int(graph[i][k-1])))
        num.remove(int(graph[i][k-1]))
    A.append(summary + 1)

s = ",".join(str(t) for t in A)
print(s)

소스코드가 조금 더럽네요.

솔직히 다른것보다 주어진 튜플에서 숫자 추출해내는게 제일 어려웠습니다 ...

결국 False로 이루어진 리스트 생성해서 중복체킹 안되게끔 ....

수많은 밸류 에러들을 뚫고 한문제 다시 치워버렸습니다

profile
Complex Dynamics에서 탈출한 원숭이

0개의 댓글