E
E
egeriev2019-03-18 17:48:19
Python
egeriev, 2019-03-18 17:48:19

How to make the algorithm repeatedly execute until some condition is met?

I have an algorithm:

n = int(input())
arr = [ int(input()) for i in range(n) ]
teaMin = int(input())

sm = sum(arr)
sums = set()
sums.add(0)

arr.sort()

for item in arr:
    for k in range(sm, min(arr) - 1, -1):
        if (k - item) in sums:
            sums.add(k)

It finds all combinations of adding elements in an array. I need it to work until an element appears in this list, which is divisible by the number k without a remainder (the number k is entered by the user).
Example: user entered 16 in k
[2,3,5]

At the exit:
[2, 3, 5, (5+2)7, (5+3)8, ((2+3)+5)10, (there is no element that satisfies the condition, so the second circle)(10+2)12 , (10+3)13, (10+5)15 ... 30, (30+2)32

At 32, the search for combinations should stop, because 32% 16 = 0
I did not write down the repeating e-mails

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question