Notice
Recent Posts
Recent Comments
Link
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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
- error
- ๋ชจ๊ฐ์ฝ
- ๊ตญ๋น์ง์
- ํ์ด์ฌ
- ์ฝ๋ฉํ ์คํธ
- ๊ฐ๋ฐ
- javascript
- ์ฝ๋ฉ์ ํ
- ํฌ๋กค๋ง
- ํ๋ก์ ํธ
- ๋ ธ๋ง๋์ฝ๋
- mongodb
- HTML
- ์ฝ๋ฉ
- CSS
- ์๊ณ ๋ฆฌ์ฆ
- node.js
- ํ๋ก๊ทธ๋๋จธ์ค
- ๋๋ฆผ์ฝ๋ฉ
- KDT
- Til
- ๋ฐฑ์ค
- heapq
- ๊ทธ๋ฆฌ๋
- react
- JS
- fe
- Python
- ํ ์ดํ๋ก์ ํธ
- ํ๋ก ํธ์๋
Archives
- Today
- Total
๐ฑ → ๐ณ
[ํ๋ก๊ทธ๋๋จธ์ค] ์์ ์ฐพ๊ธฐ - python ๋ณธ๋ฌธ
728x90
https://school.programmers.co.kr/learn/courses/30/lessons/42839
๋ฌธ์ ์ ํ : ์์ ํ์
๋์ด๋ : level 2
์ฝ๋
๋ด ์ฝ๋
import itertools
def is_prime(n):
if n <= 1:
return False
if n <= 3:
return True
if n % 2 == 0 or n % 3 == 0:
return False
i = 5
while i * i <= n:
if n % i == 0 or n % (i + 2) == 0:
return False
i += 6
return True
def remove_non_primes(arr):
return [num for num in arr if is_prime(num)]
def solution(numbers):
arr = []
for i in range(1, len(numbers)+1):
arr.extend(list(itertools.permutations(numbers, i)))
li = set()
for i in arr:
_str = ''.join(i)
li.add(int(_str))
li = remove_non_primes(li)
return len(li)
- permutations ์ฌ์ฉํด์ ๋ชจ๋ ๊ฒฝ์ฐ์ ์ arr์ ์ ์ฅ
- ์ซ์๋ก ๋ณํ ํ ๊ฐ์ ์ซ์ ์ญ์ (set ์ฌ์ฉ)
- remove_non_primes() ํจ์ ์ฌ์ฉํด์ ์์๊ฐ ์๋๋ฉด ์ญ์
- is_prime() ํจ์ ์ฌ์ฉํด์ ์์ ์ธ์ง ์๋์ง ํ๋ณ
๋ค๋ฅธ ํ์ด
import math
def check_prime(n):
if n < 2:
return False
if n == 2:
return True
for i in range(2, int(math.sqrt(n)) + 1):
if (n % i) == 0:
return False
return True
number_set = set()
def permutation(base, array):
if base:
number_set.add(int(base))
for i, s in enumerate(array):
permutation(base + s, array[:i] + array[i+1:])
def solution(numbers):
answer = 0
permutation("", list(numbers))
answer = len(list(filter(check_prime, number_set)))
return answer
๋ด ํ์ด๋ ๋น์ทํ๊ธด ํ๋ฐ ์ด๋ฐ ๋ฐฉ๋ฒ๋ ์๊ตฐ
- python filter : https://www.daleseo.com/python-filter/
728x90
'Algorithms' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค] ํ๋ก์ธ์ค - python (0) | 2023.08.30 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค] ์ ํ๋ฒํธ ๋ชฉ๋ก - python (0) | 2023.08.29 |
[ํ๋ก๊ทธ๋๋จธ์ค] ๊ทค ๊ณ ๋ฅด๊ธฐ - Python (0) | 2023.08.15 |
[ํ๋ก๊ทธ๋๋จธ์ค] ๋ฉ๋ฆฌ๋ฐ๊ธฐ - Python (0) | 2023.08.14 |
[ํ๋ก๊ทธ๋๋จธ์ค] N๊ฐ์ ์ต์๊ณต๋ฐฐ์ - Python (0) | 2023.08.13 |