hye-log

[프로그래머스]음양 더하기(Python) 본문

CodingTest/Programmers

[프로그래머스]음양 더하기(Python)

iihye_ 2022. 7. 20. 18:08

0. 문제 링크

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

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

1. 문제 설명

1) 음수 구하기

2) 합 구하기

 

2. 입출력

# input
absolutes = [4, 7, 12]
signs = [True, False, True]

# output
answer = 9

 

3. 코드

1) 첫 번째 코드

def solution(absolutes, signs):
    # absolutes : 숫자 배열
    # signs : 숫자의 부호

    answer = 123456789

    # 음수 구하기
    for i in range(len(absolutes)):
        if signs[i] == False:
           absolutes[i] *= -1

    # 합 구하기
    answer = sum(absolutes)

    return answer

 

실행 결과

 

4. 알게된 점

 

5. Github

https://github.com/iihye/Algorithm/blob/main/Programmers/neg_pos.py

 

GitHub - iihye/Algorithm

Contribute to iihye/Algorithm development by creating an account on GitHub.

github.com

 

 

728x90
Comments