반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 컴퓨터 비전
- recommender system
- 로지스틱 회귀
- Support Vector Machine
- OpenGL
- 파이썬
- pre-trained
- 그래픽스
- Regularization
- 컴퓨터 그래픽스
- 머신러닝
- CNN
- petal to metal
- 딥러닝
- 신경망
- logistic regression
- Vision
- SVM
- cs231n
- Unsupervised learning
- 추천 시스템
- 백준
- neural network
- Kaggle
- C++
- SGD
- 비용함수
- 인공지능
- CPP
- Computer Vision
Archives
- Today
- Total
kwan's note
순열과 조합 본문
반응형
itertools를 사용하지 않고 순열과 조합을 작성해 보았습니다.
다만 여기선 input을 튜플로 받아 중복을 해소하였습니다.
함수안에서 중복을 해소하는 방법도 있겠지만 최대한 간단하게 해결해보려고 했습니다.
def permutation(arr, r):
used = [0]*len(arr)
per2return=[]
def generate(item, used):
if len(item) == r:
per2return.append(item.copy())
return
# 3.
for i in range(len(arr)):
if not used[i]:
item.append(arr[i])
used[i] = 1
generate(item, used)
used[i] = 0
item.pop()
generate([], used)
return per2return
def combination(arr, r):
comb_return=[]
def generate(chosen):
if len(chosen) == r:
comb_return.append(chosen.copy())
return
start = arr.index(chosen[-1]) + 1 if chosen else 0
for i in range(start, len(arr)):
chosen.append(arr[i])
generate(chosen)
chosen.pop()
generate([])
return comb_return
T=int(input())
array=tuple(map(int,input().split()))
print("permutaion: ",permutation(array,T))
print("combination: ",combination(array,T))
반응형
'Algorithm > python' 카테고리의 다른 글
백준 20055번 컨베이어 벨트 위의 로봇 파이썬 (python code) (1) | 2021.04.26 |
---|---|
백준 - 14889번: 스타트와 링크 파이썬 (0) | 2021.04.26 |
백준 3190 번 뱀 파이썬 (python code) (0) | 2021.04.25 |
백준 2217번 로프 파이썬 (python code) (0) | 2021.01.21 |
백준 11758번 CCW 파이썬 (python code) (0) | 2021.01.17 |