Programmers / 두 개 뽑아서 더하기 / Python

2022. 3. 29. 19:09·coding test - python/Programmers
728x90
반응형

 

*문제 출처는 프로그래머스에 있습니다.

문제 제목: 두 개 뽑아서 더하기

문제 사이트: https://programmers.co.kr/learn/courses/30/lessons/68644


나의 풀이

def solution(numbers):
    answer = []
    for i in range(len(numbers)):
        for j in range(i+1,len(numbers)):
            answer.append(numbers[i]+numbers[j])
    return sorted(list(set(answer)))

나의 풀이가 다른 사람들도 많이 제출한 풀이라서 처음엔 놀랐다...

중복을 제거하기 위해서 set함수를 사용하였고 리스트를 sorted함수로 정리해주었다.

for문에서 오류가 많이 났었는데, 범위 정할 때 i + 1로 시작하지 않고, 1로 지정해주어서 오류가 잦았다...

나중에 문제를 다시 읽으면서 틀린걸 깨달았다.

 

 

다른 풀이

from itertools import combinations
def solution(numbers):
    return sorted(set(sum(i) for i in list(combinations(numbers, 2))))

from itertools import combinations 설명

 

https://docs.python.org/ko/3/library/itertools.html#itertools.combinations

 

 

itertools — 효율적인 루핑을 위한 이터레이터를 만드는 함수 — Python 3.10.4 문서

 

docs.python.org

https://blog.naver.com/hunii123/222322728605

 

[라이브러리] itertools : combinations (조합)

#itertools #pythoncombination #python조합 #파이썬조합 #파이썬itertools ※ 이것은 취업을 위한 코딩테...

blog.naver.com


※ 알아야 할 것

- 다른 풀이에서는 라이브러리를 이용하여 문제를 풀었다.

- sorted함수는 리스트 함수일 때 list를 작성하지 않으면 오류가 난다.

예시) sorted(list(set(answer))) ⭕ / sorted(set(answer))❌

728x90
반응형

'coding test - python > Programmers' 카테고리의 다른 글

Programmers / 자릿수 더하기 / Python  (0) 2022.03.29
Programmers / 짝수와 홀수 / Python  (0) 2022.03.29
Programmers / 모의고사 / Python  (0) 2022.03.29
Programmers / 핸드폰 번호 가리기 / Python  (0) 2022.03.29
Programmers / 직사각형 별찍기 / Python  (0) 2022.03.29
'coding test - python/Programmers' 카테고리의 다른 글
  • Programmers / 자릿수 더하기 / Python
  • Programmers / 짝수와 홀수 / Python
  • Programmers / 모의고사 / Python
  • Programmers / 핸드폰 번호 가리기 / Python
sillon
sillon
꾸준해지려고 합니다..
    반응형
  • sillon
    sillon coding
    sillon
  • 전체
    오늘
    어제
    • menu (614)
      • notice (2)
      • python (68)
        • 자료구조 & 알고리즘 (23)
        • 라이브러리 (19)
        • 기초 (8)
        • 자동화 (14)
        • 보안 (1)
      • coding test - python (301)
        • Programmers (166)
        • 백준 (76)
        • Code Tree (22)
        • 기본기 문제 (37)
      • coding test - C++ (5)
        • Programmers (4)
        • 백준 (1)
        • 기본기문제 (0)
      • 공부정리 (5)
        • 신호처리 시스템 (0)
        • Deep learnig & Machine lear.. (41)
        • Data Science (18)
        • Computer Vision (17)
        • NLP (40)
        • Dacon (2)
        • 모두를 위한 딥러닝 (강의 정리) (4)
        • 모두의 딥러닝 (교재 정리) (9)
        • 통계 (2)
      • HCI (23)
        • Haptics (7)
        • Graphics (11)
        • Arduino (4)
      • Project (21)
        • Web Project (1)
        • App Project (1)
        • Paper Project (1)
        • 캡스톤디자인2 (17)
        • etc (1)
      • OS (10)
        • Ubuntu (9)
        • Rasberry pi (1)
      • App & Web (9)
        • Android (7)
        • javascript (2)
      • C++ (5)
        • 기초 (5)
      • Cloud & SERVER (8)
        • Git (2)
        • Docker (1)
        • DB (4)
      • Paper (7)
        • NLP Paper review (6)
      • 데이터 분석 (0)
        • GIS (0)
      • daily (2)
        • 대학원 준비 (0)
      • 영어공부 (6)
        • job interview (2)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    소수
    Python
    programmers
    백준
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
sillon
Programmers / 두 개 뽑아서 더하기 / Python
상단으로

티스토리툴바