R
R
Ruslan Amerkhanov2018-02-20 00:00:27
Python
Ruslan Amerkhanov, 2018-02-20 00:00:27

How to take into account the totality of the sums of all elements of the list?

There is a set of variables ae. Provided that the sum of any two elements from the list is less than the specified value, the operation is performed. How to write a check for these sums without resorting to a boring type enumeration:

if a + b < x or \
    a + c < x or \:
    print('...')

Answer the question

In order to leave comments, you need to log in

3 answer(s)
L
longclaps, 2018-02-20
@nice-coding

Are you out of your mind, are you? The whole test is something - choose the two largest variables, add and compare.
What's up?..

S
Sergey Gornostaev, 2018-02-20
@sergey-gornostaev

from itertools import combinations

values_list = [1, 2, 3]
x = 4

if any(map(lambda v: v < x, map(sum, combinations(values_list, 2)))):
    print('...')

R
Ruslan Amerkhanov, 2018-02-21
@nice-coding

a = 1
b = 5
c = 9
x = 15
mxmGR = 0
mxmJR = 0
lst = [a, b, c]
mxm = max(lst)
mxmQnt = lst.count(mxm)

for i in lst:
    if mxmQnt >= 2:
        mxmGR, mxmJR = mxm, mxm
    else:
        mxmGR = mxm
        if mxmJR < i < mxmGR:
            mxmJR = i

if mxmGR + mxmJR < x:
    print('...')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question