Notice
Recent Posts
Recent Comments
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | ||||||
| 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| 9 | 10 | 11 | 12 | 13 | 14 | 15 |
| 16 | 17 | 18 | 19 | 20 | 21 | 22 |
| 23 | 24 | 25 | 26 | 27 | 28 | 29 |
| 30 |
Tags
- 백준
- ChatGPT
- 금융문자분석경진대회
- PYTHON
- Real or Not? NLP with Disaster Tweets
- 편스토랑 우승상품
- 우분투
- 자연어처리
- 프로그래머스
- 파이썬
- 더현대서울 맛집
- ubuntu
- github
- 데이콘
- leetcode
- Kaggle
- 코로나19
- 편스토랑
- AI 경진대회
- Baekjoon
- SW Expert Academy
- Docker
- dacon
- 프로그래머스 파이썬
- Git
- programmers
- hackerrank
- 캐치카페
- gs25
- 맥북
Archives
- Today
- Total
솜씨좋은장씨
[Programmers] 두 개 뽑아서 더하기 (Python) 본문
728x90
반응형

1일 1문제 278일차의 문제는
프로그래머스의 두 개 뽑아서 더하기 입니다.
코딩테스트 연습 - 두 개 뽑아서 더하기
정수 배열 numbers가 주어집니다. numbers에서 서로 다른 인덱스에 있는 두 개의 수를 뽑아 더해서 만들 수 있는 모든 수를 배열에 오름차순으로 담아 return 하도록 solution 함수를 완성해주세요. 제한
programmers.co.kr
Solution
from itertools import combinations
def solution(numbers):
return list(sorted(set([sum(combs) for combs in combinations(numbers, 2)])))
Solution 해설
먼저 이 문제는 숫자가 담긴 리스트를 주면
여기서 숫자를 두개씩 뽑아서 더하여 만들 수 있는 숫자를 오름차순으로 나열한 리스트를 구하는 문제입니다.
itertools의 combinations를 활용하여 숫자 두개의 쌍이 겹치지 않는 리스트를 구하고
각각의 sum을 구한 리스트를 만든 다음
이를 set으로 중복값을 없앤 후 sorted를 활용하여 오름차순으로 정렬하였습니다.

SOMJANG/CODINGTEST_PRACTICE
1일 1문제 since 2020.02.07. Contribute to SOMJANG/CODINGTEST_PRACTICE development by creating an account on GitHub.
github.com
'Programming > 코딩 1일 1문제' 카테고리의 다른 글
| [Programmers] 최고의 집합 (Python) (0) | 2020.12.22 |
|---|---|
| [Programmers] 이상한 문자 만들기 (Python) (0) | 2020.12.19 |
| [leetCode] 1464. Maximum Product of Two Elements in an Array (Python) (0) | 2020.12.17 |
| [leetCode] 1662. Check If Two String Arrays are Equivalent (Python) (0) | 2020.12.12 |
| [leetCode] 1672. Richest Customer Wealth (Python) (0) | 2020.12.09 |
Comments