Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

Algorithm/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์†Œ์ˆ˜ ๋งŒ๋“ค๊ธฐ (level.1)

๊ฐ์ž ๐Ÿฅ” 2021. 9. 3. 16:16
๋ฐ˜์‘ํ˜•

 

๋ฌธ์ œ๋งํฌ

https://programmers.co.kr/learn/courses/30/lessons/12977

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์†Œ์ˆ˜ ๋งŒ๋“ค๊ธฐ

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

programmers.co.kr

 

๋‚˜์˜ ํ’€์ด

  1. ์†Œ์ˆ˜์ž„์„ ํ™•์ธํ•˜๋Š” checkํ•จ์ˆ˜์™€, ๋ฉ”์ธํ•จ์ˆ˜์ธ solution ํ•จ์ˆ˜๋กœ ๊ตฌ์„ฑํ–ˆ๋‹ค.
  2. check์—์„œ๋Š” 2๋ถ€ํ„ฐ ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ฉฐ ๋‚˜๋ˆ ๋ณด๊ณ , ๋‚˜๋จธ์ง€๊ฐ€ 0์ด๋‚˜์˜ค๋Š”์ˆœ๊ฐ„ False๋ฅผ ์ถœ๋ ฅํ•˜๊ฒŒ ํ•˜๋ฉด์„œ ์†Œ์ˆ˜๋ฃฐ ๊ตฌ๋ณ„ํ–ˆ๋‹ค.
  3. solution์—์„œ๋Š” combination ํ•จ์ˆ˜๋กœ 3๊ฐœ์”ฉ ์กฐํ•ฉ์„ ์ด๋ค„๋†“๊ณ , sum์„๊ตฌํ•ด checkํ•จ์ˆ˜๋กœ ๋ณด๋ƒˆ๋‹ค. 
  4. checkํ•จ์ˆ˜๋ฅผ ๊ฑฐ์ณ ๋‚˜์˜จ ๊ฒฐ๊ณผ True์ด๋ฉด answer์— +1 ํ•ด์ฃผ๋Š” ๋ฐฉ์‹์ด๋‹ค.
from itertools import combinations

def check(num):
    for i in range(2, num):
        if num % i == 0:
            return False
    return True
    
def solution(nums):
    answer = 0
    combi = list(combinations(nums, 3))
    for n in combi:
        if check(sum(n)) == True:
            answer +=1
    return answer

 

๋‹ค๋ฅธ์‚ฌ๋žŒ ํ’€์ด

๋Œ€๋ถ€๋ถ„ combination์„ ํ™œ์šฉํ•œ ์ฝ”๋“œ๊ฐ€ ์ข‹์•„์š”๋ฅผ ๋งŽ์ด ๋ฐ›์•˜๋‹ค. ๋‚˜์ฒ˜๋Ÿผ ํ•จ์ˆ˜๋ฅผ ๋‘๊ฐœ๋กœ ๋‚˜๋ˆ ์„œ ํ‘ผ์‚ฌ๋žŒ๋„ ์žˆ์—ˆ๊ณ , ๊ทธ๋ƒฅ solution ํ•˜๋‚˜์˜ ํ•จ์ˆ˜๋กœ ํ‘ผ ํ’€์ด๋„ ์žˆ์—ˆ๋‹ค. ๋‹ค ํ’€์ด๊ฐ€ ๋น„์Šทํ•˜๋ฏ€๋กœ ๋งํฌ๋งŒ ๋‚จ๊ฒจ๋‘๊ฒ ๋”ฐ.

๋ฐ˜์‘ํ˜•