亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

用所有數字替換每個元音以找到加密代碼

用所有數字替換每個元音以找到加密代碼

翻閱古今 2023-02-07 14:35:12
所以我必須替換名稱列表中的每個元音以匹配加密代碼,所以我需要的是,例如,對于“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 :9NDRE8NDRE7NDRE6NDRE5NDRE4NDRE3NDRE2NDRE1NDRE0NDRE0NDR90NDR80NDR70NDR60NDR50NDR40NDR30NDR20NDR10NDR0Expected output :...0NDR01NDR91NDR81NDR71NDR61NDR51NDR41NDR31NDR21NDR11NDR0....
查看完整描述

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

... 


查看完整回答
反對 回復 2023-02-07
?
慕少森

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 個排列。



查看完整回答
反對 回復 2023-02-07
  • 2 回答
  • 0 關注
  • 140 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號