Home python Problem with IndexError: list index out of range

Problem with IndexError: list index out of range

Author

Date

Category

A set of N positive integers is given. It is necessary to choose an arbitrary number of numbers from the set so that their sum is as large as possible and at the same time is not divisible by 6. In the answer you need to indicate the number of selected numbers and their sum.
If it is impossible to get the required amount, it is considered that 0 numbers are selected and their sum is equal to 0.

a = []
N = int (input ())
maxsum = 0
for i in range (N):
  a.append (int (input ()))
a.sort (reverse = True)
for j in range (N):
  sum = a [j] # line 8
  maxsum + = sum
  if maxsum% 6 == 0 and a! = []:
    a.sort ()
    a.pop (0)
    sum = maxsum = 0
    for o in range (len (a)):
      sum = a [o]
      maxsum + = sum
if maxsum% 6! = 0:
  print (len (a), maxsum)

Why the error IndexError: list index out of range on line 8 is thrown every time a is entered into the array 6 ? Explain, please, in simple language to a beginner. You can also fix some clumsy code. In general, the program works with the exception of this case with a six.


Answer 1, authority 100%

itertools.combinations
an iterator that returns all possible sequences of r elements taken from the iterable n_. Sequences are listed in lexicographic sort order. If the input iterator is sorted, the sequences will be created in sorted order.
Items are considered unique based on their position.

import itertools, random
def combinator (n_: list):
n_.sort (reverse = True)
  for r in reversed (range (len (n _) + 1)):
    for n in itertools.combinations (n_, r):
      sn = sum (n)
      if sn% 6:
        return len (n), sn
N = [random.randrange (9) for _ in range (10)]
print (combinator (N))

Answer 2, authority 67%

The error occurs due to the fact that you, inside a loop traversing the list, reduce the list itself. As a result, at some point it will turn out that the index j goes beyond the real boundaries of the list. In general, it is not recommended to change the list that the loop goes through in a loop (at least not to resize it), otherwise you will be trying for a long time to catch numerous incomprehensible errors.

If, in the problem statement, change the “arbitrary number of numbers” to “the maximum number of numbers”, then the algorithm will be as follows:

  1. There is an original list, let’s sort it
  2. We calculate the sum of all elements – if it is not a multiple of 6, then we return the entire list
  3. If the sum is a multiple of 6, we remove from the list the minimum element that is not a multiple of 6, as a result, the sum of the list becomes not a multiple of 6. Immediately return the remaining elements.
  4. If all elements of the list are multiples of 6, then return an empty list.

Implementation:

import random
def sum_not_mul_6 (s):
  s = sorted (s) # p. 1
  if sum (s)% 6! = 0: # p. 2
    return s
  for i, item in enumerate (s): # p. 3
    if item% 6! = 0:
      del s [i]
      return s
  return [] # p. 4
s = [random.randrange (9) for _ in range (10)]
print (s, sum (s)) # Example output: [7, 8, 3, 1, 6, 3, 6, 7, 6, 1] 48
x = sum_not_mul_6 (s)
print (x, sum (x)) # Sample output: [1, 3, 3, 6, 6, 6, 7, 7, 8] 47

You can use itertools.combinations to display all list item combinations that are not a multiple of 6, as in vadim vaduxa .


Answer 3

If you have this problem, you should not use sum = a [j] in your for loop, but simply sum = j .

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions