강의

멘토링

커뮤니티

Inflearn Community Q&A

hyexxxmi5095's profile image
hyexxxmi5095

asked

Introduction to Python Algorithm Problem Solving (Coding Test Preparation)

11. Combination of Numbers (DFS)

조합만들기처럼 res 리스트를 만들어서

Written on

·

198

0

안녕하세요 저는 sum을 인자로 두지않고, 조합만들기처럼 res 리스트를 만들어서 진행했습니다.

import sys
sys.stdin = open("input.txt", 'r')

def dfs(L, s):
    global cnt
    if L==k and sum(res) % m ==0:
        cnt+=1
    else:
        for i in range(s, n):
            res[L] = num[i]
            dfs(L+1, i+1)

if __name__ == "__main__":
    n, k = map(int, input().split())
    num = list(map(int, input().split()))
    m = int(input())
    cnt = 0
    res = [0]*(k)
    dfs(0,0)
    print(cnt)

10. 조합만들기에서 총합 개념만 조금 더해서 만든 코든데 계속 IndexError: list assignment index out of range가 납니다.. 왜 그런지 모르겠어요..

코테 준비 같이 해요! python

Answer 1

0

codingcamp님의 프로필 이미지
codingcamp
Instructor

안녕하세요^^

if L==k and sum(res) % m ==0:  //이 조건문이 잘못되어 L이 k보다 커지게 됩니다.

hyexxxmi5095's profile image
hyexxxmi5095

asked

Ask a question