[2021 카카오 블라인드] 메뉴 리뉴얼 파이썬 풀이

제니의 블로그·2021년 6월 7일
0

알고리즘풀이

목록 보기
1/3

프로그래머스 문제 링크

무식무식하게 풀어도 된다. (정확성 테스트만 있어서)

python에서 제공되는 갓-Counter와 combinations를 활용했다.
파이썬으로 알고리즘을 풀다보면 기본적으로 제공되는 라이브러리들을 잘 활용하면 얼마나 시간을 애낄 수 있는지 깨닫는다.

from itertools import combinations
from collections import Counter

def solution(orders, course):
    answer = []
    new_orders = []
    for dish in orders:
      new_orders.append(''.join(sorted(dish)))

    for dish_count in course: 
      comb_list = []
      for order in new_orders:
        comb_list = comb_list + list(combinations(order, dish_count))
        
      comb_counter = Counter(comb_list)
      count_val = list(comb_counter.values())
      if len(count_val) > 1 and max(count_val)>1:
        count = count_val.count(max(count_val))
        for c in comb_counter.most_common(n=count):
          answer.append(''.join(list(c[0])))
    
    return sorted(answer)

if __name__ == "__main__":
  print(solution(["ABCFG", "AC", "CDE", "ACDE", "BCFG", "ACDEH"], [2,3,4]))
  print(solution(["ABCDE", "AB", "CD", "ADE", "XYZ", "XYZ", "ACD"], [2,3,5]))
  print(solution(["XYZ", "XWY", "WXA"],[2,3,4]))
profile
기록하는 아주 믖진 습관!

0개의 댓글