Python 从另一个字符串中删除一个字符串并生成所有可能的结果
我似乎看不到我的逻辑错误以及为什么它没有遍历所有可能的解决方案,有人能看到我的错误吗?或者我完全是在倒退,有更好的方法吗 例如,如果给您Python 从另一个字符串中删除一个字符串并生成所有可能的结果,python,algorithm,search,iteration,Python,Algorithm,Search,Iteration,我似乎看不到我的逻辑错误以及为什么它没有遍历所有可能的解决方案,有人能看到我的错误吗?或者我完全是在倒退,有更好的方法吗 例如,如果给您abcabcb并告诉您删除ab,您可能的结果将是cabcb、bcacb、bcabc、abccb、abcbc在这个问题中编辑您删除的字符串必须保持顺序,因此ab与ba不一样 given = abcabcb removed = ab XXcabcb XbcaXcb XbcabcX abcXXcb abcXbcX 我当前的代码将发现第一个答
abcabcb
并告诉您删除ab
,您可能的结果将是cabcb、bcacb、bcabc、abccb、abcbc
在这个问题中编辑您删除的字符串必须保持顺序,因此ab与ba不一样
given = abcabcb
removed = ab
XXcabcb
XbcaXcb
XbcabcX
abcXXcb
abcXbcX
我当前的代码将发现第一个答案两次,第二个答案,第三个答案,再次发现第一个答案,然后发现第四个答案两次并停止
打印输出,[0,1,'cabcb']
[0,4,'bcacb']
[0,4,'bcacb']
[0,4,'bcacb']
[0,6,'bcabc']
[0,6,'bcabc']
[0,1,'cabcb']
[3,4,‘abccb’]
[3,4,‘abccb’]
[3,4,‘abccb’]
结果是['cabcb','cabcb','bcacb','bcacb','bcacb','bcabc','bcabc','bcabc','cabcb','abccb','abccb']
python代码:
def hiddenMessage(given, remove):
alist = []
temp = []
temp = oneHiddenMessage(given, remove)
alist.append(temp[len(temp)-1])
count = 0
for i in range(len(remove)-1,-1,-1):
for j in range(temp[i], len(given)):
temp = oneHiddenMessagerec(given, remove, i, j)
if temp == False:
return alist
alist.append(temp[len(temp)-1])
print temp
def oneHiddenMessage(given, remove):
message = ""
index = 0
counterR = 0 #what element in remove you are looking at
returned = []
for counterG in range(len(given)): #searches entire given word
if counterR<len(remove): #makes sure hasn't gone beyond the elements of the string
if given[counterG]==remove[counterR]: #if the character at position counterG of given is == as char at position counter R of remove skip over
counterR+=1 #increment counterR
returned.append(counterG) #tracks the location of each char in removed relative to given
else:
message+=given[counterG] #if they aren't == add char to message
else:
message+=given[counterG] #if out of char from remove, throw rest of char onto message
if len(message)!=(len(given)-len(remove)):
return False #if not the expected size then not possible and throws false
returned.append(message)
return returned #returns locations of the removed char in respects to given and also the message
def oneHiddenMessagerec(given, remove, beingmoved, location):
message = ""
index = 0
counterR = 0
returned = []
for counterG in range(len(given)):
if counterR<len(remove):
if given[counterG]==remove[counterR] and not (beingmoved==counterR and location>counterG): #checks whether they are the same element and not the same as the previous attempt
counterR+=1
returned.append(counterG)
else:
message+=given[counterG]
else:
message+=given[counterG]
if len(message)!=(len(given)-len(remove)):
return False
returned.append(message)
return returned
def隐藏消息(给定,删除):
alist=[]
温度=[]
temp=oneHiddenMessage(给定,删除)
附加列表(临时[len(temp)-1])
计数=0
对于范围内的i(透镜(移除)-1,-1,-1):
对于范围内的j(温度[i],len(给定)):
temp=oneHiddenMessagerec(给定、移除、i、j)
如果temp==False:
返回列表
附加列表(临时[len(temp)-1])
打印温度
def ONEIDDENMESSAGE(给定,删除):
message=“”
索引=0
计数器R=0#您正在查看删除中的哪个元素
返回=[]
对于范围内的计数器(len(给定)):#搜索整个给定单词
如果我觉得你的代码太复杂太长。但我建议另一种方法。您可以在第一个字符串中找到所有出现的第二个字符串字符。在您的示例中,这将产生如下数组:[[0,3],[1,4,6]]
然后您应该删除所有对,以便第一个元素小于第二个元素[0,1],[0,4],[0,6],[3,4],[3,6]
。这可以通过一个递归函数来实现,从第一个列表中选择一个元素,然后转到下一个列表,选择一个比上一个列表中的元素大的元素,依此类推
0
.1
.4
.6
3
.1 # not good because 1<3
.4
.6
什么是func(0,-1,”)
?它说我们必须删除第二个字符串[0]
或a
,在这种情况下,它的索引大于-1
,我们的字符串到目前为止是”
。然后作为第一个字符串[0]='a'
我们忽略它并调用func(1,0,“”
,因为我们删除了a
,我们想删除第二个字符串[1]
,它的索引>0,我们的当前字符串是”
,因为我们没有添加a
。类似地,我们删除下一个b
并调用func(2,1,“”)
这意味着我们已经删除了第二个字符串中的前两个字符,我们应该删除下一个字符,由于第二个\u字符串
中没有其他字符,我们将第一个\u字符串
中的所有剩余字符添加到当前\u字符串
中并打印它。之后,我们返回func(1,0,“”)
中的一个步骤,并尝试查找下一个b(第二个字符串[1])
,因为下一个b
出现在索引4
中,我们将第一个字符串[1,2,3]
添加到我们的当前字符串中,并调用func(2,4,'bca')
。。。此例程将继续进行,直到找到所有可能的字符串。另一个Python解决方案是创建第一个字符串中出现的第二个字符串的数组,并使用itertools。product
创建所有递增的组合,并从第一个字符串中删除它们:
first_string = "abcabcb"
second_string = "ab"
array = []
for c in second_string:
arr = []
for i,c2 in enumerate(first_string):
if c2==c:
arr.append(i)
array.append(arr)
from itertools import product
f = lambda x: all( [x[i-1]<x[i] for i in range(1,len(x))])
combs = list(filter(f,product(*array)))
for item in combs:
string = ""
for i in range(len(first_string)):
if i not in item:
string += first_string[i]
print string
first\u string=“abcabcb”
第二个字符串=“ab”
数组=[]
对于第二个字符串中的c:
arr=[]
对于枚举中的i,c2(第一个字符串):
如果c2==c:
附件(i)
array.append(arr)
来自itertools进口产品
f=lambda x:all([x[i-1]对你有好处;)你的问题是“我放弃,你能完成工作吗?”那么你的问题是什么?请解释一下你的算法。@DSM,可能ab
并不意味着ba
@vish4071:啊,这是有道理的。所以我们必须按照remove
中相同的顺序删除字母。func的参数是什么?我在跟踪代码方面有点困难……忽略我,把它填出来:)如果你不能找到倒立,试着自己一步一步地写。
cabcb
bcacb
bcabc
abccb
abcbc
first_string = "abcabcb"
second_string = "ab"
array = []
for c in second_string:
arr = []
for i,c2 in enumerate(first_string):
if c2==c:
arr.append(i)
array.append(arr)
from itertools import product
f = lambda x: all( [x[i-1]<x[i] for i in range(1,len(x))])
combs = list(filter(f,product(*array)))
for item in combs:
string = ""
for i in range(len(first_string)):
if i not in item:
string += first_string[i]
print string