TIL(24.06.15.)

codataffee·2024년 6월 15일

TIL

목록 보기
68/135
post-thumbnail

#INTRO

취미 : 카페 가기

토요일 좋다.


#코드카타

  • KATA #63

  • PYTHON

    • 경우의 수 계산

      # 테스트 데이터
      clothes = [
      ["yellow_hat", "headgear"], 
      ["blue_sunglasses", "eyewear"], 
      ["green_turban", "headgear"]
      ]
      
      def solution(clothes):
      # 1 : 종류별 의상 갯수를 담는 딕셔너리
      clothes_dict = {}
      for name, kind in clothes:
          if kind in clothes_dict:
              clothes_dict[kind] += 1
          else:
              clothes_dict[kind] = 1
      # 1 결과 : 
       clothes_dict = {
       "headgear": 2,  # yellow_hat, green_turban
       "eyewear": 1    # blue_sunglasses
        }
       
      # 2 : 경우의 수 계산
      combinations = 1 # 곱셈을 위해 초기값 1 세팅
      for count in clothes_dict.values():
          combinations *= (count + 1) 
      # 2 과정 : 
      headgear의 경우의 수 :
      의상 개수 : 2
      경우의 수 : 2 + 1 = 3 
      (yellow_hat, green_turban, 아무것도 입지 않음)
      
      eyewear의 경우의 수 :
      의상 개수 : 1
      경우의 수 : 1 + 1 = 2 
      (blue_sunglasses, 아무것도 입지 않음)
      
      # 2 결과 :
      루프를 통해 각 종류별 경우의 수를 모두 곱하기 :
      첫 번째 반복 : 
      combinations *= (2 + 1) -> combinations = 1 * 3 = 3
      두 번째 반복 : 
      combinations *= (1 + 1) -> combinations = 3 * 2 = 6
      
      return combinations - 1 
      # 아무것도 입지 않는 경우의 수 1 빼주기
      

# 머신러닝 강의, 세션 복습


#OUTRO

오늘의 한 줄.

주말 3일, 평일 4일 하고 싶다 !

profile
커피 좋아하는 데이터 꿈나무

0개의 댓글