Home > Blockchain >  Creating a limit to find the sum of array values to a set number Numpy Python
Creating a limit to find the sum of array values to a set number Numpy Python

Time:10-27

I want to make a function where the sum of the Arrays and Arrays2 array is equivalent to val. The function should modify the Arrays and Arrays2 values so that the last index will output the sum of all values in the array to be val. How will be able to get the Expected Output?

import numpy as np 

Arrays = np.array([50, 30, 25, 87, 44, 68, 45])
Arrays2 = np.array([320])
val = 300

Expected output:

[50, 30, 25, 87, 44, 64]
[300]

CodePudding user response:

something like this?

import numpy as np 

Arrays = np.array([50, 30, 25, 87, 44, 68, 45])
Arrays2 = np.array([320])
val = 300


def thisRareFunction(arr):

    outArrays = []
    acum = 0
    for x in arr:
        acum  = x
        if acum <=val:
            outArrays.append(x)
        else:
            outArrays.append(x -(acum-val))
            break
    return outArrays

print(thisRareFunction(Arrays))
print(thisRareFunction(Arrays2))
  • Related