2 回答
TA贡献1829条经验 获得超4个赞
这是一个解决方案。基本逻辑是:
识别元音 ( vowel_inx) 的所有位置。
为这些位置创建所有数字组合的叉积。
循环这个叉积,并通过将数字分配到它们在原始术语中的相应位置来创建新词。
vowels = ["A", "E", "I", "O", "U", "Y"]
pre = "ANDRE"
import itertools
vowel_inx = [i for i in range(len(pre)) if pre[i] in vowels ]
ranges = [range(10) for _ in vowel_inx]
for comb in itertools.product(*ranges):
pw = list(pre)
for i in range(len(vowel_inx)):
pw[vowel_inx[i]] = str(comb[i])
print ("".join(pw))
输出是:
0NDR0
0NDR1
0NDR2
0NDR3
0NDR4
0NDR5
0NDR6
0NDR7
0NDR8
0NDR9
1NDR0
1NDR1
1NDR2
...
TA贡献2019条经验 获得超9个赞
这是一个正确的方法:
voyelle = ["A", "E", "I", "O", "U", "Y"]
def permute_single_index(word, index):
permutations = []
word_left = word[0:index]
word_right = word[index+1:]
for i in reversed(range(10)):
permutations.append(f"{word_left}{i}{word_right}")
return permutations
def testVoyellePrenom(pre):
upper_pre = pre.upper()
replace_indexes = [i for i, x in enumerate(upper_pre) if x in voyelle]
permutations = [upper_pre]
for ri in replace_indexes:
new_permutations = []
for word in permutations:
new_permutations += permute_single_index(word, ri)
permutations += new_permutations
print(permutations)
print(len(permutations))
def main():
testVoyellePrenom("Andre")
if __name__ == "__main__":
main()
你最终得到 121 个排列。
添加回答
举报