最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

用Python實(shí)現(xiàn)2024年春晚劉謙魔術(shù)

 更新時(shí)間:2024年02月11日 09:22:41   作者:夏天是冰紅茶  
昨晚春晚上劉謙的兩個(gè)魔術(shù)表演都非常精彩,忍不住用編程去模擬一下這個(gè)過(guò)程,所以本文給大家用Python實(shí)現(xiàn)2024年春晚劉謙魔術(shù),文中通過(guò)代碼示例給大家介紹的非常詳細(xì),需要的朋友可以參考下

簡(jiǎn)介

這是新春的第一篇,今天早上睡到了自然醒,打開手機(jī)刷視頻就被劉謙的魔術(shù)所吸引,忍不住用編程去模擬一下這個(gè)過(guò)程。

首先,聲明的一點(diǎn),大年初一不學(xué)習(xí),所以這其中涉及的數(shù)學(xué)原理約瑟夫環(huán)大家可以找找其他的教程看看,我這塊只是復(fù)現(xiàn)它魔術(shù)里面的每個(gè)步驟。

魔術(shù)的步驟

總而言之,可以分為以下8個(gè)步驟:

Step 1: 將四張4張牌撕成兩半,直接將兩堆疊放;
Step 2: 假設(shè)姓名為n個(gè)字,重復(fù)n次,將堆在最上的牌放到最下面;
Step 3: 將牌堆最上的3張拿出,不改變順序,并隨機(jī)插入牌堆中間;
Step 4: 將牌堆最上方的牌拿走,放在一旁;
Step 5: 按照南/北/不知道是南或者北方地區(qū),判斷自己屬于哪一地區(qū),并分別將牌堆最上的1/2/3,不改變順序,并隨機(jī)插入牌堆中間;
Step 6: 按性別男/女,從牌堆最上方拿走1/2張牌,一邊念口訣:“見(jiàn)證奇跡的時(shí)刻”,每念一個(gè)字,將牌堆最上方的牌放到牌堆最下;
Step 7: 念口訣“好運(yùn)留下米”時(shí),將牌堆最上的牌放到牌堆最下;念“煩惱扔出去”時(shí),將牌堆最上方的牌移除。重復(fù)這兩句口訣,直到手中只有一張牌;
Step 8: 最后留下的牌和Step 4拿走的牌是一樣的。

過(guò)程拆開分來(lái)其實(shí)就是對(duì)列表進(jìn)行一個(gè)簡(jiǎn)單的操作了

用python實(shí)現(xiàn)其中的過(guò)程

0. 模擬撲克牌打亂并抽取的過(guò)程;

import random
import itertools
import copy
# 定義撲克牌
suits = ['紅桃', '方塊', '梅花', '黑桃']
ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
jokers = ['小王', '大王']
deck_of_cards = list(itertools.product(suits, ranks)) + jokers
random.shuffle(deck_of_cards)       # 模擬打亂的操作
print(f"隨機(jī)生成的{len(deck_of_cards)}撲克牌:", deck_of_cards)
selected_cards = random.sample(deck_of_cards, 4)
print("隨機(jī)抽取其中的四張牌:", selected_cards)

隨機(jī)抽取其中的四張牌: [('紅桃', '9'), ('黑桃', '8'), ('黑桃', 'A'), ('黑桃', 'K')]

1. 將四張4張牌撕成兩半,直接將兩堆疊放;

def split_and_stack(cards):
    cards_copy = copy.copy(cards)
    merged_cards = cards + cards_copy
    return merged_cards
 
split_cards = split_and_stack(selected_cards)
print("撕成兩半后堆疊:", split_cards)

撕成兩半后堆疊: [('紅桃', '9'), ('黑桃', '8'), ('黑桃', 'A'), ('黑桃', 'K'), ('紅桃', '9'), ('黑桃', '8'), ('黑桃', 'A'), ('黑桃', 'K')]

2. 假設(shè)姓名為n個(gè)字,重復(fù)n次,將堆在最上的牌放到最下面;

def repeat_name(cards, name):
    name_length = len(name)
    for _ in range(name_length):
        # 取出堆在最上的牌,放到最下面
        top_card = cards.pop(0)  
        cards.append(top_card) 
    return cards
 
split_cards_repeated = repeat_name(split_cards, name)
print(f"{name} 重復(fù)姓名字?jǐn)?shù)次后的牌堆:", split_cards_repeated)

