음양 더하기

Mkim4·2023년 6월 20일
1

프로그래머스-음양 더하기 (링크 바로가기)

(나의 풀이방법)

def solution(absolutes, signs):
    for i in range(len(signs)):
        if signs[i] == False:
            absolutes[i] = -(absolutes[i]) #[4, -7, 12]
    answer = sum(absolutes)
    return answer

(다른사람의 풀이방법)

def solution(absolutes, signs):
    return sum(absolutes if sign else -absolutes for absolutes, sign in zip(absolutes, signs))

zip함수

zip( ) 함수_ 길이가 같은 리스트 등의 요소를 묶어주는 함수

zip_list = zip(num, fruit, color)
num = [1, 2, 3]
fruit = ['apple', 'banana', 'orange']
color = ['red', 'yellow', 'orange']
zip_list = [(1, 'apple', 'red'), (2, 'banana', 'yellow'), (3, 'orange', 'orange')]
profile
귀요미 개발자

0개의 댓글