Algorithm/Programmers
[프로그래머스/python3] Level 1 음양 더하기 문제풀이
찐무
2021. 9. 17. 14:47
728x90
반응형

문제
https://programmers.co.kr/learn/courses/30/lessons/76501
코딩테스트 연습 - 음양 더하기
어떤 정수들이 있습니다. 이 정수들의 절댓값을 차례대로 담은 정수 배열 absolutes와 이 정수들의 부호를 차례대로 담은 불리언 배열 signs가 매개변수로 주어집니다. 실제 정수들의 합을 구하여 re
programmers.co.kr
어떤 정수들이 있습니다. 이 정수들의 절댓값을 차례대로 담은 정수 배열 absolutes와 이 정수들의 부호를 차례대로 담은 불리언 배열 signs가 매개변수로 주어집니다. 실제 정수들의 합을 구하여 return 하도록 solution 함수를 완성해주세요.
<정답>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def solution(absolutes, signs): | |
answer = 0 | |
for i in range(len(absolutes)): | |
if (signs[i] == True): | |
answer += absolutes[i] | |
else: | |
answer -= absolutes[i] | |
return answer |
※ 해당 문제에서 주의할 점은 signs의 값이 문자열로 'true', 'false'로 들어있는 것이 아니라, bool로 True, False로 들어있다는 점이다.
따라서 if문을 작성할 때, if(signs[i] == 'true')가 아닌 if(signs[i] == True)가 되어야 한다.
728x90
반응형