夏天是冰紅茶 重復(fù)姓名字?jǐn)?shù)次后的牌堆: [('黑桃', 'A'), ('黑桃', 'K'), ('紅桃', '9'), ('黑桃', '8'), ('黑桃', 'A'), ('黑桃', 'K'), ('紅桃', '9'), ('黑桃', '8')]

3. 將牌堆最上的3張拿出,不改變順序,并隨機(jī)插入牌堆中間;

def take_top_and_insert(cards):
    top_three_cards = cards[:3]  # 取出最上面的3張牌
    remaining_cards = cards[3:]  # 剩下的牌
    insert_index = random.randint(1, len(remaining_cards))
    shuffled_cards = remaining_cards[:insert_index] + top_three_cards + remaining_cards[insert_index:]
    return shuffled_cards
 
shuffled_cards = take_top_and_insert(split_cards_repeated)
print("牌堆最上的3張拿出,隨機(jī)插入后的牌堆:", shuffled_cards)

牌堆最上的3張拿出,隨機(jī)插入后的牌堆: [('黑桃', '8'), ('黑桃', 'A'), ('黑桃', 'A'), ('黑桃', 'K'), ('紅桃', '9'), ('黑桃', 'K'), ('紅桃', '9'), ('黑桃', '8')]

4. 將牌堆最上方的牌拿走,放在一旁;

def take_top_card(cards):
    top_card = cards.pop(0)  # 取出最上方的牌
    return top_card
 
top_card = take_top_card(shuffled_cards)
print("拿走的牌:", top_card)
print("剩余的牌:", shuffled_cards)

拿走的牌: ('黑桃', '8')
剩余的牌: [('黑桃', 'A'), ('黑桃', 'A'), ('黑桃', 'K'), ('紅桃', '9'), ('黑桃', 'K'), ('紅桃', '9'), ('黑桃', '8')] 

5. 按照南/北/不知道是南或者北方地區(qū),判斷自己屬于哪一地區(qū),并分別將牌堆最上的1/2/3,不改變順序,并隨機(jī)插入牌堆中間;

def insert_cards_based_on_region(cards, region):
    if region == "南":
        insert_count = 1
    elif region == "北":
        insert_count = 2
    else:
        insert_count = 3
 
    top = cards[:insert_count]
    remaining_cards = cards[insert_count:]
    insert_index = random.randint(0, len(remaining_cards)-1)
    shuffled_cards = remaining_cards[:insert_index] + top + remaining_cards[insert_index:]
 
    return shuffled_cards
 
 
shuffled_cards_region = insert_cards_based_on_region(shuffled_cards, region)
print(f"{region}方地區(qū)插入后的牌堆:", shuffled_cards_region)

南方地區(qū)插入后的牌堆: [('黑桃', 'A'), ('黑桃', 'K'), ('紅桃', '9'), ('黑桃', 'K'), ('黑桃', 'A'), ('紅桃', '9'), ('黑桃', '8')] 

6. 按性別男/女,從牌堆最上方拿走1/2張牌,一邊念口訣:“見(jiàn)證奇跡的時(shí)刻”,每念一個(gè)字,將牌堆最上方的牌放到牌堆最下;

def take_and_chant(cards, gender, chant="見(jiàn)證奇跡的時(shí)刻"):
    take_count = 0
    if gender == "男":
        take_count = 1
    elif gender == "女":
        take_count = 2
    else:
        print("未知性別")
 
    remaining_cards = cards[take_count:]  # 剩下的牌
    print(remaining_cards)
    # 念口訣過(guò)程
    for c in chant:
        remaining_cards.append(remaining_cards.pop(0))  # 將最上方的牌放到牌堆最下
 
    return remaining_cards
 
remaining_cards= take_and_chant(shuffled_cards_region, gender, chant)
print(f"剩余的牌堆:", remaining_cards)

[('黑桃', 'K'), ('紅桃', '9'), ('黑桃', 'K'), ('黑桃', 'A'), ('紅桃', '9'), ('黑桃', '8')]
剩余的牌堆: [('紅桃', '9'), ('黑桃', 'K'), ('黑桃', 'A'), ('紅桃', '9'), ('黑桃', '8'), ('黑桃', 'K')] 

7/8. 念口訣“好運(yùn)留下米”時(shí),將牌堆最上的牌放到牌堆最下;念“煩惱扔出去”時(shí),將牌堆最上方的牌移除。重復(fù)這兩句口訣,直到手中只有一張牌;最后留下的牌和Step 4拿走的牌是一樣的。

