์ƒˆ์†Œ์‹

๐Ÿงฎ PS

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] (Python) - ์†Œ์ˆ˜ ์ฐพ๊ธฐ

  • -

๋ฌธ์ œ ๋ถ„๋ฅ˜ : ์™„์ „ํƒ์ƒ‰ Lv.2

 

Code(python)

from itertools import permutations as pm

def prime(n) :
    if (n < 2) :
        return False
    for i in range(2, int(n**0.5) + 1) :
        if (n % i ==0) :
            return False
    return True

def check(numbers) :
    numbers = list(numbers)
    cnt = 0
    for i in numbers :
        if prime(i) :
            cnt += 1
    return cnt

def get_permutations(numbers):
    max_len = len(numbers)
    numbers = list(numbers)
    int_nb = list(map(int, numbers))
    if max_len < 2 :
        numbers = map(int, numbers)
        return check(set(numbers))
    else :
        for i in range(2, max_len + 1):
            temp = list(map("".join, (pm(numbers, i))))
            temp = list(map(int, temp))
            int_nb.extend(temp)
    return check(set(int_nb))

def solution(numbers):
    return    get_permutations(numbers)

 

์ฝ”๋“œ ์„ค๋ช…

 

prime(n) : n์ด ์†Œ์ˆ˜์ธ์ง€ ํŒ๋ณ„ํ•˜๋Š” ํ•จ์ˆ˜

check(numbers): numbers์˜ ์›์†Œ๋“ค์„ prime(n) ์— ๋„ฃ๊ณ , True๋ฉด cnt๋ฅผ 1์”ฉ ๋”ํ•ด์ค€๋‹ค.

get_permutations(numbers) : itertools์˜ permutation ๋ฉ”์„œ๋“œ ์‚ฌ์šฉ. numbers ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ์กฐํ•ฉ์˜ ์ˆซ์ž๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. ( set๊ณผ map์„ ์ด์šฉํ•˜์—ฌ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•˜๊ณ , intํ˜•์œผ๋กœ conversionํ–ˆ๋‹ค.)

Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.