1. ToDo
- 프로그래머스 코딩테스트 풀이
2. 풀이 조건 요약
[기본 조건]
- nums에 들어있는 숫자의 개수는 3개 이상 50개 이하입니다.
- nums의 각 원소는 1 이상 1,000 이하의 자연수이며, 중복된 숫자가 들어있지 않습니다.
3. 풀이
from itertools import combinations
def solution(nums: list):
answer = 0
temp = list(combinations(nums, 3))
for (a, b, c) in temp:
total = a + b + c
temp = 0
for i in range(2, total):
if total % i == 0:
temp += 1
break
if temp == 0:
answer += 1
return answer
test_arr = [[1,2,3,4], [1,2,7,6,4], [1, 4, 5, 6, 2, 3], [1, 9, 3, 2, 8, 7, 6, 5, 4]]
for ar in test_arr:
print(solution(ar))
4. 다른 사람의 풀이
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[0], i[1], i[2]): answer += 1
return answer
test_arr = [[1,2,3,4], [1,2,7,6,4], [1, 4, 5, 6, 2, 3], [1, 9, 3, 2, 8, 7, 6, 5, 4]]
for ar in test_arr:
print(solution(ar))