본문 바로가기

프로그래밍/Python

반복하지 않는 수, 중복되지 않는 수, 0부터 9까지 순열 만들기 코드

728x90
반응형

0부터 9까지 총 10개의 숫자로 만들 수 있는 순열의 개수는 8877690개이다.

아래 코드는 반복하지 않는 수를 txt 파일로 저장하는 파이썬 코드이다.

from itertools import permutations, chain, islice
import math


def make_no_repeats(length):
    my_iter = permutations(range(10), length)
    no_zero_start_index = math.perm(9, length - 1)
    no_lead_zeros = islice(my_iter, no_zero_start_index, None)

    return no_lead_zeros


if __name__ == '__main__':
    my_list = []
    iter_no_repeats = chain(*map(make_no_repeats, range(1, 11)))
    while True:
        try:
            my_list.append(int(''.join(map(str, next(iter_no_repeats)))))
        except StopIteration:
            break

    print(len(my_list))
    with open('test.txt', 'w') as f:
        f.write(str(my_list))

 

https://drive.google.com/file/d/1rX3Ohzw39R0q2Iv4AhPJg6swxDdczjDc/view?usp=sharing 

 

반복하지않는수.txt

 

drive.google.com

 

위 링크는 반복하지 않는 수를 txt 파일로 저장한걸 구글 드라이브로 올려뒀다... 이거 그냥 게시글에 복붙하니까 안되서 그냥 심심한김에 올려본다

728x90
반응형