nueoyhk 2021. 1. 8. 13:12

dojang.io/mod/page/view.php?id=2365

 

파이썬 코딩 도장: 33.2 함수 안에서 함수 만들기

이번에는 함수 안에서 함수를 만드는 방법을 알아보겠습니다. 다음과 같이 def로 함수를 만들고 그 안에서 다시 def로 함수를 만들면 됩니다. def 함수이름1():     코드     def 함수이름2():    

dojang.io

 

 

앞으로 많이 사용할 것 같다 (프로그래머스)

 

[BFS]
https://programmers.co.kr/learn/courses/30/lessons/43165

def solution(numbers, target):
    def dfs(index, include, not_include):
        print(include, not_include)
        if not not_include and sum(include) == target:
            nonlocal answer
            answer += 1
        if not not_include:
            # not_include가 비어있는 경우
            return

        value = numbers[index]
        dfs(index+1, include+[value], numbers[index+1:])    # + 부호
        dfs(index+1, include+[-value], numbers[index+1:])   # - 부호

    answer = 0
    dfs(0, [], numbers)
    return answer