Skip to content
This repository was archived by the owner on Mar 18, 2024. It is now read-only.

[2023-08-21] sumin #137 #149

Merged
merged 5 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""
풀이시간: 20분

<input>
- 1 ≤ n ≤ 1,000,000
- 3 ≤ k ≤ 10

<solution>
1) n을 k진수로 변환
2) 소수 판별

<시간복잡도>
O(log_k*n * √n)
"""
# n을 k진수로 변환하는 함수
def convert_to_base(n: int, base: int) -> str:
result = '' # k진수로 변환 후 값
while n:
n, remainder = divmod(n, base)
result += str(remainder)
return result[::-1]

# 소수 판별
def is_prime(number: int) -> bool:
if number <= 1:
return False
i = 2
while i * i <= number:
if number % i == 0:
return False
i += 1
return True


def solution(n: int, k: int) -> int:
converted_num = convert_to_base(n, k) # n을 k진수로 변환
cnt = 0 # 조건에 맞는 소수의 개수
# 변환된 수에 대한 조건으로 0이 양쪽 or 오른쪽 or 왼쪽 or 아예 안 붙는 경우에 해당하는 지 확인해야 하기 때문에 0을 기준으로 나눠줌
# -> 0을 기준으로 나눠지는 수는 해당 조건들 중 어느 하나라도 만족함
for segment in converted_num.split('0'):
if not segment: # 빈 문자열
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

역시 빈문자열을 제외할 방법이 condition문을 제외하고는 어렵네요 ㅠ

continue
if is_prime(int(segment)): # 소수
cnt += 1
return cnt

# 테스트 케이스
n1, k1 = 437674, 3
print(solution(n1, k1))
2 changes: 1 addition & 1 deletion Programmers - 문제풀이/전화번호 목록/sumin.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<시간 복잡도>
1) O(nlogn)
2) O(nm): n은 전화번호의 총 개수, m은 최대
2) O(nm): n은 전화번호의 총 개수, m은 전화번호의 길이로 최대 20

<기타>
이 외에 트라이로도 풀이 가능
Expand Down