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 8751803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:07:02+00:00 2026-06-13T13:07:02+00:00

Hello I am currently getting an index out of range error from the following

  • 0

Hello I am currently getting an index out of range error from the following code: (I will post the code first and then the error)

Main File:

import Knapsack_Test

size = 10
W = 2*size
knapsack = Knapsack_Test.Knapsack_Test()

for i in range(1, 10):   

    knapsack.greedy_knapsack_test(size, W)  

    size = size + 10*i
    W = 2*size

Class File (Only the greedy function):

def greedy_knap(self, v, w, W):

    knap_array = []

    for i in range(1, len(v)):
        #The index out of range error occurs here:
        knap_array[i] = [v[i],w[i]] 

    sort_order = self.sort.merge_sort(knap_array)

    weight = 0
    value = 0

    knap_sac= []

    n = len(knap_array)
    j = 0
    profit = 0
    while weight < W and j < n:

        if weight + knap_array[i][1] <= W:

            knap_sac.append(knap_array[i])
            weight = weight + knap_array[i][1]
            profit = profit + knap_array[i][0]

        j = j + 1

    return profit

The test File (for greedy function):

def greedy_knapsack_test(self, size, W):
    v = []
    w = []
    for i in range(1,size):
        v.append(random.randint(1,1000))
    for i in range(1,size):
        w.append(random.randint(1,1000))
    start = time.time()
    self.knapsack.greedy_knap(v, w, W)
    end = time.time() - start
    return end

The Error:

Traceback (most recent call last):
  File "\\minerfiles.mst.edu\dfs\users\asadmb\Desktop\Programming 3\Knapsack_Main.py", line 10, in <module>
    knapsack.greedy_knapsack_test(size, W)
  File "\\minerfiles.mst.edu\dfs\users\asadmb\Desktop\Programming 3\Knapsack_Test.py", line 31, in greedy_knapsack_test
    self.knapsack.greedy_knap(v, w, W)
  File "\\minerfiles.mst.edu\dfs\users\asadmb\Desktop\Programming 3\KnapsackClass.py", line 30, in greedy_knap
    knap_array[i] = [v[i],w[i]]
IndexError: list assignment index out of range
  • 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-13T13:07:03+00:00Added an answer on June 13, 2026 at 1:07 pm
    knap_array = []
    
    for i in range(1, len(v)): #The index out of range error occurs here:
        knap_array.append([v[i],w[i]])
    

    you can’t create list element by referencing them.

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

Sidebar

Related Questions

Hello I currently have this code checking if a cookie exists, it works all
Hello everyone I'm currently having 2 issues with the code below: Upon return of
Hello Ruby/Rails/Merb developers! Im currently working on a web project that will have a
Possible Duplicate: “The breakpoint will not currently be hit. The source code is different
Hello Friends i m getting blocked with a confusing error its seems simple but
Hello Am Currently Doing An App Related To Sound Modulation I need to know
Hello Im currently writing a product syncronisation script for magento. I know how to
Hello I'm currently creating a mobile site for a business. And I wanted to
Hello I'm currently on Day 2 of Jeffrey Way's Codeigniter tutorial. In this screencast
Hello my friend is currently development an application using SOA Architecture, He sent me

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.