공부/코딩테스트

[프로그래머스] 소수만들기

ghhong 2021. 12. 28. 15:18

 

나의 답 :

def solution(nums):
    answer = 0
    l = len(nums)
    for i in range(0,l-2):
        for j in range(i+1,l-1):
            for k in range(j+1,l):
                sum=nums[i]+nums[j]+nums[k]
                print(sum)
                flag=True
                for w in range(2,sum):
                    if(sum%w==0):
                        flag=False
                        break
                if(flag==True): answer+=1

    return answer

 

다른 사람의 답 : 

def solution(nums):
    from itertools import combinations as cb
    answer = 0
    for a in cb(nums, 3):
        cand = sum(a)
        for j in range(2, cand):
            if cand%j==0:
                break
        else:
            answer += 1
    return answer

 

 

for else문 좋다.