def solution(n):
    answer = 0
    for i in str(n):
        answer += int(i)

    return answer

 

나의 풀이 - itertools combinations, set 사용

from itertools import combinations

def solution(numbers):
    answer = []
    for i in combinations(numbers, 2):
        answer.append(sum(i))
    return sorted(list(set(answer)))

나의 풀이

def solution(n):
    answer = ''

    while n > 0:
        n, y = divmod(n, 3)
        answer += str(y)
    return int(answer, 3)

 

살펴볼만한 코드

def solution(n):
    tmp = ''
    while n:
        tmp += str(n % 3)
        n = n // 3

    answer = int(tmp, 3)
    return answer

내 코드

def solution(a, b):
	return sum([a[i] * b[i] for i in range(len(a))])

 

살펴볼만한 코드

def solution(a, b):
	return sum([x * y for x, y in zip(a, b)])

 

파이썬 zip 함수란?

iterable 객체를 인자로 받아 묶은 뒤 하나씩 반환

 

a = [1, 2, 3]

b = ['a', 'b', 'c']

for c in zip(a, b):

    print(c)

 

-> (1, 'a'), (2, 'b'), (3, 'c')...

+ Recent posts