๐Ÿ’ป

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต > Summer/Winter Coding(~2018) > ์†Œ์ˆ˜ ๋งŒ๋“ค๊ธฐ ๋ณธ๋ฌธ

์•Œ๊ณ ๋ฆฌ์ฆ˜/๊ธฐ์ดˆ๋‹ค์ง€๊ธฐ

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต > Summer/Winter Coding(~2018) > ์†Œ์ˆ˜ ๋งŒ๋“ค๊ธฐ

๋˜ํšจ๋‹ˆ 2024. 11. 1. 16:01

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

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

SW๊ฐœ๋ฐœ์ž๋ฅผ ์œ„ํ•œ ํ‰๊ฐ€, ๊ต์œก, ์ฑ„์šฉ๊นŒ์ง€ Total Solution์„ ์ œ๊ณตํ•˜๋Š” ๊ฐœ๋ฐœ์ž ์„ฑ์žฅ์„ ์œ„ํ•œ ๋ฒ ์ด์Šค์บ ํ”„

programmers.co.kr

LV1. ์†Œ์ˆ˜ ๋งŒ๋“ค๊ธฐ

 

๐Ÿ’ก์•„์ด๋””์–ด

์ฃผ์–ด์ง„ ์ˆซ์ž๋“ค๋กœ ๋งŒ๋“ค์ˆ˜ ์žˆ๋Š” ์กฐํ•ฉ์„ ์ƒ๊ฐํ•˜๊ณ , ์†Œ์ˆ˜์ธ์ง€ ํ™•์ธํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.

๋ ˆ๋ฒจ1์˜ ์‰ฌ์šด ๋ฌธ์ œ์ธ๋ฐ ๋ฐ”๋ณด ๊ฐ™์ด '์†Œ์ˆ˜'์˜ ์ •์˜๊ฐ€ ์ˆœ๊ฐ„์ ์œผ๋กœ ์ƒ๊ฐ์ด ์•ˆ๋‚˜๊ณ  ํ™€์ˆ˜๋กœ ์ฐฉ๊ฐํ•˜๊ณ  ํ’€์—ˆ๋‹ค ๋ฐ”๋ณด๊ฐ™์ด ใ…Žใ…Ž

๋‘๊ฐ€์ง€ ๋ฐฉ๋ฒ•์œผ๋กœ ํ’€์–ด๋ณด์•˜๋Š”๋ฐ, ์ฒซ๋ฒˆ์งธ๋Š” ๋ฐ˜๋ณต๋ฌธ 3๊ฐœ๋ฅผ ์จ์„œ 3๊ฐœ์˜ ์ˆ˜ ํ•ฉ์„ ๊ตฌํ–ˆ๊ณ  ๋‘๋ฒˆ์งธ๋Š” itertools ๋ชจ๋“ˆ์— combinations ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค. 

 

 

๐Ÿ’ป ์ฝ”๋“œ์ž‘์„ฑ

from itertools import combinations, permutations

def check(number):
# ์†Œ์ˆ˜์ธ์ง€ ์ฒดํฌํ•˜๋Š” ํ•จ์ˆ˜
# ์†Œ์ˆ˜: 1๊ณผ ์ž๊ธฐ ์ž์‹ ๋งŒ์„ ์•ฝ์ˆ˜๋กœ ํ•˜๋Š” ์ˆซ์ž
    cnt = 0
    for i in range(1, number+1):
        if number % i == 0 : cnt += 1
        
    if cnt == 2 : return True

    return False

def solution(nums):
    answer = 0
# ์ฒซ๋ฒˆ์งธ ํ’€์ด: ๋ฐ˜๋ณต๋ฌธ 3๊ฐœ ์‚ฌ์šฉ
    # temp = 0
    # cnt = 0
    # for i in range(len(nums)-2):
    #     for j in range(i+1, len(nums)-1):
    #         for k in range(j+1, len(nums)):
    #             temp = nums[i] + nums[j] + nums[k]
    #             if check(temp) :
    #                 print(temp)
    #                 answer += 1 
    
# ๋‘๋ฒˆ์งธ ํ’€์ด: itertools ๋ชจ๋“ˆ combination ํ•จ์ˆ˜ ์‚ฌ์šฉ
    comb_list = []
    for i in combinations(nums, 3):
         comb_list.append(sum(i))

    for i in range(len(comb_list)):
        if check(comb_list[i]):
            answer += 1
        
    return answer
๋ฐ˜์‘ํ˜•
Comments