목록전체 글 (85)
잘 정리해보자
1. 스택이 비어있을 때 '}'를 만나면 '{'로 바꿔주는 연산을 한다. 2. 주어진 문자열의 모든 문자를 순회한 결과 스택에 '{'가 남아있는 갯수를 세어 반으로 나눈다. 그러면 '{'를 지우기 위해 필요한 '}'의 갯수가 나온다. python num = 1 while True : cnt = 0 line = input().rstrip() if line.startswith('-') : break stack = []; flag = True line_list = list(line) for txt in line_list : if not(stack) and txt == '}' : cnt += 1 stack.append('{') elif txt == '}' : stack.pop() elif txt == '{' :..
pytnon while True : line = input().rstrip() if line == '.' : break stack = []; flag = True line_list = list(line) for txt in line_list : if txt == '(' or txt =='[' : stack.append(txt) elif txt == ']' : if stack and stack[-1] == '[' : stack.pop() else : flag = False break elif txt == ')' : if stack and stack[-1] == '(' : stack.pop() else : flag = False break print('yes' if flag and not(stack) els..
stack : LIFO (Last In First Out) 구조 python T = int(input()) for t in range(T) : line = list(input()) stack = []; flag = True for gual in line : if gual == '(' : stack.append(gual) elif not(stack) and gual == ')' : flag = False break elif gual == ')' : stack.pop() if len(stack) == 0 and flag == True: print('YES') else : print('NO') 문제 : www.acmicpc.net/problem/9012
백준에서 "자주 틀리는 요인"으로 정리해 둔 부분에서 python만 추렸습니다. Python과 Pypy 파이썬 2를 쓰면 안 됩니다. 코드가 틀린 건 아니지만, 언어가 틀렸습니다. BOJ는 numpy 등 외장모듈을 지원하지 않습니다. (사실 모든 언어가 그렇습니다.) 풀이가 분명히 맞고 시간복잡도도 충분히 작은데 시간 초과가 난다면 언어를 Pypy로 설정하고 제출하면 됩니다. 파이썬은 원래 편리성과 속도를 맞바꾼 언어이기 때문에, 맞아야 될 풀이가 시간 초과더라도 이상할 게 전혀 없습니다. 두 수를 입력받고 나서 비교할 때는 반드시 int로 변환을 합시다. 문자열의 비교는 사전 순 비교이기 때문에, 3은 10보다 작지만 "3"은 "10"보다 큽니다. is 키워드는 두 대상의 값이 같은지가 아니라 완전히 ..
1. if __name__ == '__main__' : 로 사용되는 경우 현재 실행되는 부분을 구분할 때 사용 nametest.py def test1() : if __name__ == '__main__' : print('__name__ = __main__') return 'test function' def test2() : print(__name__) return '__name__ test!!' //호출 test1() test2() > nametest.py 안에서 test1호출 한 경우 : 실행결과 : __name__ = __main__ Out[22]: 'test function' > nametest.py 안에서 test2호출 한 경우 : 실행결과 : __main__ Out[23]: '__name__ t..
Promise() : 비동기 통신에 사용되며, 통신 후 callback 들을 받은 후 모든 콜백을 받은 시점을 캐치하는 것. -> 콜백 콜백 콜백 ... 대신 쓸 수 있다. 예제 : function promise1(){ return new Promise(function(resolve,reject){ setTimeout(function(){ console.log('promise1'); resolve(); },100); }); } function promise2(){ return new Promise(function(resolve,reject){ setTimeout(function(){ console.log('promise2'); resolve(); },200); }); } function promise3(..
프로그래머스 연습문제 Hash level1 - 완주하지 못한 선수 python #해시 - 완주하지 못한 선수 #case 1. 효율성out def solution(participant, completion): answer = '' for c in completion : participant[participant.index(c)] = '' answer = ''.join(participant) return answer #case 2. counter 이용 from collections import Counter def solution(participant, completion): answer = '' p = Counter(participant) c = Counter(completion) answer = list..
프로그래머스 연습문제 정렬 level1 - K번째 수 python #level 1 #정렬 - K번째 수 def solution(array, commands): answer = [] for arr in commands : i = arr[0]; j = arr[1]; k = arr[2] li = array[i-1:j] li.sort() answer.append(li[k-1]) return answer programmers.co.kr/learn/courses/30/lessons/42748