将整数分成 "as symmetric array as possible" in python3
Divide integer into an "as symmetric array as possible" in python3
我试图在 python3 中将整数拆分为 "as symmetric array as possible",但似乎没有正确处理。
所需的 input/output 可以用此数据模式 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
到目前为止,我通过在 Whosebug 上查看一些代码得到了这段代码。
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))))
然而,它会导致两个拆分数组。我真的只是想正确地理解模式,如果有人知道一些巧妙的技巧来解决这个问题,那将非常有帮助!
感谢任何帮助!
干杯!
我解决了这个问题。不是一个绝妙的解决方案,但有效。代码:
def get_dim_arr(x):
"""
Get even divided array of integer X
@Param X integer
@Return [len(arr), arr], where arr contains number of columns per row
"""
curr = 1
res_arr = [1]
while curr < x:
val, idx = min((val, idx) for (idx, val) in enumerate(res_arr))
print(res_arr)
if(val > len(res_arr)): # if abs(row, col) >= 1
# split
temp = []
for i in range(0,len(res_arr)):
if i == 0:
temp.append(res_arr[i])
else:
temp.append(res_arr[i]-1)
temp.append(res_arr[i]-1)
res_arr = temp
else:
val+=1
res_arr[idx] = val
curr+=1
return [len(res_arr), res_arr]
x = 20
print(get_dim_arr(x))
结果:
[1]
[2]
[2, 1]
[2, 2]
[3, 2]
[3, 3]
[3, 2, 2]
[3, 3, 2]
[3, 3, 3]
[4, 3, 3]
[4, 4, 3]
[4, 4, 4]
[4, 3, 3, 3]
[4, 4, 3, 3]
[4, 4, 4, 3]
[4, 4, 4, 4]
[5, 4, 4, 4]
[5, 5, 4, 4]
[5, 5, 5, 4]
[4, [5, 5, 5, 5]]
我认为这是一个不错的提议:
import math
def squarit(n: int):
size = int(math.floor(math.sqrt(n)))
ans = [n//size]*size
for i in range(n-n//size*size):
ans[i] += 1
return size, ans
for i in range(4, 25):
print(f'{i}: {squarit(i)}')
结果:
4: (2, [2, 2])
5: (2, [3, 2])
6: (2, [3, 3])
7: (2, [4, 3])
8: (2, [4, 4])
9: (3, [3, 3, 3])
10: (3, [4, 3, 3])
11: (3, [4, 4, 3])
12: (3, [4, 4, 4])
13: (3, [5, 4, 4])
14: (3, [5, 5, 4])
15: (3, [5, 5, 5])
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])
21: (4, [6, 5, 5, 5])
22: (4, [6, 6, 5, 5])
23: (4, [6, 6, 6, 5])
24: (4, [6, 6, 6, 6])
我试图在 python3 中将整数拆分为 "as symmetric array as possible",但似乎没有正确处理。
所需的 input/output 可以用此数据模式 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
到目前为止,我通过在 Whosebug 上查看一些代码得到了这段代码。
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))))
然而,它会导致两个拆分数组。我真的只是想正确地理解模式,如果有人知道一些巧妙的技巧来解决这个问题,那将非常有帮助!
感谢任何帮助!
干杯!
我解决了这个问题。不是一个绝妙的解决方案,但有效。代码:
def get_dim_arr(x):
"""
Get even divided array of integer X
@Param X integer
@Return [len(arr), arr], where arr contains number of columns per row
"""
curr = 1
res_arr = [1]
while curr < x:
val, idx = min((val, idx) for (idx, val) in enumerate(res_arr))
print(res_arr)
if(val > len(res_arr)): # if abs(row, col) >= 1
# split
temp = []
for i in range(0,len(res_arr)):
if i == 0:
temp.append(res_arr[i])
else:
temp.append(res_arr[i]-1)
temp.append(res_arr[i]-1)
res_arr = temp
else:
val+=1
res_arr[idx] = val
curr+=1
return [len(res_arr), res_arr]
x = 20
print(get_dim_arr(x))
结果:
[1]
[2]
[2, 1]
[2, 2]
[3, 2]
[3, 3]
[3, 2, 2]
[3, 3, 2]
[3, 3, 3]
[4, 3, 3]
[4, 4, 3]
[4, 4, 4]
[4, 3, 3, 3]
[4, 4, 3, 3]
[4, 4, 4, 3]
[4, 4, 4, 4]
[5, 4, 4, 4]
[5, 5, 4, 4]
[5, 5, 5, 4]
[4, [5, 5, 5, 5]]
我认为这是一个不错的提议:
import math
def squarit(n: int):
size = int(math.floor(math.sqrt(n)))
ans = [n//size]*size
for i in range(n-n//size*size):
ans[i] += 1
return size, ans
for i in range(4, 25):
print(f'{i}: {squarit(i)}')
结果:
4: (2, [2, 2])
5: (2, [3, 2])
6: (2, [3, 3])
7: (2, [4, 3])
8: (2, [4, 4])
9: (3, [3, 3, 3])
10: (3, [4, 3, 3])
11: (3, [4, 4, 3])
12: (3, [4, 4, 4])
13: (3, [5, 4, 4])
14: (3, [5, 5, 4])
15: (3, [5, 5, 5])
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])
21: (4, [6, 5, 5, 5])
22: (4, [6, 6, 5, 5])
23: (4, [6, 6, 6, 5])
24: (4, [6, 6, 6, 6])