c

codeye

V1

2022/09/25阅读：23主题：默认主题

# faro shuffle

Python中阶：faro shuffle洗牌

``['ace', 'two', 'three', 'four', 'five', 'six']``

``['ace', 'four', 'two', 'five', 'three', 'six' ]``

If 8 perfect faro shuffles are performed on a deck of 52 playing cards, the deck is restored to its original order. Write a function that inputs an integer n and returns an integer representing the number of faro shuffles it takes to restore a deck of n cards to its original order. Assume n is an even number between 2 and 2000.

``arr = ['A','K','Q','J','9','8']arr[0] = arr[0]arr[1] = arr[3]arr[2] = arr[1]arr[3] = arr[4]arr[4] = arr[2]arr[5] = arr[5]print(arr)  #结果不对，因为赋值覆盖了前面的赋值['A', 'J', 'J', '9', 'J', '8']``

Python按顺序执行，前面分6步执行赋值导致结果不符 考虑一行代码搞定数组 6 个元素的赋值

``arr = ['A','K','Q','J','9','8']print(arr[0:6:2],arr[1:6:2])arr = arr[0:6:2] + arr[1:6:2]print(arr)['A', 'Q', '9'] ['K', 'J', '8']``

``def faro_cycles(deck_size):    arr, count = list(range(deck_size)), 0    original_arr = arr    while True:        arr = arr[0:deck_size:2] + arr[1:deck_size:2]        count += 1        if original_arr == arr:            break    return count``

``> number_to_letter()> 从字母到数字()> caesar_shift_single_character(l, amount)> caesar_shift(text, amount)``

``alphabet = "abcdefghijklmnopqrstuvwxyz"``

``import stringstring.ascii_letter``

``def number_to_letter(i): return alphabet[i%26]         #%26做绕口令def letter_to_number(l): return alphabet.find(l) # 字母表中的索引``

``def caesar_shift_single_character(l, amount):    i = letter_to_number(l)    if i == -1:     # character not found in alphabet      return ""                         # remove it, it's space or punctuation    else:      return number_to_letter(i + amount)              # Caesar shift``

# 如何对一个完整的文本进行编码

``def caesar_shift(text, amount):    shifted_text = ""    for char in text.lower():     # 也将大写字母转换为小写字母       shifted_text += caesar_shift_single_character(char, amount)   return shifted_text``

### 主程序

``message = """Once upon a midnight dreary, while I pondered, weak and weary,Over many a quaint and curious volume of forgotten lore—While I nodded, nearly napping, suddenly there came a tapping,As of some one gently rapping, rapping at my chamber door—"'Tis some visitor," I muttered, "tapping at my chamber door—Only this and nothing more.""""``

``message = """曾几何时，在一个沉闷的午夜，当我虚弱而疲惫地思索着。思考许多被遗忘的古朴而好奇的传说当我点点头，几乎打盹时，突然传来一阵敲击声。就像有人轻轻地拍打着我的房门。"是某个访客，"我喃喃自语，"敲打我的房门只有这个，没有别的。""""``
``code = caesar_shift(message, 2)print(code) #输出结果 ``

``"qpegwrqpcokfpkijvftgctayjkngkrqpfgtgfygcmcpfygctaqxgtocpacswckpvcpewtkqwuxqnwogqhhqtiqvgpnqtgyjkngkpqffgpgctnapcrrkpiuwffgpnavjgtgecogcvcrrkpicuqhuqogpgigpvnatcrrkpitcrrkpicvoaejcodgtfqqtvkuqogxkukvqtkowvgtgfvcrrkpicvoaejcodgtfqqtqpnavjkucpfqvjioqtg"``

``text = codealphabet = "abcdefghijklmnopqrstuvwxyz"def count_most(text):   #a-z遍历26字母表     bench,res = 0,sorted(text)    for e in alphabet:        #e_most是出现次数最多的字母，bench是出现总次数        if res.count(e) > bench:             bench = res.count(e)            e_most = e    return e_most,benchprint(count_most(text))``

c
V1