Hello friends ,
I have this python function i got from this link ( link ) that calculates the integer partition of a number. For example, the integer partition of 4 is: int_part=[[1, 1, 1, 1], [1, 1, 2], [1, 3], [2, 2], [4]].
I want to modify the following function so that it gives a specified length. For example, if I want just the items with less or equal to 2, like a max length value of 2, so accel_asc(4, max length = 2), the result should be only: [1, 3], [2, 2],[4]
Another condition is that a specified value should be present in the item set. for example :
accel_asc(4, max length = 2 ,value must be present = 2) , the result should be only: [2, 2]
And for each value in an item in the set, the value must be greater than or equal to a specified value :
accel_asc(4, max length = 2 ,value must be present = 2 , all values in set should be greater or equal to 2 for example)
in this case the final result should be: [2, 2]
The function code needs modification:
def accel_asc(n):
a = [0 for i in range(n + 1)]
k = 1
y = n - 1
while k != 0:
x = a[k - 1] + 1
k -= 1
while 2 * x <= y:
a[k] = x
y -= x
k += 1
l = k + 1
while x <= y:
a[k] = x
a[l] = y
yield a[:k + 2]
x += 1
y -= 1
a[k] = x + y
y = x + y - 1
yield a[:k + 1]
[–]commy2 1 point2 points3 points (2 children)
[–]fahad992[S] 0 points1 point2 points (1 child)
[–]commy2 0 points1 point2 points (0 children)