I'm trying to split a Integer into an "as symmetric array as possible" in python3 but just doesn't seem to get it right.
The desired input/output can be described by this Data Pattern Table.
# data pattern TABLE
# INPUT (int) => OUTPUT (len(array), array )
# 1 => 1 1
# 2 => 1 2
# 3 => 2 2,1
# 4 => 2 2,2
# 5 => 2 3,2
# 6 => 2 3,3
# 7 => 3 3,2,2
# 8 => 3 3,3,2
# 9 => 3 3,3,3
# 10 => 3 4,3,3
# 11 => 3 4,4,3
# 12 => 3 4,4,4
# 13 => 4 4,3,3,3
# 14 => 4 4,4,3,3
# 15 => 4 4,4,4,3
# 16 => 4 4,4,4,4
# 17 => 4 5,4,4,4
# 18 => 4 5,5,4,4
# 19 => 4 5,5,5,4
# 20 => 4 5,5,5,5
So far I got this code from looking around some on stackoverflow.
def splitnum(a, n):
num, div = a, n
return (num // div + (1 if x < num % div else 0) for x in range (div))
x =20
print(list(splitnum(x, int(x/2))))
However it results in a two-splitted array. I'm really just trying to understand the pattern properly and if someone know some neat trick to solve this it would be very helpful!
Any help is appreciated!
Cheers!