def chant_and_modify(cards):
    iter = 1
    while len(cards) > 1:
        chant_good_luck = "好運(yùn)留下米"
        chant_throw_away = "煩惱扔出去"
        print(f"\n第{iter}輪口訣開始:")
        cards.append(cards.pop(0))
        print(f"口訣{chant_good_luck}結(jié)束后手上的牌:", cards)
        cards.pop(0)
        print(f"口訣{chant_throw_away}結(jié)束后手上的牌:", cards)
        iter += 1
 
    return cards[0]
 
final_card = chant_and_modify(remaining_cards)
print(f"\n最終留下的牌:{final_card}, Step 4:{top_card}")

第1輪口訣開始:
口訣好運(yùn)留下米結(jié)束后手上的牌: [('黑桃', 'K'), ('黑桃', 'A'), ('紅桃', '9'), ('黑桃', '8'), ('黑桃', 'K'), ('紅桃', '9')]
口訣煩惱扔出去結(jié)束后手上的牌: [('黑桃', 'A'), ('紅桃', '9'), ('黑桃', '8'), ('黑桃', 'K'), ('紅桃', '9')]

第2輪口訣開始:
口訣好運(yùn)留下米結(jié)束后手上的牌: [('紅桃', '9'), ('黑桃', '8'), ('黑桃', 'K'), ('紅桃', '9'), ('黑桃', 'A')]
口訣煩惱扔出去結(jié)束后手上的牌: [('黑桃', '8'), ('黑桃', 'K'), ('紅桃', '9'), ('黑桃', 'A')]

第3輪口訣開始:
口訣好運(yùn)留下米結(jié)束后手上的牌: [('黑桃', 'K'), ('紅桃', '9'), ('黑桃', 'A'), ('黑桃', '8')]
口訣煩惱扔出去結(jié)束后手上的牌: [('紅桃', '9'), ('黑桃', 'A'), ('黑桃', '8')]

第4輪口訣開始:
口訣好運(yùn)留下米結(jié)束后手上的牌: [('黑桃', 'A'), ('黑桃', '8'), ('紅桃', '9')]
口訣煩惱扔出去結(jié)束后手上的牌: [('黑桃', '8'), ('紅桃', '9')]

第5輪口訣開始:
口訣好運(yùn)留下米結(jié)束后手上的牌: [('紅桃', '9'), ('黑桃', '8')]
口訣煩惱扔出去結(jié)束后手上的牌: [('黑桃', '8')]

最終留下的牌:('黑桃', '8'), Step 4:('黑桃', '8')

完整的代碼

import random
import itertools
import copy
# 定義撲克牌
suits = ['紅桃', '方塊', '梅花', '黑桃']
ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
jokers = ['小王', '大王']
deck_of_cards = list(itertools.product(suits, ranks)) + jokers
random.shuffle(deck_of_cards)       # 模擬打亂的操作
print(f"隨機(jī)生成的{len(deck_of_cards)}撲克牌:", deck_of_cards)
selected_cards = random.sample(deck_of_cards, 4)
print("隨機(jī)抽取其中的四張牌:", selected_cards)
 
 
# 模擬性別為男的情況
name = "夏天是冰紅茶"
gender = "男"
chant = "見(jiàn)證奇跡的時(shí)刻"
region = "南"
 
# step 1: 將四張4張牌撕成兩半,直接將兩堆疊放;
def split_and_stack(cards):
    cards_copy = copy.copy(cards)
    merged_cards = cards + cards_copy
    return merged_cards
 
split_cards = split_and_stack(selected_cards)
print("撕成兩半后堆疊:", split_cards)
 
# Step 2: 設(shè)你的姓名為n個(gè)字,重復(fù)n次,將堆在最上的牌放到最下面;
def repeat_name(cards, name):
    name_length = len(name)
    for _ in range(name_length):
        # 取出堆在最上的牌,放到最下面
        top_card = cards.pop(0)
        cards.append(top_card)
    return cards
 
split_cards_repeated = repeat_name(split_cards, name)
print(f"{name} 重復(fù)姓名字?jǐn)?shù)次后的牌堆:", split_cards_repeated)
 
