기타/Codility

[Easy] Brackets

백곰곰 2022. 5. 25. 21:21
728x90
반응형

[문제]

괄호 검사

 

Brackets coding task - Learn to Code - Codility

Determine whether a given string of parentheses (multiple types) is properly nested.

app.codility.com

[코드]

List를 Stack 처럼 사용하여 풀이

# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")

def solution(S):
    # write your code in Python 3.6
    stackS = []
    for i in S :
        if not stackS :
            stackS.append(i)
        elif stackS[-1]=='{' and i == '}' :
            stackS.pop()
        elif stackS[-1] == '(' and i == ')' :
            stackS.pop()
        elif stackS[-1] == '[' and i == ']' :
            stackS.pop()
        else :
            stackS.append(i)
    
    if not stackS :
        return 1
    else :
        return 0
728x90

'기타 > Codility' 카테고리의 다른 글

[Easy] Dominator  (0) 2022.05.26
[Easy] Fish  (0) 2022.05.26
[Easy] Distinct  (0) 2022.05.25
[Easy] PassingCars  (0) 2022.05.24
[Easy] FrogJmp  (0) 2022.05.22