Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8560473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:17:01+00:00 2026-06-11T16:17:01+00:00

Consider the following code in python: def main(): n = input( ) s =

  • 0

Consider the following code in python:

def main():
    n = input( )
    s = input( )

    coins = [ 0 ]*n
    dp = [ 0 ]*( s+1 )

    print coins
    print dp

    for i in range( n ) :
        coins[ i ] = input( )

    dp[ 0 ]=0
    dp[ 1 ]=1

    for i in range( 2, s+1 ) :
        min_val = 999999
        for j in range( 0, n ) :
            if i-coins[ j ] > 0 :
                if dp[ i-coins[ j ] ] + 1 < min_val :
                    min_val = dp[ i-coins[ j ] ] + 1


    print coins
    print dp

    print coins[ s ]

if __name__ == "__main__" :
    main()

When I compile and run this program, I get the following runtime error:

File "test.py", line 33, in <module>
  main();
File "test.py", line 30 in main
  if dp[ i-coins[ j ] ] + 1 < min_val :

IndexError: list index out of range

What’s wrong with it?

Input:

5 10
1 3 5 7 9
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T16:17:02+00:00Added an answer on June 11, 2026 at 4:17 pm

    try to use

    coins = [ 0 ]*n
    dp = [ 0 ]*( s+1 )
    

    to init the array.

    I got an total different error on my machine:

      File "ttt.py", line 31, in <module>
        main()
      File "ttt.py", line 21, in main
        if dp[ i-coins[ j ] ] + 1 < min_val :
    TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
    

    and print dp[s] rather than print coins[s] at the last line.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

consider the following python code: import gtk class MainWindow(): def __init__(self): self.window = gtk.Window()
consider the following python code class Base(object): def __init__(self): self.foo = 5 class Derived(Base):
Consider the following Python (3.x) code: class Foo(object): def bar(self): pass foo = Foo()
Consider the following Python code: import os print os.getcwd() I use os.getcwd() to get
Consider the following simple python code >>> L = range(3) >>> L [0, 1,
Consider the following python code: class Foo(object): def __init__(self, value): self._value = value @property
Consider the following example code class A: def __init__(self, i): self.i = i print("Initializing
Consider the following code: class Base(object): @classmethod def do(cls, a): print cls, a class
consider I have the following code in python module a.py : def func(): obj
Consider the following Python code: def f(*args): for a in args: pass foo =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.