# Step 3: 將牌堆最上的3張拿出,不改變順序,并隨機(jī)插入牌堆中間
def take_top_and_insert(cards):
    top_three_cards = cards[:3]  # 取出最上面的3張牌
    remaining_cards = cards[3:]  # 剩下的牌
    insert_index = random.randint(1, len(remaining_cards))
    shuffled_cards = remaining_cards[:insert_index] + top_three_cards + remaining_cards[insert_index:]
    return shuffled_cards
 
shuffled_cards = take_top_and_insert(split_cards_repeated)
print("牌堆最上的3張拿出,隨機(jī)插入后的牌堆:", shuffled_cards)
 
# Step 4: 將牌堆最上方的牌拿走,放在一旁
def take_top_card(cards):
    top_card = cards.pop(0)  # 取出最上方的牌
    return top_card
 
top_card = take_top_card(shuffled_cards)
print("拿走的牌:", top_card)
print("剩余的牌:", shuffled_cards)
 
# Step 5: 按照南/北/不知道是南或者北方地區(qū),判斷自己屬于哪一地區(qū),并分別將牌堆最上的1/2/3,不改變順序,并隨機(jī)插入牌堆中間
def insert_cards_based_on_region(cards, region):
    if region == "南":
        insert_count = 1
    elif region == "北":
        insert_count = 2
    else:
        insert_count = 3
 
    top = cards[:insert_count]
    remaining_cards = cards[insert_count:]
    insert_index = random.randint(0, len(remaining_cards)-1)
    shuffled_cards = remaining_cards[:insert_index] + top + remaining_cards[insert_index:]
 
    return shuffled_cards
 
 
shuffled_cards_region = insert_cards_based_on_region(shuffled_cards, region)
print(f"{region}方地區(qū)插入后的牌堆:", shuffled_cards_region)
 
 
# Step 6: 按性別男/女,從牌堆最上方拿走1/2張牌,一邊念口訣:“見(jiàn)證奇跡的時(shí)刻”,每念一個(gè)字,將牌堆最上方的牌放到牌堆最下。
def take_and_chant(cards, gender, chant="見(jiàn)證奇跡的時(shí)刻"):
    take_count = 0
    if gender == "男":
        take_count = 1
    elif gender == "女":
        take_count = 2
    else:
        print("未知性別")
 
    remaining_cards = cards[take_count:]  # 剩下的牌
    print(remaining_cards)
    # 念口訣過(guò)程
    for c in chant:
        remaining_cards.append(remaining_cards.pop(0))  # 將最上方的牌放到牌堆最下
 
    return remaining_cards
 
 
remaining_cards= take_and_chant(shuffled_cards_region, gender, chant)
print(f"剩余的牌堆:", remaining_cards)
 
# Step 7: 念口訣“好運(yùn)留下米”時(shí),將牌堆最上的牌放到牌堆最下;念“煩惱扔出去”時(shí),將牌堆最上方的牌移除。重復(fù)這兩句口訣,直到手中只有一張牌;
def chant_and_modify(cards):
    iter = 1
    while len(cards) > 1:
        chant_good_luck = "好運(yùn)留下米"
        chant_throw_away = "煩惱扔出去"
        print(f"\n第{iter}輪口訣開始:")
        cards.append(cards.pop(0))
        print(f"口訣{chant_good_luck}結(jié)束后手上的牌:", cards)
        cards.pop(0)
        print(f"口訣{chant_throw_away}結(jié)束后手上的牌:", cards)
        iter += 1
 
    return cards[0]
 
# Step 8: 最后留下的牌和Step 4拿走的牌是一樣的。
final_card = chant_and_modify(remaining_cards)
print(f"\n最終留下的牌:{final_card}, Step 4:{top_card}")

大家可以自己去試一試,在步驟6后男生拿走的牌總是會(huì)在對(duì)應(yīng)的第5位,女生拿走的牌總是會(huì)在對(duì)應(yīng)的第3位。

結(jié)語(yǔ)

其實(shí)說(shuō)實(shí)話,這種數(shù)學(xué)魔術(shù)在我小時(shí)候買的書里就曾經(jīng)看到過(guò)許多。雖然現(xiàn)在了解了其中的數(shù)學(xué)原理,但當(dāng)時(shí)的驚奇與歡樂(lè)感覺(jué)依然難以忘懷。劉謙老師在表演中展現(xiàn)了非凡的技藝,不僅僅是數(shù)學(xué)的巧妙運(yùn)用,更是他善于抓住觀眾的好奇心,創(chuàng)造出讓人難以置信的奇跡。

