Notice
Recent Posts
Link
- Today
- Total
hye-log
[프로그래머스]소수 만들기(Python) 본문
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 |
Comments