[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค-Lv2] ํƒ€๊ฒŸ ๋„˜๋ฒ„ / Python

๐Ÿ“š Problem Solving/Programmers

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ํƒ€๊ฒŸ ๋„˜๋ฒ„

n๊ฐœ์˜ ์Œ์ด ์•„๋‹Œ ์ •์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์ˆ˜๋ฅผ ์ ์ ˆํžˆ ๋”ํ•˜๊ฑฐ๋‚˜ ๋นผ์„œ ํƒ€๊ฒŸ ๋„˜๋ฒ„๋ฅผ ๋งŒ๋“ค๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด [1, 1, 1, 1, 1]๋กœ ์ˆซ์ž 3์„ ๋งŒ๋“ค๋ ค๋ฉด ๋‹ค์Œ ๋‹ค์„ฏ ๋ฐฉ๋ฒ•์„ ์“ธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. -1+1+1+1+1 = 3 +1-1+1+1+

programmers.co.kr

import sys
from itertools import product

input = sys.stdin.readline

target = int(input())
numbers = list(map(int, input().split()))


def solution(numbers, target):
    i = [(x, -x) for x in numbers]
    j = list(map(sum, product(*i)))
    return j.count(target)


print(solution(numbers, target))

 

ํ•ด์„ค

i๋Š” numbers์— ์žˆ๋Š” ๊ฐ ์ˆ˜์˜ ์–‘์ˆ˜์™€ ์Œ์ˆ˜ ๊ฐ’์„ ํŠœํ”Œ๋กœ ์ €์žฅํ•œ ๋ฆฌ์ŠคํŠธ๋‹ค.

product๋ฅผ ์ด์šฉํ•ด i์— ๋“ค์–ด์žˆ๋Š” ๋ฐ์ดํ„ฐ๋“ค์˜ ๊ณฑ์ง‘ํ•ฉ์„ ๊ตฌํ•ด์ค€๋‹ค.

๊ณฑ์ง‘ํ•ฉ๋“ค์˜ ํ•ฉ์„ ๊ตฌํ•ด target๊ณผ ๊ฐ™์€ ์ˆ˜์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.