본문 바로가기

PROGRAMMERS26

[프로그래머스/파이썬] 방문 길이 내 풀이def solution(dirs): answer = 0 cur_location = (0,0) # 현재 위치 path = set() # 겹치는 것은 제외 # 현재 위치 & 방향 -> 다음 위치 def move(current, direction): x, y = current if direction == "U": y += 1 elif direction == "D": y -= 1 elif direction == "R": x += 1 elif direction == "L": x -= 1 if not -5  메모set에는.. 2025. 1. 16.
[프로그래머스/파이썬] 롤케이크 자르기 내 풀이def solution(topping): answer = 0 young_bro = set() # 종류만 확인 # 철수한테 모든 토핑이 있다는 가정, 종류별 개수 딕셔너리 생성 # 해당 부분은 from collections import Counter로 쉽게 대체 가능 old_bro = {} for _topping in topping: if _topping in old_bro.keys(): old_bro[_topping] += 1 else: old_bro[_topping] = 1 for _topping in topping: young_bro.add(_topp.. 2025. 1. 15.
[프로그래머스/파이썬] 튜플 내 풀이def solution(s): answer = [] # 숫자만 남김 > 콤마 기준 스플릿 > 정수 변환 > 리스트로 반환 def str_to_set(x): return list(map(int, x.lstrip("{").rstrip("}").split(","))) # {} 단위로 스플릿 하기 위해 '},{'를 기준으로 스플릿 후 위 함수 적용 ss = list(map(str_to_set, s[1:-1].split('},{'))) # 길이가 작은 순서대로 추가해주면 원래 순서를 복원할 수 있음 for _ss in sorted(ss, key=lambda x: len(x)): # 어차피 중복은 없으므로, set으.. 2025. 1. 14.
[프로그래머스/파이썬] [1차] 캐시 내 풀이from collections import dequedef solution(cacheSize, cities): # 변수 초기화 answer = 0 cache = deque(maxlen=cacheSize) # deque 최대 크기 설정 # 대소문자 구분 없으므로 전체 lower case 변환 def lower(x): return x.lower() cities = list(map(lower, cities)) for city in cities: # cache에 있을 경우 if city in cache: answer += 1 cache.remove(city) .. 2025. 1. 13.
[프로그래머스/파이썬] [PCCP 기출문제] 1번 / 동영상 재생기 내 풀이def solution(video_len, pos, op_start, op_end, commands): answer = '' # 시간 str을 모두 초단위로 변환하여 계산하는 함수 def time_to_sec(*time_string): sec_list = [] for _t in time_string: mm, ss = map(int, _t.split(":")) ss += mm * 60 sec_list.append(ss) return sec_list # 최종적으로 계산된 초를 시간 단위로 변환하는 함수 def sec_to_time(second): mm = s.. 2025. 1. 10.
[프로그래머스/파이썬] [PCCE 기출문제] 10번 / 공원 내 풀이def solution(mats, park): answer = -1 # 놓을 곳이 없을 경우 반환할 값으로 초기화 found = False # 찾지 못했을 경우로 초기화 # 큰 돗자리부터 시작 for mat in sorted(mats, reverse=True): # 돗자리가 공원을 넘어가지 않도록 범위 설정 for i in range(len(park)-mat+1): for j in range(len(park[0])-mat+1): # 공원의 i, j가 이미 점유되었을 경우 더이상 탐색하지 않고 다음으로 넘어감 if park[i][j] != "-1": .. 2025. 1. 9.