<프로그래머스 문제풀이>
해답)
from functools import reduce
def solution(a, b):
return reduce(lambda x, y: x+y, range(a, b+1)) if a <= b \
else reduce(lambda x, y: x+y, range(b, a+1))
풀이)
* reduce 함수는 이터러블 내의 두 인자씩 계속 계산을 해줍니다. (recursive하게 갑니다.)
https://codepractice.tistory.com/86
(파이썬) functools 모듈의 reduce 함수
functools 모듈의 reduce 함수는 다음과 같다. def reduce(function, iterable, initializer=None): it = iter(iterable) if initializer is None: value = next(it) else: value = initializer for element in it..
codepractice.tistory.com
저는 자바스크립트에 익숙해서 reduce를 써줬는데
파이썬은 그냥 sum(range(a,b+1)) 이런식으로 해도 상관없더군요 ㅎㅎ
reduce 복습한다 생각했습니다~
* 이 문제 및 로고의 저작권은 Programmers에 있습니다.
출처: 프로그래머스 코딩 테스트 연습, https://programmers.co.kr/learn/challenges
728x90
'문제풀이 > Programmers' 카테고리의 다른 글
[프로그래머스💯] 코딩테스트 연습 > 문자열 내림차순으로 배치하기 / Python 문제풀이 (0) | 2020.03.13 |
---|---|
[프로그래머스💯] 코딩테스트 연습 > 연습문제 > 문자열 내 p와 y의 개수 (0) | 2020.03.13 |
[프로그래머스💯] 코딩테스트 연습 > 이분탐색 > 입국심사 / Python 문제풀이 (0) | 2020.03.09 |
[프로그래머스💯] 코딩테스트 연습 > (DFS/BFS) > 여행경로 / Python 문제풀이 (0) | 2020.03.04 |
[프로그래머스💯] 코딩테스트 연습 > 동적 계획법 > 카드 게임 (0) | 2020.03.02 |
댓글