以上就是用Python實(shí)現(xiàn)2024年春晚劉謙魔術(shù)的詳細(xì)內(nèi)容,更多關(guān)于Python春晚劉謙魔術(shù)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • django中F與Q查詢的使用

    django中F與Q查詢的使用

    一般查詢都是單條件查詢,F(xiàn)和Q是組合條件查詢,本文主要介紹了django中F與Q查詢的使用,感興趣的可以了解一下
    2021-06-06
  • Python?PyCharm無(wú)法打開終端命令行最終解決方案(實(shí)測(cè)成功)

    Python?PyCharm無(wú)法打開終端命令行最終解決方案(實(shí)測(cè)成功)

    這篇文章主要介紹了在使用PyCharm?2024版本時(shí)遇到的無(wú)法打開終端的問(wèn)題,文中提供了兩種解決方案,大家可以根據(jù)自己的需求選擇對(duì)應(yīng)的解決方法,需要的朋友可以參考下
    2024-12-12
  • python中關(guān)于os.path.pardir的一些坑

    python中關(guān)于os.path.pardir的一些坑

    這篇文章主要介紹了python中關(guān)于os.path.pardir的一些坑及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • flask應(yīng)用部署到服務(wù)器的方法

    flask應(yīng)用部署到服務(wù)器的方法

    這篇文章主要介紹了flask應(yīng)用部署到服務(wù)器的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • 詳解Tensorflow數(shù)據(jù)讀取有三種方式(next_batch)

    詳解Tensorflow數(shù)據(jù)讀取有三種方式(next_batch)

    本篇文章主要介紹了Tensorflow數(shù)據(jù)讀取有三種方式(next_batch),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-02-02
  • Python實(shí)現(xiàn)文件查詢關(guān)鍵字功能的示例詳解

    Python實(shí)現(xiàn)文件查詢關(guān)鍵字功能的示例詳解

    這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)文件查詢關(guān)鍵字功能,文中的示例詳解詳細(xì),具有一定的借鑒價(jià)值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2026-02-02
  • 使用Python實(shí)現(xiàn)炫酷的數(shù)據(jù)動(dòng)態(tài)圖大全

    使用Python實(shí)現(xiàn)炫酷的數(shù)據(jù)動(dòng)態(tài)圖大全

    數(shù)據(jù)可視化是通過(guò)圖形、圖表、地圖等可視元素將數(shù)據(jù)呈現(xiàn)出來(lái),以便更容易理解、分析和解釋,它是將抽象的數(shù)據(jù)轉(zhuǎn)化為直觀形象的過(guò)程,本文給大家介紹了使用Python實(shí)現(xiàn)炫酷的數(shù)據(jù)動(dòng)態(tài)圖大全,需要的朋友可以參考下
    2024-06-06
  • python flask實(shí)現(xiàn)分頁(yè)效果

    python flask實(shí)現(xiàn)分頁(yè)效果

    這篇文章主要為大家詳細(xì)介紹了python flask實(shí)現(xiàn)分頁(yè)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • Python3中的2to3轉(zhuǎn)換工具使用示例

    Python3中的2to3轉(zhuǎn)換工具使用示例

    這篇文章主要介紹了Python3中的2to3轉(zhuǎn)換工具使用示例,本文詳細(xì)講解了使用的步驟,并總結(jié)了一些使用注意事項(xiàng),需要的朋友可以參考下
    2015-06-06
  • python機(jī)器學(xué)習(xí)pytorch?張量基礎(chǔ)教程

    python機(jī)器學(xué)習(xí)pytorch?張量基礎(chǔ)教程

    這篇文章主要為大家介紹了python機(jī)器學(xué)習(xí)pytorch?張量基礎(chǔ)教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10

最新評(píng)論

福州市| 梓潼县| 迁安市| 社会| 建始县| 廉江市| 西青区| 旌德县| 滁州市| 盱眙县| 应城市| 米脂县| 阿荣旗| 略阳县| 顺义区| 鹤庆县| 怀化市| 县级市| 麻阳| 汽车| 曲靖市| 通河县| 阿拉善盟| 高清| 綦江县| 潞西市| 虞城县| 和平县| 济宁市| 武乡县| 孟津县| 兰溪市| 龙陵县| 东明县| 齐齐哈尔市| 敦煌市| 台前县| 上栗县| 广州市| 高尔夫| 启东市|