假设我有一个数字3523014作为字符串。我怎样才能找到以列表形式组合的所有子字符串集合,这些字符串加起来表示一个数字。我已经编写了一个代码,但是我只给出了一些线性搜索的子字符串输出。
请修改代码 -
def find_ten_substring(num_str):
str1=""
list1=[]
a=0
for i in range(0,len(num_str)):
a=a+int(num_str[i])
str1+=str(num_str[i])
if(a==10):
a=0
list1.append(str1)
str1=""
return(list1)
num_str="3523014"
print("The number is:",num_str)
result_list=find_ten_substring(num_str)
print(result_list)
结果来自[' 352']。 预期的输出应该是['                       
答案 0 :(得分:1)
这将解决您的问题......
def subset_sum(numbers, target, partial=[]):
s = sum(partial)
# check if the partial sum is equals to target
if s == target:
print "sum(%s)=%s" % (partial, target)
if s >= target:
return # if we reach the number why bother to continue
for i in range(len(numbers)):
n = numbers[i]
remaining = numbers[i+1:]
subset_sum(remaining, target, partial + [n])
if __name__ == "__main__":
num_str="3523014"
print("The number is:",num_str)
result_list=subset_sum(list(map(int,num_str)),10)
print(result_list)
答案 1 :(得分:1)
让我从您的代码出问题开始。.
现在我的解决方案..
def find_ten_substring(num_str):
ten_substr=[] #list to store all substring
for i in range(len(num_str)):
sum=0
sub_str=""
for j in range(i,len(num_str)):
sum+=int(num_str[j])
if sum<10:
sub_str+=num_str[j]
elif sum==10:
sub_str+=num_str[j]
ten_substr.append(sub_str)
#checks if my current index is not my last index as at avoid index out of bound
#error in next line
if (j!=len(num_str)-1):
#checks if value at next index is "0". If true, it does not make sum as zero and
#substring as empty, instead continues with same value in next iteration.
if (num_str[j+1]=="0"):
continue;
else:
break;
return ten_substr;
num_str="28353002"
print("The number is:",num_str)
result_list=find_ten_substring(num_str)
print(result_list)
答案 2 :(得分:0)
这是最简单的方法,使用两个for循环,在这里我将总和目标视为10
def find_ten_substring(num_str):
num_str=str(num_str)
number = list(map(int,num_str))
lst=[]
for i in range(0,len(num_str)):
sum_val=0
substr=""
for j in range(i,len(num_str)):
substr+=num_str[j]
sum_of_substr=list(map(int,substr))
sum_val=sum(sum_of_substr)
if(sum_val==10):
lst.append(substr)
return lst
num_str="3523014"
print("The number is:",num_str)
result_list=find_ten_substring(num_str)
print(result_list)
答案 3 :(得分:0)
df = pd.DataFrame({'A':['a','b'],
'B':[[['A1', 'A2']],[['A1', 'A2', 'A3']]]})
df.set_index('A')['B'].apply(lambda x: pd.Series(x[0]))
答案 4 :(得分:0)
def find_ten_substring(num_str):
m=[]
l=[]
for i in range(0,len(num_str)):
for j in range(i,len(num_str)):
m.append(num_str[i:j+1])
for k in set(m):
s=0
for b in range(0,len(k)):
s=s+int(k[b])
if(s==10 and b+1==len(k)):
l.append(k)
return l
num_str="3523014"
print("The number is:",num_str)
result_list=find_ten_substring(num_str)
print(result_list)
答案 5 :(得分:0)
def find_ten_substring(num_str):
m=[]
l=[]
for i in range(0,len(num_str)):
for j in range(i,len(num_str)):
m.append(num_str[i:j+1])
for k in set(m):
s=0
for b in range(0,len(k)):
s=s+int(k[b])
if(s==10 and b+1==len(k)):
l.append(k)
return l
num_str="2825302"
print("The number is:",num_str)
result_list=find_ten_substring(num_str)
print(result_list)