https://school.programmers.co.kr/learn/courses/30/lessons/72411
# 메뉴 리뉴얼
from itertools import combinations
def solution(orders, course):
answer = [] # 최종 결과
tmp=set([]) # 문자열로 변환하기 전 결과
for c in course: # course를 돌면서(조합할 개수를 설정해줌)
res=dict() # (문자열:등장한 개수)로 저장됨
for o in orders:
for comb in combinations(o, c): # 현재 order에서 c만큼을 선택하는 조합 구하기
comb=tuple(sorted(comb))
# 원소에 저장된 문자열 또한 알파벳 오름차순임
# 먼저 정렬을 해준 뒤 구해주어야 올바른 결과가 나온다
# 예를들면 "XY" "YX"가 있다고 했을 때 오름차순을 해주지 않으면 다르다고 인식
if comb not in res: # 만약 dictionary에 아직 없고 처음 나온 문자열 조합이라면
res[comb]=1 # 개수를 1로 지정해주고 추가
else:# 이미 있다면
res[comb]+=1 # 개수 1 늘려줌
if len(res)!=0: # 아예 없을 수도 있음-> 없으면 max 안구해지고 오류 남
m=max(res.values()) # 가장 많이 나온 횟수를 저장하고
if m<2: # 최소 2명 이상 주문했어야 하므로 주문 횟수의 최댓값이 2 이하이면 넘어가기
continue
# 만약 2 이상이라면
for key, value in res.items(): # dictionary를 돌면서
if value==m: # 최댓값과 같은 횟수가 더 있는지 확인
tmp.add(key) # 있다면 추가
for t in tmp: # 저장된 결과를 문자열로 변환해서 저장
answer.append("".join(t))
answer.sort() # 결과 알파벳순 정렬
return answer
import collections
import itertools
def solution(orders, course):
result = []
for course_size in course:
order_combinations = []
for order in orders:
order_combinations += itertools.combinations(sorted(order), course_size)
most_ordered = collections.Counter(order_combinations).most_common()
result += [ k for k, v in most_ordered if v > 1 and v == most_ordered[0][1] ]
return [ ''.join(v) for v in sorted(result) ]
[(('X', 'Y'), 2), (('W', 'X'), 2), (('X', 'Z'), 1), (('Y', 'Z'), 1), (('W', 'Y'), 1), (('A', 'W'), 1), (('A', 'X'), 1)]
이런식으로 값과 나온 횟수가 함께 저장되어있다 -> 내림차순