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

 

'다전공_컴퓨터공학 > C, JAVA, PYTHON' 카테고리의 다른 글

[JAVA] 6장 클래스  (0) 2021.01.14
[JAVA] 3장 연산자  (0) 2021.01.06
[JAVA] 2장 변수와 타입  (0) 2021.01.05
[C] struct, union  (0) 2020.12.03
[C] volatile  (0) 2020.12.01

+ Recent posts