[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค/python] Summer/Winter Coding(~2018) : ์†Œ์ˆ˜ ๋งŒ๋“ค๊ธฐ

2021. 6. 24. 17:04

๋ฌธ์ œ ์„ค๋ช…

์ฃผ์–ด์ง„ ์ˆซ์ž ์ค‘ 3๊ฐœ์˜ ์ˆ˜๋ฅผ ๋”ํ–ˆ์„ ๋•Œ ์†Œ์ˆ˜๊ฐ€ ๋˜๋Š” ๊ฒฝ์šฐ์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ˆซ์ž๋“ค์ด ๋“ค์–ด์žˆ๋Š” ๋ฐฐ์—ด nums๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, nums์— ์žˆ๋Š” ์ˆซ์ž๋“ค ์ค‘ ์„œ๋กœ ๋‹ค๋ฅธ 3๊ฐœ๋ฅผ ๊ณจ๋ผ ๋”ํ–ˆ์„ ๋•Œ ์†Œ์ˆ˜๊ฐ€ ๋˜๋Š” ๊ฒฝ์šฐ์˜ ๊ฐœ์ˆ˜๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

 

์ œํ•œ์‚ฌํ•ญ

  • nums์— ๋“ค์–ด์žˆ๋Š” ์ˆซ์ž์˜ ๊ฐœ์ˆ˜๋Š” 3๊ฐœ ์ด์ƒ 50๊ฐœ ์ดํ•˜์ž…๋‹ˆ๋‹ค.
  • nums์˜ ๊ฐ ์›์†Œ๋Š” 1 ์ด์ƒ 1,000 ์ดํ•˜์˜ ์ž์—ฐ์ˆ˜์ด๋ฉฐ, ์ค‘๋ณต๋œ ์ˆซ์ž๊ฐ€ ๋“ค์–ด์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

 

์ •๋‹ต

from itertools import combinations

def solution(nums):
    answer = 0
   
    for com in combinations(nums, 3):
        check = True
        sum_com = sum(com)
        for i in range(2, int(sum_com**0.5)+1):
            if sum_com%i == 0:
                check = False
                break
        if check == True :
            answer += 1
                
    return answer
728x90

BELATED ARTICLES

more