https://programmers.co.kr/learn/courses/30/lessons/12977
ํ์ด
# ํ๋ก๊ทธ๋๋จธ์ค 1๋จ๊ณ - ์์ ๋ง๋ค๊ธฐ
from itertools import combinations
def check(a, b, c):
total = a + b + c
for i in range(2, total):
if total % i == 0: return False
return True
def solution(nums):
answer = 0
A = list(combinations(nums, 3))
for i in A:
if check(i[i], i[1], i[2]): answer += 1
return answer
itertools์ combinations์ ์ฌ์ฉํ์ฌ 3๊ฐ์ง๋ฅผ ์ ํํ๋ ์กฐํฉ์ ๋ง๋ค๊ณ
check ํจ์๋ฅผ ํตํด ์์์ธ์ง ์๋์ง ํ๋ณ
permutations / combination / product ์ฌ์ฉ๋ฒ
items = ['1', '2', '3', '4', '5']
from itertools import permutations
list(permutations(items, 2))
# ๋ชจ๋ ์์ด์ ์กฐํฉ (์์๊ฐ ๋ฐ๋ ์ค๋ณต O)
from itertools import combinations
list(combinations(items, 2))
# ๋ชจ๋ ์์ด์ ์กฐํฉ(์ค๋ณต x)
from itertools import product
items = [['a', 'b', 'c,'], ['1', '2', '3', '4'], ['!', '@', '#']]
# [('a','1','!), ('a','1','@') ...]
'๐๏ธ Algorithm > โฌ ํ๋ก๊ทธ๋๋จธ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Programmers] [์๊ฐ ์ฝ๋ ์ฑ๋ฆฐ์ง ์์ฆ2] [Python] Level1_์์ ๋ํ๊ธฐ (0) | 2022.03.16 |
---|---|
[Programmers] [์๊ฐ ์ฝ๋ ์ฑ๋ฆฐ์ง ์์ฆ3] [Python] Level1_์๋ ์ซ์ ๋ํ๊ธฐ (0) | 2022.03.16 |
[Programmers] [SQL] ์ ์ ์๊ฐ ๊ตฌํ๊ธฐ(1) (0) | 2022.03.14 |
[Programmers] [SQL] ๋๋ช ๋๋ฌผ ์ ์ฐพ๊ธฐ (0) | 2022.03.14 |
[Programmers] [SQL] ๊ณ ์์ด์ ๊ฐ๋ ๋ช ๋ง๋ฆฌ ์์๊น (0) | 2022.03.14 |