728x90
반응형

https://knowallworld.tistory.com/146

 

[Python] 순열, 조합, 중복순열, 중복조합(itertools이용한 백트래킹)

순열 ==> 순열은 순서를 고려하여 뽑는 경우의 수 https://knowallworld.tistory.com/142 [백준 파이썬 15649번]N과M★백트래킹★ 백트래킹이란?? - 길을 가다가 이 길이 아닌 것 같으면 왔던 길로 되돌아가 다

knowallworld.tistory.com

==> 참고!!!

 

1. 순열(Permutations)


==> 서로 다른 N개 중에서 R개를 선택하여 순서대로 나열

 

EX) A, B, C, D 4장 중 2장 선택 ==> 4P2 ==> 4 *3 = 12

 

itertools.permutations()

 

a= [i for i in range(4)]
permu = list(itertools.permutations(a ,2))
print(permu)
print(len(permu))

[(0, 1), (0, 2), (0, 3), (1, 0), (1, 2), (1, 3), (2, 0), (2, 1), (2, 3), (3, 0), (3, 1), (3, 2)]
12

 

EX-01) 

 

1> Factorial 

https://knowallworld.tistory.com/107

 

[백준 파이썬 10872번]팩토리얼★재귀★

import sys def fact(n): if n > 1: return n*fact(n-1) else : return 1 a = int(sys.stdin.readline()) print(fact(a)) # fact (10) = 10*fact(9) # fact(9) = 9*fact(8) # # # # fact(2) = 2*fact(1) # fact(1) = 1

knowallworld.tistory.com

==> 팩토리얼 참고

def fact(n):

    if n > 1:
        return n*fact(n-1)
    else :
        return 1
a = int(4)
print(fact(a))

 4! = 24

 

2> 5P5

 

print(len(list(itertools.permutations([i for i in range(5)], 5))))

5P5 = 5*4*3*2*1 = 120

 

3> 10P3

print(len(list(itertools.permutations([i for i in range(10)], 3))))

10P3 = 10*9*8 = 720

 

 

2. 조합(Combinations)

==> 서로 다른 N개 중에서R개를 택하여 순서를 무시하고 나열

 

EX) A, B, C, D 가 적힌 4장 중에서 2장의 카드를 택하여 순서 무시하고 책상 위에 나열하기

 

itertools.combinations()

 

1> 5C0

 

print(len(list(itertools.combinations([i for i in range(5)], 0))))

1

 

2> 5C5 

 

print(len(list(itertools.combinations([i for i in range(5)], 5))))

1

 

3> 5C2

print(len(list(itertools.combinations([i for i in range(5)], 2))))

 10

 

4> 5C3

print(len(list(itertools.combinations([i for i in range(5)], 3))))

10

 

5> 빨간공 4개, 파란 공 5개 ==> 4개 꺼내기

 

(1) 공의 색상 무시하고 공 4개 꺼내기

print(len(list(itertools.combinations([i for i in range(9)], 4))))

9C4 = 126

 

 

(2) 빨간 공 2개와 파란 공 2개 꺼내기

 

print(len(list(itertools.combinations([i for i in range(4)], 2))) * len(list(itertools.combinations([i for i in range(5)], 2))) )

4C2 * 5C2  = 60

 

 

 

6> 불량 2대 포함된 10대 TV중 3대 선택

 

(1) 10대의 TV 중 3대 선택

print(len(list(itertools.combinations([i for i in range(10)], 3))))

10C3 = 120

(2) 양호한 TV 3대 모두 선택

print(len(list(itertools.combinations([i for i in range(8)], 3))))

8C3 = 56

(3) 양호한 TV 2대와 불량품 1대 선택

 

print(len(list(itertools.combinations([i for i in range(2)], 1))) * len(list(itertools.combinations([i for i in range(8)], 2))) )

2C1 * 8C2 = 56

 

3. 이항정리(Binomial Theorem)

 

==> 두 항의 대수합의 거듭제곱을 전개하는 공식

 

이항정리

==> 이항계수 = () 로 표시

이항계수 = () 로 표시

파스칼 관계식

파스칼 관계식

파스칼 관계식-02

파스칼 관계식-02

출처 :  [쉽게 배우는 생활속의 통계학]  [북스힐 , 이재원] 

※혼자 공부 정리용

728x90
반응형

+ Recent posts