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

๐Ÿ“š Problem Solving/Programmers

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์†Œ์ˆ˜ ์ฐพ๊ธฐ

ํ•œ์ž๋ฆฌ ์ˆซ์ž๊ฐ€ ์ ํžŒ ์ข…์ด ์กฐ๊ฐ์ด ํฉ์–ด์ ธ์žˆ์Šต๋‹ˆ๋‹ค. ํฉ์–ด์ง„ ์ข…์ด ์กฐ๊ฐ์„ ๋ถ™์—ฌ ์†Œ์ˆ˜๋ฅผ ๋ช‡ ๊ฐœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š”์ง€ ์•Œ์•„๋‚ด๋ ค ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ์ข…์ด ์กฐ๊ฐ์— ์ ํžŒ ์ˆซ์ž๊ฐ€ ์ ํžŒ ๋ฌธ์ž์—ด numbers๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ข…์ด

programmers.co.kr

import sys
from itertools import permutations

input = sys.stdin.readline

numbers = input().rstrip()


def isPrime(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 solution(numbers):
    answer = []
    for i in range(len(numbers)):
        temp = list(set((map(int, map("".join, permutations(numbers, i + 1))))))
        print(temp)
        for n in temp:
            if isPrime(n):
                answer.append(n)
    return len(set(answer))


print(solution(numbers))

 

ํ•ด์„ค

permutations๋ฅผ ํ†ตํ•ด ๋ชจ๋“  ์กฐํ•ฉ์„ ์ถ”์ถœํ•˜๊ณ  set์œผ๋กœ ์ค‘๋ณต๋˜๋Š” ๊ฐ’์„ ์ œ๊ฑฐํ–ˆ๋‹ค.

๊ทธ๋ ‡๊ฒŒ ์–ป๋Š” ๋ฐ์ดํ„ฐ๋ฅผ isPrime ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์†Œ์ˆ˜ ํŒ์ •์„ ํ•œ ๋’ค, ์†Œ์ˆ˜์ด๋ฉด answer์— ์ถ”๊ฐ€ํ•œ๋‹ค.

answer์˜ ์ค‘๋ณต ๊ฐ’์„ ์ œ๊ฑฐํ•˜๊ธฐ ์œ„ํ•ด set์„ ์ด์šฉํ•˜๊ณ  answer์— ๊ธธ์ด๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.