I'm having trouble with an algorithm in python. I have an array of 4 values [a,b,c,d] those are percentages so at any given time a+b+c+d=1. I need a loop that goes through all possible combinations of these numbers with a stepsize of 0.1. Example:
[0,0,0,1]
[0,0,0.1,0.9]
[0,0.1,0.1,0.8]
[0.1,0.1,0.1,0.7]
.....
[0.1,0.1,0.2,0.6]
[0.1,0.1,0.3,0.5]
....
[1,0,0,0]
I created a code that seems to overflow... any help? ty I Know its a noob question...
def frange(start, stop, step):
while start <= stop:
yield start
start += step
def distribuir(p,array):
if len(array) == 3:
array.append(p)
print(Array)
return
for i in frange(0,1,0.1):
temp = []
temp.append(array)
temp.append(i)
distribuir(p-i,temp)
0.0through1.0by a step size of0.1, consider what happens if you dorange(10)and multiply each value by0.1. Once you have that, you should know how to write the next loop down (hint: if the first value is0.4, then the next value only has to loop from0.0to0.6), and the next, and the next. If you get stuck somewhere, you can post your code and where you're stuck.0s, and treating1, 1, 1, 7and7, 1, 1, 1as the same partition, all of which are different from the desired output here.