def solution(msg):
dic = {}
answer = []
for i in range(65, 91) :
dic[chr(i)] = i-64
max_num = 26
while msg :
for i in range(1, len(msg)+1) :
if msg[:i] not in dic :
answer.append(dic[msg[:i-1]])
max_num += 1
dic[msg[:i]] = max_num
msg = msg[i-1:]
break
if msg[:i] == msg :
return answer + [dic[msg]]
๋ฌธ์ ๋๋ก ํ๋ฉด๋จ
from datetime import datetime
import re
def solution(m, musicinfos):
m = re.sub('[A-G]#', lambda x : x.group().lower()[0], m)
broad_list = [re.sub('[A-G]#', lambda x : x.group().lower()[0], info).split(',') for info in musicinfos]
songs = []
music = []
for i, info in enumerate(broad_list) :
start, end, title, melody = info
playtime = (datetime.strptime(end,'%H:%M') - datetime.strptime(start,'%H:%M')).seconds//60
q, r = divmod(playtime, len(melody))
song = melody*q + melody[:r]
if m in song :
songs.append(title)
music.append(len(song))
return songs[music.index(max(music))] if music else "(None)"
def convert(n, base):
T = "0123456789ABCDEF"
q, r = divmod(n, base)
if q == 0:
return T[r]
else:
return convert(q, base) + T[r]
def solution(n = 16, t = 16, m = 2, p = 1):
answer = ''
for i in range(t*m) :
answer += convert(i, n)
return ''.join([answer[m*x+p-1] for x in range(t)])
์ ์ฒด ๋งํด์ผํ ์ซ์์์ ์๊ธฐ์ฐจ๋ก ์ซ์๋ง ๊ณจ๋ผ๋ด๋ฉด๋จ
import numpy as np
def solution(m, n, board):
board_list = np.array([list(map(ord, x)) for x in board])
answer = 0
while True :
zero_list = set()
for h in range(m-1) :
for w in range(n-1) :
if board_list[h, w] == board_list[h, w+1] == board_list[h+1, w] == board_list[h+1, w+1] and board_list[h:h+2, w:w+2].sum() != -4:
zero_list.add((h, w))
zero_list.add((h, w+1))
zero_list.add((h+1, w))
zero_list.add((h+1, w+1))
for h, w in zero_list :
board_list[h, w] = -1
for w in range(n) :
for h in range(m) :
if board_list[h, w] == -1 :
tmp = board_list[:h,w].copy()
board_list[0,w] = -1
board_list[1:h+1,w] = tmp
if answer == len(board_list[board_list == -1]) :
return len(board_list[board_list == -1])
else :
answer = len(board_list[board_list == -1])
def solution(cacheSize, cities):
if cacheSize == 0 : return 5*len(cities)
cities = [x.lower() for x in cities]
LRU = []
cost = 0
for city in cities :
if city in LRU :
LRU.pop(LRU.index(city))
cost += 1
else :
if len(LRU) >= cacheSize:
LRU.pop(0)
cost += 5
LRU.append(city)
return cost
from collections import Counter
def solution(str1, str2) :
set1 = Counter(list(str1[i:i+2].upper() for i in range(len(str1) - 1) if str1[i:i+2].isalpha()))
set2 = Counter(list(str2[i:i+2].upper() for i in range(len(str2) - 1) if str2[i:i+2].isalpha()))
inter = set1 & set2
union = set1 | set2
return 65536 if len(union) == 0 else int((sum(inter.values()) / sum(union.values())) * 65536)
import numpy as np
def solution(arr1, arr2):
return np.dot(arr1, arr2).tolist()
from functools import reduce
def solution(n):
return reduce(lambda x, _ :[x[1], x[0] + x[1]], range(n-1), [0, 1])[1] % 1234567
from functools import reduce
def solution(A, B):
A.sort()
B.sort(reverse = True)
return reduce(lambda x, y : x + y[0]*y[1], zip(A, B), 0)
def solution(s):
arr = sorted(map(int, s.split(' ')))
return '{} {}'.format(arr[0], arr[-1])