본문 바로가기

Python265

[프로그래머스 Lv.3] 여행경로 def solution(tickets): answer = [] graph = {} for i in range(len(tickets)): if tickets[i][0] not in graph: graph[tickets[i][0]] = [tickets[i][1]] else: graph[tickets[i][0]].append(tickets[i][1]) graph[tickets[i][0]].sort(reverse=True) print(graph) stack = ["ICN"] while stack: top = stack[-1] if top not in graph or not graph[top]: print("stack pop : ",stack[-1]) answer.append(stack.pop()) else: p.. 2023. 7. 30.
[Python] replace() / strip(), lstrip(), rstrip() 함수 정리 .replace() replace()는 문자열을 변경하는 함수이다. 문자열 안에서 특정 문자를 새로운 문자로 변경하는 기능을 가지고 있다. 사용 방법은 '변수. replace(old, new, [count])' 형식으로 사용한다. - old : 현재 문자열에서 변경하고 싶은 문자 - new: 새로 바꿀 문자 - count: 변경할 횟수. 횟수는 입력하지 않으면 old의 문자열 전체를 변경한다. 기본값은 전체를 의미하는 count=-1로 지정되어있다. .strip(), .lstrip(), rstrip() strip()을 이용하면 문자열에서 특정 문자를 제거할 수 있다. Python의 String은 다음 함수를 제공한다. - strip([chars]) : 인자로 전달된 문자를 String의 왼쪽과 오른쪽에서.. 2023. 7. 22.
[프로그래머스] 문자열 나누기 def solution(s): answer = [] string = s while(string != ''): x = string[0] list_str = list(string) count_x = 0 count_not_x = 0 index = 0 for i,value in enumerate(list_str): if value == x: count_x += 1 else: count_not_x += 1 if count_x != 0 and count_x == count_not_x: index = i break index = i answer.append(list_str[:index+1]) string = ''.join(list_str[index+1:]) return len(answer) 이번 문제는 주어진 조건대.. 2023. 7. 21.
[프로그래머스] 옹알이(1) def solution(babbling): answer = 0 available = ('aya','ye','woo','ma') available_prefix = ('a','y','w','m') def checkFunc(arr): arr_length = len(arr) i = 0 while(i 2023. 7. 18.
[프로그래머스] 바탕화면 정리 def solution(wallpaper): left_top = [49,49] right_down = [0,0] row_l, col_l = len(wallpaper), len(wallpaper[0]) area = [[""]*col_l for _ in range(row_l)] for i,row in enumerate(wallpaper): for j,item in enumerate(row): area[i][j] = item if item == "#": left_top[0] = min(left_top[0],i) left_top[1] = min(left_top[1],j) right_down[0] = max(right_down[0],i+1) right_down[1] = max(right_down[1],j+1) r.. 2023. 6. 23.
[프로그래머스] 공원 산책 def solution(park, routes): row_l = len(park) col_l = len(park[0]) start_location = (0,0) route_list = [] area = [[""]*col_l for _ in range(row_l)] for i,row in enumerate(park): for j,item in enumerate(row): area[i][j] = item if item == "S": start_location = [i,j] for i in routes: route_list.append((i[0],int(i[2]))) for i in route_list: d, n = i[0], i[1] exist_x = False if d == "N": if (start_lo.. 2023. 6. 23.