用所有数字替换每个元音以找到加密代码
Replace every vowels by all the numbers to find the encrypted code
所以我必须替换名称列表中的每个元音以匹配加密代码,所以我需要的是,例如,对于“Andre”,我需要得到:0ndr0、0ndr1、0ndr2、0ndr3 ... 1ndr1、1ndr2 ... 9ndr5 ... 9ndr9
这是我所做的简化版本:
def testVoyellePrenom():
voyelle = ["A", "E", "I", "O", "U", "Y"]
myNumbers = [a + 1 for a in range(-1,9)]
myNumbers.reverse()
pre = "ANDRE"
testing = ""
for x in pre:
if(x in voyelle):
for nb in myNumbers:
pre = pre.replace(x,str(nb))
x = str(nb)
testing = pre
print(testing)
Output :
9NDRE
8NDRE
7NDRE
6NDRE
5NDRE
4NDRE
3NDRE
2NDRE
1NDRE
0NDRE
0NDR9
0NDR8
0NDR7
0NDR6
0NDR5
0NDR4
0NDR3
0NDR2
0NDR1
0NDR0
Expected output :
...
0NDR0
1NDR9
1NDR8
1NDR7
1NDR6
1NDR5
1NDR4
1NDR3
1NDR2
1NDR1
1NDR0
....
这是一个解决方案。基本逻辑是:
- 找出元音的所有位置(
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
...
正确的做法是:
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 个排列。
所以我必须替换名称列表中的每个元音以匹配加密代码,所以我需要的是,例如,对于“Andre”,我需要得到:0ndr0、0ndr1、0ndr2、0ndr3 ... 1ndr1、1ndr2 ... 9ndr5 ... 9ndr9
这是我所做的简化版本:
def testVoyellePrenom():
voyelle = ["A", "E", "I", "O", "U", "Y"]
myNumbers = [a + 1 for a in range(-1,9)]
myNumbers.reverse()
pre = "ANDRE"
testing = ""
for x in pre:
if(x in voyelle):
for nb in myNumbers:
pre = pre.replace(x,str(nb))
x = str(nb)
testing = pre
print(testing)
Output :
9NDRE
8NDRE
7NDRE
6NDRE
5NDRE
4NDRE
3NDRE
2NDRE
1NDRE
0NDRE
0NDR9
0NDR8
0NDR7
0NDR6
0NDR5
0NDR4
0NDR3
0NDR2
0NDR1
0NDR0
Expected output :
...
0NDR0
1NDR9
1NDR8
1NDR7
1NDR6
1NDR5
1NDR4
1NDR3
1NDR2
1NDR1
1NDR0
....
这是一个解决方案。基本逻辑是:
- 找出元音的所有位置(
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
...
正确的做法是:
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 个排列。