https://programmers.co.kr/learn/courses/30/lessons/64065
from collections import defaultdict
def solution(s):
answer = list()
set_map = defaultdict(set)
sets = s.split('}')[:-2]
for s in sets:
new_set = set()
new_num = ''
for l in s:
if l.isnumeric():
new_num += l
elif new_num:
new_set.add(int(new_num))
new_num = ''
if new_num:
new_set.add(int(new_num))
set_map[len(new_set)] = new_set
for i in range(1, len(set_map) + 1):
answer.append((set_map[i] - set_map[i - 1]).pop())
return answer
def solution(s):
answer = []
list_ = s[2:-2].split('},{')
for i,j in enumerate(list_):
list_[i] = j.split(',')
list_[i] = set(list_[i])
list_.sort(key=len)
for i in range(len(list_)-1):
for j in range(i+1):
list_[i+1] = list_[i+1] - list_[j]
for i in range(len(list_)):
a = list(list_[i])
answer.append(int(a[0]))
return answer
split('},{')
를 사용하여 괄호 벗겨줌sort(key=len)
으로 key 함수를 주어 정렬import re
import heapq as hq
def solution(s):
answer = []
setlist = []
numset = set()
for i in re.findall('{[\d,]*}',s):
x=eval(i)
hq.heappush(setlist,(len(x), x))
while setlist:
count, nowset = hq.heappop(setlist)
nownum = (nowset - numset).pop()
numset.add(nownum)
answer.append(nownum)
return answer
eval
함수로 문자열 형태에서 집합으로 변환해줌import re
from collections import Counter
def solution(s):
s = Counter(re.findall('\d+', s))
return list(map(lambda x: int(x[0]), s.most_common()))