[프로그래머스]소수 만들기(Python)

2022. 8. 9. 13:17·CodingTest/Programmers

0. 문제 링크

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

 

프로그래머스

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

programmers.co.kr

 

1. 문제 설명

1) 주어진 배열에서 3개의 숫자를 더한 후

2) 소수인지 판단

3) 주어진 배열에서 가능한 조합의 개수 구하기

 

2. 입출력

# input
nums = [1, 2, 7, 6, 4]

# output
answer = 4

 

3. 코드

def isPrime(temp):
    if temp > 1:
        for i in range(2, temp):
            if temp % i == 0:
                return False
    else:
        return False
    return True

def solution(nums):
    # nums : 주어진 숫자의 배열
    answer = 0
    len_num = len(nums)

    for i in range(len_num):
        for j in range(i+1, len_num):
            for k in range(j+1, len_num):
                temp = nums[i] + nums[j] + nums[k]
                if isPrime(temp) == True:
                    answer += 1
    return answer

 

실행 결과

 

4. 알게된 점

1) boolean을 사용하여 판별하여 간단하게 코드 구현

 

5. Github

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

 

GitHub - iihye/Algorithm

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

github.com

 

728x90
저작자표시 비영리 변경금지 (새창열림)

'CodingTest > Programmers' 카테고리의 다른 글

[프로그래머스]완주하지 못한 선수(Python)  (0) 2022.08.10
[프로그래머스]폰켓몬(Python)  (0) 2022.08.09
[프로그래머스]내적(Python)  (0) 2022.07.21
[프로그래머스]음양 더하기(Python)  (0) 2022.07.20
[프로그래머스]없는 숫자 더하기(Python)  (0) 2022.07.19
'CodingTest/Programmers' 카테고리의 다른 글
  • [프로그래머스]완주하지 못한 선수(Python)
  • [프로그래머스]폰켓몬(Python)
  • [프로그래머스]내적(Python)
  • [프로그래머스]음양 더하기(Python)
iihye_
iihye_
  • iihye_
    hye-log
    iihye_
    • 분류 전체보기 (296)
      • Development (19)
        • Spring (4)
        • Python (5)
        • React (4)
        • Next.js (1)
        • JavaScript (4)
        • CSS (1)
      • Infra (15)
        • Docker (1)
        • Jenkins (2)
        • Nginx (3)
        • JBoss (1)
        • Windows (3)
        • Linux (3)
        • Kafka (2)
      • Database (1)
        • MongoDB (1)
      • Tools (2)
        • Git (0)
        • Github (1)
        • Document (1)
      • CodingTest (125)
        • Programmers (38)
        • Baekjoon (52)
        • SWEA (27)
        • Jungol (4)
        • Codetree (1)
        • Goorm (3)
      • Education (99)
        • SSAFY 10기 (5)
        • AI Tech 4기 (94)
      • NomadCoder (35)
        • (JS)크롬 앱 만들기 (35)
      • Notice (0)
  • 링크

    • Github
  • 인기 글

  • 최근 글

  • 전체
    오늘
    어제
  • hELLO· Designed By정상우.v4.10.5
iihye_
[프로그래머스]소수 만들기(Python)
상단으로

티스토리툴바