【发布时间】:2020-05-16 00:52:56
【问题描述】:
我正在尝试编写代码,该代码将返回可能分解的大量 3,最多 3 位数字。
该号码由num=str([0-999])+str([0-999])+str([0-999]) 组成。所有组件都是独立且随机的。
例如,'1111' 的预期输出将是:[[1,11,1],[11,1,1],[1,1,11]]。
到目前为止我写的代码:
def isValidDecomp(num,decmp,left):
if(len(num)-len(decmp)>=left):
if(int(decmp)<999):
return True
return False
def decomp(num,length,pos,left):
"""
Get string repping the rgb values
"""
while (pos+length>len(num)):
length-=1
if(isValidDecomp(num,num[pos:pos+length],left)):
return(int(num[pos:pos+length]))
return 0 #additive inverse
def getDecomps(num):
length=len(num)
decomps=[[],[],[]]
l=1
left=2
for i in range(length):
for j in range(3):
if(l<=3):
decomps[j].append(decomp(num,l,i,left))
l+=1
if(l>3): #check this immediately
left-=1
l=1#reset to one
return decomps
d=getDecomps('11111')
print( d)
我的代码在不同情况下的(不正确的)输出:
input,output
'11111', [[1, 1, 1, 1, 1], [11, 11, 11, 11, 1], [111, 111, 111, 11, 1]]
'111', [[1, 1, 1], [0, 11, 1], [0, 11, 1]]
'123123145', [[1, 2, 3, 1, 2, 3, 1, 4, 5], [12, 23, 31, 12, 23, 31, 14, 45, 5], [123, 231, 0, 123, 231, 0, 145, 45, 5]]
谁能告诉我我做错了什么?
【问题讨论】:
标签: python python-3.x string list split