mardi 2 août 2022

Split whole number into float numbers

Goal: split 100 into 5 random 2 decimal place numbers.

So far, I can simulate any number of divisions.

However, these are only integers and are "balanced", in that they are the same or close in values to each other. So, the output is always the same.

Code:

def split(x, n):
 
    if(x < n):
        print(-1)
 
    elif (x % n == 0):
        for i in range(n):
            print(x//n, end =" ")
    else:
        zp = n - (x % n)
        pp = x//n
        for i in range(n):
            if(i>= zp):
                print(pp + 1, end =" ")
            else:
                print(pp, end =" ")
       
split(100, 5)
>>> 20 20 20 20 20 

Desired Output:

  • List of numbers,
  • Floating point numbers (2 dp),
  • Non-balanced.

Example Desired Output:

[10.50, 22.98, 13.23, 40.33, 12.96]



Aucun commentaire:

Enregistrer un commentaire