jm_p_op

itertools 만들기,재귀, 경우의수 본문

수학/알고리즘

itertools 만들기,재귀, 경우의수

jm_p_op 2024. 3. 22. 10:00

코테중 함수명이 기억 안난다면 직접 만들어 써야 하니 연습(실제 코드 짤땐, 검색으로 찾아서 쓰면된다....)

def itertool(selection:list,lim:int,count=1,not_select=0) -> list:
	#미선택포함
    if count>lim:
        output=[[]]
    else:
        output=[]
    #선택만
    output=[[]]



	if lim>=count:
        for i in range(not_select,len(selection)):
        	type=i+1 # 비독립
            type=i # 중복제거
            type=0 # 독립
            for case in itertool(selection,lim,count=count+1,not_select=type):
                output.append([i]+case)
    return output
x=itertool([0,1,2],lim=3)
print(x)

 

 

 

(중복&미선택)

def itertool(selection:list,lim:int,count=1) -> list:
    output=[[]]
    if lim>=count:
        for i in range(0,len(selection)):
            for case in itertool(selection,lim,count=count+1):
                output.append([i]+case)
    return output
x=itertool([0,1,2],lim=2)
#return [[], [0], [0, 0], [0, 1], [0, 2], [1], [1, 0], [1, 1], [1, 2], [2], [2, 0], [2, 1], [2, 2]]

(중복)

def itertool(selection:list,lim:int,count=1) -> list:
    if count>lim:
        output=[[]]
    else:
        output=[]
    if lim>=count:
        for i in range(0,len(selection)):
            for case in itertool(selection,lim,count=count+1):
                output.append([i]+case)
    return output
x=itertool([0,1,2],lim=2)
print(x)
# [[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]]

(중복제거)

def itertool(selection:list,lim:int,count=1,not_select=0) -> list:
    if count>lim:
        output=[[]]
    else:
        output=[]
    if lim>=count:
        for i in range(not_select,len(selection)):
            for case in itertool(selection,lim,count=count+1,not_select=i):
                output.append([i]+case)
    return output
x=itertool([0,1,2],lim=2)
print(x)
# [[0, 0], [0, 1], [0, 2], [1, 1], [1, 2], [2, 2]]

(중복제거,비독립)

def itertool(selection:list,lim:int,count=1,not_select=0) -> list:
    if count>lim:
        output=[[]]
    else:
        output=[]
    if lim>=count:
        for i in range(not_select,len(selection)):
            for case in itertool(selection,lim,count=count+1,not_select=i+1):
                output.append([i]+case)
    return output
x=itertool([0,1,2],lim=2)
print(x)
#[[0, 1], [0, 2], [1, 2]]

 

'수학 > 알고리즘' 카테고리의 다른 글

KNN학습  (1) 2024.11.06
list.append to list  (1) 2024.03.25
백준 연구소 - 완전탐색 & 설계  (0) 2024.03.21
수치해석 - 피벗  (0) 2024.03.20
A*알고리즘 - 최단경로 알고리즘  (0) 2024.01.29