B. Van Bogaert
B. Van Bogaert

Reputation: 11

CumSum until certain value reached and then stop and print 'MAX'

I have list of numbers (0-100)

numbers = list(range(101))

then I need to take the CumSum until 1000 is reached and then stop the calculation and print 'max is reached'

np.cumsum(numbers)

array([   0,    1,    3,    6,   10,   15,   21,   28,   36,   45,   55,
     66,   78,   91,  105,  120,  136,  153,  171,  190,  210,  231,
    253,  276,  300,  325,  351,  378,  406,  435,  465,  496,  528,
    561,  595,  630,  666,  703,  741,  780,  820,  861,  903,  946,
    990, 1035, 1081, 1128, 1176, 1225, 1275, 1326, 1378, 1431, 1485,
   1540, 1596, 1653, 1711, 1770, 1830, 1891, 1953, 2016, 2080, 2145,
   2211, 2278, 2346, 2415, 2485, 2556, 2628, 2701, 2775, 2850, 2926,
   3003, 3081, 3160, 3240, 3321, 3403, 3486, 3570, 3655, 3741, 3828,
   3916, 4005, 4095, 4186, 4278, 4371, 4465, 4560, 4656, 4753, 4851,
   4950, 5050], dtype=int32)

Now I got the CumSum of the whole list

I was thinking about,

0 > 1000 ==> No. continue

0+1 > 1000 ==> No. continue

...

990 + 45 > 1000 ==> Yes. print 'MAX VALUE REACHED', and stop the calculation

I expect something like this

array([   0,    1,    3,    6,   10,   15,   21,   28,   36,   45,   55,
         66,   78,   91,  105,  120,  136,  153,  171,  190,  210,  231,
        253,  276,  300,  325,  351,  378,  406,  435,  465,  496,  528,
        561,  595,  630,  666,  703,  741,  780,  820,  861,  903,  946,
        990])
MAX VALUE REACHED

Can someone help me to transform my idea into code?

thanks!

Upvotes: 0

Views: 1653

Answers (2)

B. Van Bogaert
B. Van Bogaert

Reputation: 11

I got something like this now:

def cumsum(x):
q = np.array(range (0,x))
z = q.cumsum()
v = np.clip(z, 0, 1000)
solution = ['MAX VALUE REACHED' if i == 1000 else i for i in v]
if x == 0:
    return[0]
else:
    return solution

My output is this

[0,
 1,
 3,
 6,
 10,
 15,
 21,
 28,
 36,
 45,
 55,
 66,
 78,
 91,
 105,
 120,
 136,
 153,
 171,
 190,
 210,
 231,
 253,
 276,
 300,
 325,
 351,
 378,
 406,
 435,
 465,
 496,
 528,
 561,
 595,
 630,
 666,
 703,
 741,
 780,
 820,
 861,
 903,
 946,
 990,
 'MAX VALUE REACHED',
 'MAX VALUE REACHED',
 'MAX VALUE REACHED',
 'MAX VALUE REACHED',
 'MAX VALUE REACHED',
 'MAX VALUE REACHED',
 'MAX VALUE REACHED',
 'MAX VALUE REACHED',
 'MAX VALUE REACHED',
 'MAX VALUE REACHED',
 'MAX VALUE REACHED',
 'MAX VALUE REACHED',
 'MAX VALUE REACHED',
 'MAX VALUE REACHED',
 'MAX VALUE REACHED',
 'MAX VALUE REACHED',
 'MAX VALUE REACHED',
 'MAX VALUE REACHED',
 'MAX VALUE REACHED',
 'MAX VALUE REACHED',
 'MAX VALUE REACHED',
 'MAX VALUE REACHED',
 'MAX VALUE REACHED',
 'MAX VALUE REACHED',
 'MAX VALUE REACHED',
 'MAX VALUE REACHED',
 'MAX VALUE REACHED',
 'MAX VALUE REACHED',
 'MAX VALUE REACHED',
 'MAX VALUE REACHED',
 'MAX VALUE REACHED',
 'MAX VALUE REACHED',
 'MAX VALUE REACHED',
 'MAX VALUE REACHED',
 'MAX VALUE REACHED',
 'MAX VALUE REACHED',
 'MAX VALUE REACHED',
 'MAX VALUE REACHED',
 'MAX VALUE REACHED',
 'MAX VALUE REACHED',
 'MAX VALUE REACHED',
 'MAX VALUE REACHED',
 'MAX VALUE REACHED',
 'MAX VALUE REACHED',
 'MAX VALUE REACHED',
 'MAX VALUE REACHED',
 'MAX VALUE REACHED',
 'MAX VALUE REACHED',
 'MAX VALUE REACHED',
 'MAX VALUE REACHED',
 'MAX VALUE REACHED',
 'MAX VALUE REACHED',
 'MAX VALUE REACHED',
 'MAX VALUE REACHED',
 'MAX VALUE REACHED']

Now I have to stop the calculation after one time 'MAX VALUE REACHED' Any sugestions?

Proposed alternative

Rather than using clip create a new list that contains only the cumsum items that are less than 1,000, and when you encounter an item that is at least 1,000 break out of the loop.

import numpy as np

q = np.array(range(101)) 
z = q.cumsum()
temp = []
for _ in z:
    if _ < 1000:
        temp.append(_)
    else:
        break
solution = np.array(temp)
print (solution)
print('MAX VALUE REACHED')

Output:

[  0   1   3   6  10  15  21  28  36  45  55  66  78  91 105 120 136 153
 171 190 210 231 253 276 300 325 351 378 406 435 465 496 528 561 595 630
 666 703 741 780 820 861 903 946 990]
MAX VALUE REACHED

Upvotes: 1

Bill Bell
Bill Bell

Reputation: 21663

I hope that, by now you have something like this, given Mr Clement's comment.

>>> total = 0
>>> i = 0
>>> while total < 1000:
...     total += i
...     i += 1
... 
>>> print (total)
1035

But someone should, sooner or later, suggest that you look carefully at the itertools library, as well as numpy. Your code can be written more compactly, like this.

>>> from itertools import count, accumulate, dropwhile
>>> next(dropwhile(lambda x: x<1000, accumulate(count())))
1035

This is known as functional programming. count is a so-called generator that produces a potentially infinite series of numbers, in this case, 0, 1, 2, 3, ... . accumulate produces the potentially infinite series of sums of these. dropwhile arranges to ignore numbers in this series that fail to satisfy the lambda condition. And finally next produces the next item in the resulting series of numbers.

Upvotes: 1

Related Questions