gpt4 book ai didi

Python Anagrams 递归

转载 作者:行者123 更新时间:2023-11-28 18:45:55 24 4
gpt4 key购买 nike

<分区>

我有这个代码:

mylist = open('sortedwords.txt')
txt = mylist.read()
mylist = txt.split()
stuff = input('Type a word here: ')

def removeletters (word, Analysis):
for char in range (len(Analysis)):
if Analysis [char] in word:
word = word.replace(Analysis[char],"",1)
return word

def anagramSubset(word, textList):
newWord = word
for char in range(len(textList)):
if textList[char] not in newWord:
return False
else:
newWord = newWord.replace(textList[char],"",1)
return True

def anagram(word, textList):
savedWords =[]
for checkword in textList:
if len(word) == len(checkword) and anagramSubset(word, checkword):
savedWords.append(checkword)
print(checkword)

anagram(stuff, mylist)

它应该接受一个输入词,从输入词中删除字母,然后生成一个词子集并将其保存到一个数组中以供打印。

问题是代码将保存可以从输入中创建的每个单词。例如。 spot 的输入导致 toptopsstoppotspot等。结果应该只有topspotsstop

代码有什么问题,我该如何解决?

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com