本地部署iis部署网站,深圳学校网站建设哪家好,网站快速备案多少钱,wordpress 数据库账号刘谦春晚魔术是一个让人叹为观止的魔术表演#xff0c;其中涉及到了数学、编程和创意的结合。看了春晚魔术的朋友们#xff0c;是不是好奇春晚刘谦的魔术是怎么变的。
在这篇文章中#xff0c;我们将通过 Python 程序实现春晚刘谦魔术#xff0c;让读者对这个魔术有更深入…刘谦春晚魔术是一个让人叹为观止的魔术表演其中涉及到了数学、编程和创意的结合。看了春晚魔术的朋友们是不是好奇春晚刘谦的魔术是怎么变的。
在这篇文章中我们将通过 Python 程序实现春晚刘谦魔术让读者对这个魔术有更深入的了解。
一、导入库
import random
from collections import deque二、跟着刘谦的步骤写程序
1、打乱13张牌
用一个 cards 列表存放 13 张不一样的牌 [“2”, “3”, “4”, “5”, “6”, “7”, “8”, “9”, “10”, “J”, “Q”, “K”, “A”]
使用random.shuffle() 函数将列表中的元素随机打乱。这个函数会直接修改原始列表而不是创建一个新的打乱顺序的列表。
import random
cards [2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K, A]
print(f最初的的13张牌{cards})
random.shuffle(cards)
print(f打乱顺序的13张牌{cards})Pycharm 控制台输出如下 2、随机抽取4张牌
使用random.sample() 是 函数从列表中随机抽取指定数量的元素。这个函数不会修改原始序列而是返回一个新的包含抽取元素的列表。
from collections import deque# 随机选择4张牌
random_4_cards random.sample(cards, 4)
print(f随机抽出的4张牌{random_4_cards})Pycharm 控制台输出如下 3、将牌对折撕开
创建了一个名为 random_8_cards 的双端队列其中包含 random_4_cards 的 2 倍元素。
# 对折后撕开得到8张牌
random_8_cards deque(random_4_cards * 2)
print(f对折后撕开得到8张牌{random_8_cards})Pycharm 控制台输出如下 4、根据名字字数得到新顺序牌
使用 collections.deque 类中的rotate() 方法将双端队列中的元素向左或向右旋转指定的位置。这个方法接受一个参数表示要旋转的位置数。正数表示向右旋转负数表示向左旋转。
# 1. 根据名字有几个字将前几张牌移到最后
name int(input(请输入名字字数))
# 将双端队列中的元素向左旋转几个位置
random_8_cards.rotate(-name)
print(f根据名字字数调整后的牌{random_8_cards})Pycharm 控制台输出如下 5、最上面3张插入剩下牌中间
# 2. 取出前三张牌并随机插入剩余牌中不能插在第一张和最后一张
first_three [random_8_cards.popleft() for i in range(3)]
print(f上面3张牌是{first_three})
print(f下面5张牌是{random_8_cards})for card in first_three:insert_position random.randint(1, len(random_8_cards) - 2)random_8_cards.insert(insert_position, card)print(f插入牌是{card}随机插入位置是{insert_position1}新牌顺序是{random_8_cards})
print(f上面3张牌随机插入剩下牌中间此时新牌顺序{random_8_cards})Pycharm 控制台输出如下 6、最上面1张牌藏起来
# 3. 把最上面的牌藏起来
remembered_card random_8_cards.popleft()
print(f藏起来的1张牌是{remembered_card})
print(f剩下7张牌是{random_8_cards})Pycharm 控制台输出如下 7、根据南北方得到新顺序牌
# 4. 南方人取1张北方人取2张无法确定取3张将这些牌随机插入剩下的牌中
location int(input(请输入地区南方人输入1北方人输入2无法确定输入3))
first_location [random_8_cards.popleft() for i in range(location)]
print(f上面地区牌是{first_location})
print(f剩下牌是{random_8_cards})for card in first_location:insert_position random.randint(1, len(random_8_cards) - 2)random_8_cards.insert(insert_position, card)print(f插入牌是{card}随机插入位置是{insert_position 1}新牌顺序是{random_8_cards})
print(f根据南北方随机插入剩下牌中间此时新牌顺序{random_8_cards})Pycharm 控制台输出如下 8、根据性别得到新顺序牌
# 5. 男生取1张女生取2张将这些牌扔掉
gender int(input(请输入性别男性输入1女性输入2))
for i in range(gender):random_8_cards.popleft()
print(f根据性别扔牌此时新牌顺序{random_8_cards})Pycharm 控制台输出如下 9、见证奇迹的时刻
见证奇迹的时刻是 7 个字翻译成代码就是将双端队列中的元素向左旋转 7 个位置。
# 6. 见证奇迹的时刻
# 将双端队列中的元素向左旋转7个位置
random_8_cards.rotate(-7)
print(f见证奇迹的时刻向左旋转7次牌此时新牌顺序{random_8_cards})Pycharm 控制台输出如下 10、好运留下来烦恼丢出去
# 7. 好运留下来烦恼丢出去
while len(random_8_cards) 1:random_8_cards.append(random_8_cards.popleft()) # 第一张牌移到最后random_8_cards.popleft() # 删除现在的第一张牌print(f好运留下来烦恼丢出去第一张牌移到最后删除现在的第一张牌此时剩余牌顺序{random_8_cards})
print(f剩余最后1张牌是{random_8_cards})Pycharm 控制台输出如下 三、完整代码
完整的代码程序如下
import random
from collections import dequecards [2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K, A]
print(f最初的的13张牌{cards})
random.shuffle(cards)
print(f打乱顺序的13张牌{cards})# 随机选择4张牌
random_4_cards random.sample(cards, 4)
print(f随机抽出的4张牌{random_4_cards})
# 对折后撕开得到8张牌
random_8_cards deque(random_4_cards * 2)
print(f对折后撕开得到8张牌{random_8_cards})# 1. 根据名字有几个字将前几张牌移到最后
name int(input(请输入名字字数))
# 将双端队列中的元素向左旋转几个位置
random_8_cards.rotate(-name)
print(f根据名字字数调整后的牌{random_8_cards})# 2. 取出前三张牌并随机插入剩余牌中不能插在第一张和最后一张
first_three [random_8_cards.popleft() for _ in range(3)]
print(f上面3张牌是{first_three})
print(f下面5张牌是{random_8_cards})for card in first_three:insert_position random.randint(1, len(random_8_cards) - 2)random_8_cards.insert(insert_position, card)print(f插入牌是{card}随机插入位置是{insert_position 1}新牌顺序是{random_8_cards})
print(f上面3张牌随机插入剩下牌中间此时新牌顺序{random_8_cards})# 3. 把最上面的牌藏起来
remembered_card random_8_cards.popleft()
print(f藏起来的1张牌是{remembered_card})
print(f剩下7张牌是{random_8_cards})# 4. 南方人取1张北方人取2张无法确定取3张将这些牌随机插入剩下的牌中
location int(input(请输入地区南方人输入1北方人输入2无法确定输入3))
first_location [random_8_cards.popleft() for i in range(location)]
print(f上面地区牌是{first_location})
print(f剩下牌是{random_8_cards})for card in first_location:insert_position random.randint(1, len(random_8_cards) - 2)random_8_cards.insert(insert_position, card)print(f插入牌是{card}随机插入位置是{insert_position 1}新牌顺序是{random_8_cards})
print(f根据南北方随机插入剩下牌中间此时新牌顺序{random_8_cards})# 5. 男生取1张女生取2张将这些牌扔掉
gender int(input(请输入性别男性输入1女性输入2))
for i in range(gender):random_8_cards.popleft()
print(f根据性别扔牌此时新牌顺序{random_8_cards})# 6. 见证奇迹的时刻
# 将双端队列中的元素向左旋转7个位置
random_8_cards.rotate(-7)
print(f见证奇迹的时刻向左旋转7次牌此时新牌顺序{random_8_cards})# 7. 好运留下来烦恼丢出去
while len(random_8_cards) 1:random_8_cards.append(random_8_cards.popleft()) # 第一张牌移到最后random_8_cards.popleft() # 删除现在的第一张牌print(f好运留下来烦恼丢出去第一张牌移到最后删除现在的第一张牌此时剩余牌顺序{random_8_cards})
print(f剩余最后1张牌是{random_8_cards})# 8. 查看藏起来的1张牌
print(f藏起来的1张牌是{remembered_card})Pycharm 控制台输出如下 四、封装函数
我们将上面的代码进行封装定义一个 magic() 函数把名字字数定义为随机 1-10 的数字南北方人为随机 1-3 的数字性别为随机 1-2 的数字。
在 main 主函数内调用 10 万次 magic()函数看一下运行结果正确率。
import random
from collections import dequedef magic():cards [2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K, A]# print(f最初的的13张牌{cards})random.shuffle(cards)# print(f打乱顺序的13张牌{cards})# 随机选择4张牌random_4_cards random.sample(cards, 4)# print(f随机抽出的4张牌{random_4_cards})# 对折后撕开得到8张牌random_8_cards deque(random_4_cards * 2)# print(f对折后撕开得到8张牌{random_8_cards})# 1. 根据名字有几个字将前几张牌移到最后name int(random.randint(1, 10))# 将双端队列中的元素向左旋转几个位置random_8_cards.rotate(-name)# print(f根据名字字数调整后的牌{random_8_cards})# 2. 取出前三张牌并随机插入剩余牌中不能插在第一张和最后一张first_three [random_8_cards.popleft() for _ in range(3)]# print(f上面3张牌是{first_three})# print(f下面5张牌是{random_8_cards})for card in first_three:insert_position random.randint(1, len(random_8_cards) - 2)random_8_cards.insert(insert_position, card)# print(f插入牌是{card}随机插入位置是{insert_position 1}新牌顺序是{random_8_cards})# print(f上面3张牌随机插入剩下牌中间此时新牌顺序{random_8_cards})# 3. 把最上面的牌藏起来remembered_card random_8_cards.popleft()# print(f藏起来的1张牌是{remembered_card})# print(f剩下7张牌是{random_8_cards})# 4. 南方人取1张北方人取2张无法确定取3张将这些牌随机插入剩下的牌中location int(random.randint(1, 4))first_location [random_8_cards.popleft() for i in range(location)]# print(f上面地区牌是{first_location})# print(f剩下牌是{random_8_cards})for card in first_location:insert_position random.randint(1, len(random_8_cards) - 2)random_8_cards.insert(insert_position, card)# print(f插入牌是{card}随机插入位置是{insert_position 1}新牌顺序是{random_8_cards})# print(f根据南北方随机插入剩下牌中间此时新牌顺序{random_8_cards})# 5. 男生取1张女生取2张将这些牌扔掉gender int(random.randint(1, 2))for i in range(gender):random_8_cards.popleft()# print(f根据性别扔牌此时新牌顺序{random_8_cards})# 6. 见证奇迹的时刻# 将双端队列中的元素向左旋转7个位置random_8_cards.rotate(-7)# print(f见证奇迹的时刻向左旋转7次牌此时新牌顺序{random_8_cards})# 7. 好运留下来烦恼丢出去while len(random_8_cards) 1:random_8_cards.append(random_8_cards.popleft()) # 第一张牌移到最后random_8_cards.popleft() # 删除现在的第一张牌# print(f好运留下来烦恼丢出去第一张牌移到最后删除现在的第一张牌此时剩余牌顺序{random_8_cards})# print(f剩余最后1张牌是{random_8_cards[0]})# 8. 查看藏起来的1张牌# print(f藏起来的1张牌是{remembered_card[0]})return [random_8_cards[0], remembered_card]if __name__ __main__:# 进行多次模拟,计算正确率times 100000correct_match_times 0for i in range(times):result magic()if result[0] result[1]:correct_match_times 1match_rate f{(correct_match_times / times * 100)} % print(f运行{times}次,正确率是{match_rate})Pycharm 控制台输出如下
运行 10 万次程序正确率 100%。 五、总结
通过我们的代码实验可以看出刘谦的这个魔术纯粹是数学原理只要按照固定的顺序执行就一定能拼成完成的牌。
如果您觉得这篇文章对您有所启发或帮助请不吝点赞、转发与您的朋友和家人分享。 本文首发在“程序员coding”公众号欢迎关注与我一起交流学习。