๋ฌธ์ ์ค๋ช
์คํ์ด๋ค์ ๋งค์ผ ๋ค๋ฅธ ์ท์ ์กฐํฉํ์ฌ ์ ์ด ์์ ์ ์์ฅํฉ๋๋ค.
์๋ฅผ ๋ค์ด ์คํ์ด๊ฐ ๊ฐ์ง ์ท์ด ์๋์ ๊ฐ๊ณ ์ค๋ ์คํ์ด๊ฐ ๋๊ทธ๋ ์๊ฒฝ, ๊ธด ์ฝํธ, ํ๋์ ํฐ์ ์ธ ๋ฅผ ์ ์๋ค๋ฉด ๋ค์๋ ์ ์ฒญ๋ฐ์ง๋ฅผ ์ถ๊ฐ๋ก ์ ๊ฑฐ๋ ๋๊ทธ๋ ์๊ฒฝ ๋์ ๊ฒ์ ์ ๊ธ๋ผ์ค๋ฅผ ์ฐฉ์ฉํ๊ฑฐ๋ ํด์ผ ํฉ๋๋ค.
์ข ๋ฅ | ์ด๋ฆ |
---|---|
์ผ๊ตด | ๋๊ทธ๋ ์๊ฒฝ, ๊ฒ์ ์ ๊ธ๋ผ์ค |
์์ | ํ๋์ ํฐ์ ์ธ |
ํ์ | ์ฒญ๋ฐ์ง |
๊ฒ์ท | ๊ธด ์ฝํธ |
์คํ์ด๊ฐ ๊ฐ์ง ์์๋ค์ด ๋ด๊ธด 2์ฐจ์ ๋ฐฐ์ด clothes๊ฐ ์ฃผ์ด์ง ๋ ์๋ก ๋ค๋ฅธ ์ท์ ์กฐํฉ์ ์๋ฅผ return ํ๋๋ก solution ํจ์๋ฅผ ์์ฑํด์ฃผ์ธ์.
์ ํ์ฌํญ
์ ์ถ๋ ฅ ์
clothes | return |
---|---|
[["yellowhat", "headgear"], ["bluesunglasses", "eyewear"], ["green_turban", "headgear"]] | 5 |
[["crowmask", "face"], ["bluesunglasses", "face"], ["smoky_makeup", "face"]] | 3 |
์ ์ถ๋ ฅ ์ ์ค๋ช
์์ #1
headgear์ ํด๋นํ๋ ์์์ด yellow_hat, green_turban์ด๊ณ eyewear์ ํด๋นํ๋ ์์์ด blue_sunglasses์ด๋ฏ๋ก ์๋์ ๊ฐ์ด 5๊ฐ์ ์กฐํฉ์ด ๊ฐ๋ฅํฉ๋๋ค.
1. yellow_hat
2. blue_sunglasses
3. green_turban
4. yellow_hat + blue_sunglasses
5. green_turban + blue_sunglasses
์์ #2
face์ ํด๋นํ๋ ์์์ด crow_mask, blue_sunglasses, smoky_makeup์ด๋ฏ๋ก ์๋์ ๊ฐ์ด 3๊ฐ์ ์กฐํฉ์ด ๊ฐ๋ฅํฉ๋๋ค.
1. crow_mask
2. blue_sunglasses
3. smoky_makeup
๋ฌธ์ ํ์ด
์ฒซ๋ฒ์งธ ํ์ด๐
def solution(clothes):
a=0
two = []
one = []
while True:
if a == len(clothes):
break
for i in range(len(clothes)):
one.append(f"{clothes[i][0]}")
if i != a and i > a:
if clothes[a][1] != clothes[i][1]:
two.append((clothes[a][0],clothes[i][0]))
if i == len(clothes)-1:
a += 1
two = list(set(two))
one = list(set(one))
return len(two) + len(one)
์์์ ์ข
๋ฅ๊ฐ 2๊ฐ์ง๊น์ง์ ๋ฐฐ์ด๋ง ๋ง๋ค ์ ์๋ค
์คํจ๐คช
๋๋ฒ์งธ ํ์ด
def solution(clothes):
kind_dic={}
for name, kind in clothes: #dictionary๋ฅผ ์ด์ฉํ์ฌ ๊ฐ ์ข
๋ฅ์ ๊ฐฏ์๋ฅผ ๊ฐ์ง๊ณ ์จ ๋ค
if kind in kind_dic:
kind_dic[kind] += 1
else:
kind_dic[kind] = 1
count = 1
for i in kind_dic.values(): #dictionary์ value๋ฅผ ๊ฐ์ ธ์ 1์ ๋ํ ์๋ฅผ ๊ณฑํด์ค๋ค (1์ ๋ํ ์ด์ ? 1์ ๋ํ์ง ์์ผ๋ฉด ํด๋น๋๋ ์์์ ์ข
๋ฅ๋ฅผ ์
์ง์์ ๊ฒฝ์ฐ๋ฅผ ๋ํ๋ด์ง ๋ชปํ๋ค)
count *= i+1
return count -1 #์๋ฌด๊ฒ๋ ์ ์
์ ๊ฒฝ์ฐ๋ ์ ํ๋ค
์ฑ๊ณต๐
๋ค๋ฅธ ์ฌ๋ ํ์ด 1
import collections
from functools import reduce
def solution(c):
return reduce(lambda x,y:x*y,[a+1 for a in collections.Counter([x[1] for x in c]).values()])-1
๋ค๋ฅธ ์ฌ๋ ํ์ด 2
def solution(clothes):
from collections import Counter
from functools import reduce
cnt = Counter([kind for name, kind in clothes])
answer = reduce(lambda x, y: x*(y+1), cnt.values(), 1) - 1
return answer