이미 만들어져있는 아이디가 주어지고, 신규 고객이 아이디를 만들려할 때 이미 있는 아이디면 숫자를 하나씩 더 붙여 반환.
def solution(registered_list, new_id):
if new_id not in registered_list:
return new_id
registered_list = set(registered_list)
s_part, n_part = "", ""
for i in range(len(new_id)):
if not new_id[i].isnumeric():
s_part += new_id[i]
else:
n_part += new_id[i]
next_num = 1
if n_part != "":
next_num = int(n_part) + 1
for num in range(next_num, 1000000):
if s_part + str(num) not in registered_list:
return s_part + str(num)
print(solution(["card", "ace13", "ace16", "banker", "ace17", "ace14"], "ace15") =="ace15")
print(solution(["cow", "cow1", "cow2", "cow3", "cow4", "cow9", "cow8", "cow7", "cow6", "cow5"], "cow") == "cow10")
print(solution(["bird99", "bird98", "bird101", "gotoxy"], "bird98") == "bird100")
print(solution(["apple1", "orange", "banana3"], "apple") =="apple")
처음에 리스트에서 in을 써서 시간초과났다. set으로 바꾸니 통과
한달동안 휴일이 주어지고, 연휴를 쓸 수 있는 날이 주어질 때, 가장 길게 쉬면 며칠인지 찾는 문제.
from itertools import combinations
import re
from collections import Counter
import copy
longest_day = 0
def count_longest(possible_combs, days):
global longest_day
days_copy = copy.deepcopy(days)
for holiday in possible_combs:
days_copy[holiday] = "HOL"
cur_max = 0
tmp_max = 0
for i in range(30):
if days_copy[i] in ["SAT", "SUN", "HOL"]:
tmp_max += 1
else:
cur_max = max(cur_max, tmp_max)
tmp_max = 0
cur_max = max(cur_max, tmp_max)
longest_day = max(longest_day, cur_max)
def solution(leave, day, holidays):
global longest_day
day_dict = {"MON": 0, "TUE" : 1, "WED" : 2, "THU" : 3, "FRI" : 4, "SAT" : 5, "SUN" : 6}
day_list = ["MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"]
cur_day_index = day_dict[day]
days = [""] * 30
for i in range(30):
days[i] = day_list[cur_day_index]
cur_day_index = (cur_day_index + 1) % 7
for holiday in holidays:
holiday -= 1
days[holiday] = "HOL"
work_days = []
for i in range(30):
if days[i] not in ["SAT", "SUN", "HOL"]:
work_days.append(i)
if len(work_days) <= leave:
return 30
for possible_combs in list(combinations(work_days, leave)):
count_longest(possible_combs, days)
return longest_day
#print(solution(4, "FRI", [6, 21, 23, 27, 28]) == 10)
#print(solution(3, "SUN", [2, 6, 17, 29]) == 8)
print(solution(30, "MON", [1, 2, 3, 4, 28, 29, 30]) == 30)
격자 무니 게임판에서 위에서 아이템이 떨어지고, 3개 이상 뭉쳐있으면 터져서 없어지고 위에것들이 내려오는 게임 구현
from collections import defaultdict, deque
from itertools import combinations
import re
from collections import Counter
import copy
dx = [-1, 0, 0, 1]
dy = [0, -1, 1, 0]
def print_board(board):
for i in range(len(board)):
for j in range(len(board[0])):
print(board[i][j], end=" ")
print()
def bfs(visited, board, i ,j):
visited[i][j] = True
queue = deque()
queue.append([i, j])
tmp_visited_list = []
while queue:
y, x = queue.popleft()
tmp_visited_list.append([y, x])
for k in range(4):
ny, nx = y + dy[k], x + dx[k]
if 0 <= ny < len(board) and 0 <= nx < len(board[0]):
if not visited[ny][nx] and board[ny][nx] == board[i][j]:
visited[ny][nx] = True
queue.append([ny, nx])
return tmp_visited_list
def bomb_board(board, stacks):
visited = [[False] * len(board[0]) for _ in range(len(board))]
for i in range(len(board)):
for j in range(len(board[0])):
if not visited[i][j] and board[i][j] != 0:
tmp_visited_list = bfs(visited, board, i, j)
if len(tmp_visited_list) >= 3:
for y, x in tmp_visited_list:
board[y][x] = -1
def change_board(board, stacks):
board = [[0] * 6 for _ in range(6)]
for j in range(6):
i = 5
stack_copy = copy.deepcopy(stacks[j])
while stack_copy:
board[i][j] = stack_copy.pop(0)
i -= 1
bomb_board(board, stacks)
return board
def change_stack(board):
stacks = [[] for _ in range(6)]
for j in range(6):
for i in range(5, -1, -1):
if board[i][j] not in [0, -1]:
stacks[j].append(board[i][j])
return stacks
def is_bombed(board):
for i in range(len(board)):
for j in range(len(board[0])):
if board[i][j] == -1:
return True
return False
def solution(macaron):
answer = []
board = [[0] * 6 for _ in range(6)]
stacks = [[] for _ in range(6)]
bombed = False
for mac in macaron: # [떨어뜨린 위치, 마카롱의 색]
pos, color = mac[0] -1, mac[1]
stacks[pos].append(color)
board= change_board(board, stacks)
bombed = is_bombed(board)
stacks = change_stack(board)
while bombed:
board = change_board(board, stacks)
stacks = change_stack(board)
bombed = is_bombed(board)
board = change_board(board, stacks)
for i in range(6):
answer.append(''.join(map(str, board[i])))
return answer
print(solution([[1,1],[2,1],[1,2],[3,3],[6,4],[3,1],[3,3],[3,3],[3,4],[2,1]]) == ["000000","000000","000000","000000","000000","204004"])
print(solution([[1,1],[1,2],[1,4],[2,1],[2,2],[2,3],[3,4],[3,1],[3,2],[3,3],[3,4],[4,4],[4,3],[5,4],[6,1]]) == ["000000","000000","000000","000000","000000","404001"])
SELECT DISTINCT(subway_stations.id) AS "ID", subway_stations.name as "NAME"
FROM subway_stations left join line_routes on subway_stations.id = line_routes.station_id
WHERE subway_stations.id not in (SELECT station_id FROM line_routes WHERE line_color = "Red" or line_color = "Green")
ORDER BY subway_stations.id