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

2021. 5. 28. 14:17ใ†๐Ÿ“š Algorithm/๐Ÿ’ช๐Ÿป Python ๋ฌธ์ œ ํ’€์ด

๋ฐ˜์‘ํ˜•
 

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

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

programmers.co.kr

๋ฌธ์ œ ์„ค๋ช…

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

๊ฐ ์ข…์ด ์กฐ๊ฐ์— ์ ํžŒ ์ˆซ์ž๊ฐ€ ์ ํžŒ ๋ฌธ์ž์—ด numbers๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ข…์ด ์กฐ๊ฐ์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ์†Œ์ˆ˜๊ฐ€ ๋ช‡ ๊ฐœ์ธ์ง€ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ ์‚ฌํ•ญ

  • numbers๋Š” ๊ธธ์ด 1 ์ด์ƒ 7 ์ดํ•˜์ธ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค.
  • numbers๋Š” 0~9๊นŒ์ง€ ์ˆซ์ž๋งŒ์œผ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
  • "013"์€ 0, 1, 3 ์ˆซ์ž๊ฐ€ ์ ํžŒ ์ข…์ด ์กฐ๊ฐ์ด ํฉ์–ด์ ธ์žˆ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ์‹œ

์˜ˆ์ œ #1
[1, 7]์œผ๋กœ๋Š” ์†Œ์ˆ˜ [7, 17, 71]๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์˜ˆ์ œ #2
[0, 1, 1]์œผ๋กœ๋Š” ์†Œ์ˆ˜ [11, 101]๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • 11๊ณผ 011์€ ๊ฐ™์€ ์ˆซ์ž๋กœ ์ทจ๊ธ‰ํ•ฉ๋‹ˆ๋‹ค.

 

ํ’€์ด ๊ณผ์ •

์†Œ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๊ณผ์ •์€ ์‰ฝ๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์—ˆ์ง€๋งŒ N๊ฐœ ์ค‘ M๊ฐœ์˜ ์ˆซ์ž๋ฅผ ๊ณจ๋ผ ์ˆ˜๋ฅผ ๋งŒ๋“œ๋Š” ๊ณผ์ •์ด ์™„์ „ํƒ์ƒ‰ bruteforce๋กœ ๊ตฌํ˜„ํ–ˆ๋‹ค.

ํŒŒ์ด์ฌ์—๋Š” ๋‚ด์žฅ๋˜์–ด์žˆ๋Š” combinations ํ•จ์ˆ˜์™€ permutations ํ•จ์ˆ˜๊ฐ€ itertools ๋ชจ๋“ˆ์— ์กด์žฌํ•˜์ง€๋งŒ, ๋งŒ์•ฝ ์‹œ๊ฐ„์ดˆ๊ณผ๋ฅผ ๊ณ ๋ คํ•œ๋‹ค๋ฉด bruteforce๋ฅผ ํ†ตํ•ด ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.

boolean_check ๋ฅผ ๋‘์–ด N๊ฐœ์ค‘ M๊ฐœ ์ˆซ์ž๋ฅผ ๋ฝ‘์„ ๋•Œ ์ค‘๋ณต์„ ์ œ๊ฑฐํ–ˆ๋‹ค. 

check_list์„ set ๋ฆฌ์ŠคํŠธ๋กœ ๋งŒ๋“ค์–ด ํ•œ๋ฒˆ ๊ฒ€์‚ฌํ•œ ์ˆซ์ž๋Š” ๋‹ค์‹œํ•œ๋ฒˆ ์†Œ์ˆ˜ ๊ฒ€์‚ฌ๋ฅผ ํ•˜์ง€ ์•Š๋„๋ก ๊ตฌํ˜„ํ–ˆ๊ณ , 

์†Œ์ˆ˜ ์ธ์ง€ ํ™•์ธํ•˜๋Š” ํ•จ์ˆ˜์ธ prime์€ ์—๋ผํ† ์Šคํ…Œ๋„ค์Šค์˜ ์ฒด๋กœ ๊ตฌํ˜„ํ•  ๊นŒ ํ•˜๋‹ค , ๊ฐ€๋ณ๊ฒŒ ์†Œ์ˆ˜๊ฐ€ ์•„๋‹Œ ์กฐ๊ฑด๋“ค์„ ์ด์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ–ˆ๋‹ค.

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

    
def solution(numbers):
    list_n = list(numbers)
    answer = 0
    check_list = set()
    boolean_check = [False] * len(list_n)
    def bruteforce(num, idx):
        nonlocal answer
        if idx == len(list_n):
            if num == '':
                return
            if int(num) not in check_list:
                check_list.add(int(num))
                if prime(int(num)):
                    answer+=1
            
            return 
        
        
        for index ,i in enumerate(list_n):
            if boolean_check[index]:
                continue
            bruteforce('', idx+1)
            boolean_check[index] = True
            bruteforce(num+i , idx+1)
            boolean_check[index] = False
        
        
    bruteforce('', 0)  
    return answer

 

 

๋ฐ˜์‘ํ˜•