Given a set of N positive integers. It is necessary to choose from the set an arbitrary number of numbers so that their sum is as large as possible and not divisible by 6. In the answer, you must specify the number of selected numbers and their sum. If it is impossible to receive the necessary sum, it is considered that 0 numbers are chosen 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] #строка 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 is the IndexError: list index out of range error on line 8 issued every time the number 6 is entered into array a? Explain, please, simple language to a novice. You can also fix some clumsy code. In general, the program works with the exception of this case with a six.

  • Mark in the code where the line is 8. - pank

3 answers 3

itertools.combinations is an iterator that returns all possible sequences of r elements taken from the n_ object to be iterated. Sequences are given in lexicographical sorting order. If the input iterator is sorted, the sequences will be created in sorted order. Items are considered unique depending 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)) 
  • In general, I did not understand anything, I hope this should help me. - seregqa
  • This is a solution to your problem - vadim vaduxa

The error occurs due to the fact that you are inside the loop passing through the list, reduce the list itself. As a result, at some point it turns out that the index j goes beyond the real boundaries of the list. In the general case, it is not recommended to change the list of the cycle that the cycle goes through (at least not to change its dimensions), otherwise you will try to catch many incomprehensible errors for a long time.

If the problem condition changes the “arbitrary number of numbers” to “the maximum number of numbers”, the algorithm will be as follows:

  1. There is a source list, sort it
  2. We consider 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 the minimum element from the list, not a multiple of 6, as a result, the sum of the list becomes not a multiple of 6. We 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) # п. 1 if sum(s) % 6 != 0: # п. 2 return s for i, item in enumerate(s): # п. 3 if item % 6 != 0: del s[i] return s return [] # п. 4 s = [random.randrange(9) for _ in range(10)] print(s, sum(s)) # Пример вывода: [7, 8, 3, 1, 6, 3, 6, 7, 6, 1] 48 x = sum_not_mul_6(s) print(x, sum(x)) # Пример вывода: [1, 3, 3, 6, 6, 6, 7, 7, 8] 47 

You can use itertools.combinations , as in itertools.combinations , to display all variants of combinations of list items whose sum is not a multiple of 6.

    Whoever has such a problem, in the for loop you need to use not sum = a[j] , but simply sum = j .