[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค/python] ์™„์ „ ํƒ์ƒ‰ : ์†Œ์ˆ˜ ์ฐพ๊ธฐ

2021. 4. 2. 15:42

๋ฌธ์ œ ์„ค๋ช…

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

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

 

์ œํ•œ ์‚ฌํ•ญ

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

 

์ •๋‹ต

import itertools

def solution(numbers):
    answer = {}
    numbers = list(numbers)

    # ํฐ ์ˆ˜๋ถ€ํ„ฐ ์ •๋ ฌ
    numbers.sort(reverse=True)

    # ๊ฐ€์žฅ ํฐ ์ˆ˜ ์กฐํ•ฉ
    max_number = ""
    for i in numbers:
        max_number += str(i)
    max_number = int(max_number)

    # ์†Œ์ˆ˜ True๋กœ ์ €์žฅํ•˜๊ธฐ
    idx = [True for i in range(max_number + 1)]
    idx[0] = False
    idx[1] = False
    for i in range(2, int(max_number**0.5)+1):
        if idx[i]:
            for j in range(2*i, max_number+1, i):
                idx[j] = False
	
    # ์ˆซ์ž ์กฐํ•ฉ ์ฐพ๊ธฐ
    for i in range(1, len(numbers)+1):
        target = list(map(''.join, itertools.permutations(numbers, i)))
        for j in target:
            if int(j) not in answer:
                answer[int(j)] = idx[int(j)]

    result = 0
    for v in answer.values():
        if v == True:
            result += 1
    return result

numbers = list(numbers)

# ํฐ ์ˆ˜๋ถ€ํ„ฐ ์ •๋ ฌ
numbers.sort(reverse=True)

# ๊ฐ€์žฅ ํฐ ์ˆ˜ ์กฐํ•ฉ
max_number = ""
for i in numbers:
    max_number += str(i)
max_number = int(max_number)

๋ฐ›์€ ์ˆซ์ž๋ฆฌ์ŠคํŠธ๋ฅผ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜์—ฌ

์กฐํ•ฉํ•˜์—ฌ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๋Š” ๊ฐ€์žฅ ํฐ ์ˆ˜์ธ max_number๋ฅผ ๊ตฌํ•œ๋‹ค.

 

# ์†Œ์ˆ˜ True๋กœ ์ €์žฅํ•˜๊ธฐ
idx = [True for i in range(max_number + 1)]
idx[0] = False
idx[1] = False
for i in range(2, int(max_number**0.5)+1):
    if idx[i]:
        for j in range(2*i, max_number+1, i):
            idx[j] = False

์ „์— ํ’€์—ˆ๋˜ ์†Œ์ˆ˜๊ตฌํ•˜๊ธฐ ๋ฌธ์ œ์˜ ์—๋ผํ† ์Šคํ…Œ๋„ค์Šค์˜ ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ max_number๊นŒ์ง€์˜ ์†Œ์ˆ˜๋ฅผ ๊ตฌํ•œ๋‹ค.

์†Œ์ˆ˜๊ตฌํ•˜๊ธฐ ๋ฌธ์ œ์™€ ์—๋ผํ† ์Šคํ…Œ๋„ค์Šค์˜ ์ฒด ํฌ์ŠคํŒ…์„ ์•„๋ž˜์— ๊ฑธ์–ด๋‘์—ˆ๋‹ค.

2021.03.09 - [๐Ÿ”์•Œ๊ณ ๋ฆฌ์ฆ˜/๋ฐฑ์ค€] - [๋ฐฑ์ค€/python] 1929๋ฒˆ : ์†Œ์ˆ˜ ๊ตฌํ•˜๊ธฐ

 

[๋ฐฑ์ค€/python] 1929๋ฒˆ : ์†Œ์ˆ˜ ๊ตฌํ•˜๊ธฐ

๋ฌธ์ œ M์ด์ƒ N์ดํ•˜์˜ ์†Œ์ˆ˜๋ฅผ ๋ชจ๋‘ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. ์ž…๋ ฅ ์ฒซ์งธ ์ค„์— ์ž์—ฐ์ˆ˜ M๊ณผ N์ด ๋นˆ ์นธ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ฃผ์–ด์ง„๋‹ค. (1 ≤ M ≤ N ≤ 1,000,000) M์ด์ƒ N์ดํ•˜์˜ ์†Œ์ˆ˜๊ฐ€ ํ•˜๋‚˜ ์ด์ƒ ์žˆ๋Š” ์ž…

ye333.tistory.com

2021.03.10 - [๐Ÿ”์•Œ๊ณ ๋ฆฌ์ฆ˜/๐Ÿ”…๊ฐœ๋…์ •๋ฆฌ] - [ํŒŒ์ด์ฌ/python] ์—๋ผํ† ์Šคํ…Œ๋„ค์Šค์˜ ์ฒด

 

[ํŒŒ์ด์ฌ/python] ์—๋ผํ† ์Šคํ…Œ๋„ค์Šค์˜ ์ฒด

์—๋ผํ† ์Šคํ…Œ๋„ค์Šค์˜ ์ฒด๋Š” ๊ณ ๋Œ€ ๊ทธ๋ฆฌ์Šค ์ˆ˜ํ•™์ž์ธ ์—๋ผํ† ์Šคํ…Œ๋„ค์Šค๊ฐ€ ๊ณ ์•ˆํ•ด ๋‚ธ ์†Œ์ˆ˜๋ฅผ ์ฐพ๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. ๋งˆ์น˜ ์ฒด๋กœ ๊ฑธ๋Ÿฌ๋‚ด๋“ฏ์ด ์ˆ˜๋ฅผ ๊ฑธ๋Ÿฌ๋‚ธ๋‹ค๊ณ  ํ•˜์—ฌ ๋ถ™์—ฌ์ง„ ์ด๋ฆ„์ด๋‹ค. ์ž„์˜์˜ ์ž์—ฐ์ˆ˜ n์— ๋Œ€ํ•ด ์†Œ์ˆ˜

ye333.tistory.com

 

# ์ˆซ์ž ์กฐํ•ฉ ์ฐพ๊ธฐ
    for i in range(1, len(numbers)+1):
        target = list(map(''.join, itertools.permutations(numbers, i)))
        for j in target:
            if int(j) not in answer:
                answer[int(j)] = idx[int(j)]

numbers ๋ฆฌ์ŠคํŠธ์— ์žˆ๋Š” ์ˆซ์ž๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ์ˆซ์ž ์กฐํ•ฉ์„ ์ฐพ๋Š”๋‹ค.

ํ•œ์ž๋ฆฌ ์ˆซ์ž๋ถ€ํ„ฐ numbers์˜ ๋ชจ๋“  ์ˆซ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์กฐํ•ฉ๊นŒ์ง€ ๋ฐ˜๋ณต๋ฌธ์„ ํ†ตํ•ด ๊ตฌํ•œ๋‹ค.

itertools์˜ permutations๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ˆœ์—ด์„ ์‰ฝ๊ฒŒ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.

์•„๋ž˜๋Š” itertools ๊ด€๋ จ ํฌ์ŠคํŒ…์ด๋‹ค.

2021.04.01 - [๐Ÿ”์•Œ๊ณ ๋ฆฌ์ฆ˜/๐Ÿ”…๊ฐœ๋…์ •๋ฆฌ] - [ํŒŒ์ด์ฌ/python] ์ˆœ์—ด๊ณผ ์กฐํ•ฉ : itertools ์ด์šฉํ•˜๊ธฐ (permutations, combinations)

 

[ํŒŒ์ด์ฌ/python] ์ˆœ์—ด๊ณผ ์กฐํ•ฉ : itertools ์ด์šฉํ•˜๊ธฐ (permutations, combinations)

itertools๋ฅผ ์ด์šฉํ•˜์—ฌ ์ˆœ์—ด๊ณผ ์กฐํ•ฉ์„ ๊ตฌํ˜„ํ•˜๋ฉด ๋”์šฑ ๋น ๋ฅด๊ฒŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค. 1. ์ˆœ์—ด : permutations(N, r) N์—์„œ ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๊ณ , r๊ฐœ๋ฅผ ๋ฝ‘์•„ ์ˆœ์„œ๋Œ€๋กœ ๋‚˜์—ดํ•œ๋‹ค. ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์—, ๋ฝ‘

ye333.tistory.com

answer์— ํ•ด๋‹น ์กฐํ•ฉ์˜ ์ˆซ์ž๊ฐ€ ์—†๋‹ค๋ฉด, answer ๋”•์…”๋„ˆ๋ฆฌ์— ํ•ด๋‹น idx ๊ฐ’๊ณผ ํ•จ๊ป˜ ๋„ฃ์–ด์ค€๋‹ค.

๋”•์…”๋„ˆ๋ฆฌ์— ๋„ฃ๋Š” ์ด์œ ๋Š” 11, 011์ฒ˜๋Ÿผ ๊ฐ™์€ ์ˆ˜๊ฐ€ ์ค‘๋ณตํ•ด์„œ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

answer์— ์ €์žฅ๋œ key์˜ value๊ฐ€ True๋ผ๋ฉด key๊ฐ€ ์†Œ์ˆ˜๋ผ๋Š” ๋œป์ด๊ณ , False๋ผ๋ฉด ์†Œ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ผ๋Š” ๋œป์ด๋‹ค.

 

result = 0
    for v in answer.values():
        if v == True:
            result += 1
    return result

answer์— ์ €์žฅ๋œ value๊ฐ€ True์ธ key๋ฅผ ์„ธ์–ด result์— ์ €์žฅํ•œ ํ›„ result๋ฅผ returnํ•œ๋‹ค.

728x90

BELATED ARTICLES

more