please call me 2022-11-08 03:33:11 阅读数:446

introductioninterestingalgorithmspythonknow

Xiao Ming has 5 new books and wants to lend them to three children A, B and C. If each person can only borrow 1 book at a time, how many different ways can they borrow?

This problem is a typical permutation and combination problem, but there are sequential requirements. For example, if the same three people lent books 1, 2, and 3, then A, B, and C borrow 1, 2, 3 and C,B and A borrowing 1, 2, and 3 are two different borrowing methods.Because the amount of data is small enough (5 books, 3 people), exhaustiveness can be used.

It is foreseeable that we need to set up a three-layer loop to simulate different borrowing scenarios.If A borrows first, then A can choose one of the 5 books, that is, there are 5 choices, while B can only choose from the remaining 4 books. After B has selected the book, C can only chooseChoose from the remaining 3 books.Although through simple calculation, it can be calculated that there are a total of Different ways of borrowing.But enumerating all the borrowing scenarios through the computer is the embodiment of computer thinking:

`books = set(range(1,6))res = []for a in books:for b in books-{a}:for c in books-{a,b}:res.append((a,b,c))print(f"A borrows book {a}, B borrows book {b}, C borrows book {c}")print(f"There are {len(res)} kinds of borrowing methods")`

Here we can use numbers 1 to 5 to indicate the number of books, and the books that can be borrowed are a set, so when A borrows a book, the set that B can borrow is the original set minus theA borrowed book, so here you can use the subtraction of the set to generate a new set.Similarly, when C borrows books, the set of available books is the original set minus the books borrowed by A and B.In this way, we can do full traversal.

But as we all know, the advantage of Python lies in its large number of functions and modules. As an interpreted scripting language, it does not require learners to pay too much attention to the underlying C implementation. Therefore, it is often required in C and other languages.The function implemented by the statement can be solved clearly and clearly with one sentence of Python.Therefore, Brother Wen believes that learners who are determined to use Python as the main means to solve problems should pay more attention to the creativity of combining mathematics, implementation methods, and problem solving.In a word, don't reinvent the wheel.

Take this question as an example, for the permutation and combination, Python has done the combinations() and permutations() functions in the built-in module itertools. Using the built-in functions, the result can be obtained in one sentence, so there is no need to consider multiplecycle.

`from itertools import permutationsres = list(permutations(range(1,6),3))for i in res:print(f"A borrows the {i[0]}th book, B borrows the {i[1]}th book, C borrows the {i[2]}th book")print(f"There are {len(res)} kinds of borrowing methods")`

`A borrows book 1, B borrows book 2, and C borrows book 3A borrows the 1st book, B borrows the 2nd book, C borrows the 4th bookA borrows the 1st book, B borrows the 2nd book, C borrows the 5th bookA borrows the 1st book, B borrows the 3rd book, C borrows the 2nd bookA borrows the 1st book, B borrows the 3rd book, C borrows the 4th bookA borrows the 1st book, B borrows the 3rd book, C borrows the 5th bookA borrows the 1st book, B borrows the 4th book, C borrows the 2nd bookA borrows the 1st book, B borrows the 4th book, C borrows the 3rd bookA borrows the 1st book, B borrows the 4th book, C borrows the 5th bookA borrows the 1st book, B borrows the 5th book, C borrows the 2nd bookA borrows the 1st book, B borrows the 5th book, C borrows the 3rd bookA borrows the 1st book, B borrows the 5th book, C borrows the 4th bookA borrows the 2nd book, B borrows the 1st book, C borrows the 3rd bookA borrows the 2nd book, B borrows the 1st book, C borrows the 4th bookA borrows the 2nd book, B borrows the 1st book, C borrows the 5th bookA borrows the 2nd book, B borrows the 3rd book, C borrows the 1st bookA borrows the 2nd book, B borrows the 3rd book, C borrows the 4th bookA borrows the 2nd book, B borrows the 3rd book, C borrows the 5th bookA borrows the 2nd book, B borrows the 4th book, C borrows the 1st bookA borrows the 2nd book, B borrows the 4th book, C borrows the 3rd bookA borrows the 2nd book, B borrows the 4th book, C borrows the 5th bookA borrows the 2nd book, B borrows the 5th book, C borrows the 1st bookA borrows the 2nd book, B borrows the 5th book, C borrows the 3rd bookA borrows the 2nd book, B borrows the 5th book, C borrows the 4th bookA borrows the 3rd book, B borrows the 1st book, C borrows the 2nd bookA borrows the 3rd book, B borrows the 1st book, C borrows the 4th bookA borrows the 3rd book, B borrows the 1st book, C borrows the 5th bookA borrows the 3rd book, B borrows the 2nd book, C borrows the 1st bookA borrows the 3rd book, B borrows the 2nd book, C borrows the 4th bookA borrows the 3rd book, B borrows the 2nd book, C borrows the 5th bookA borrows the 3rd book, B borrows the 4th book, C borrows the 1st bookA borrows the 3rd book, B borrows the 4th book, C borrows the 2nd bookA borrows the 3rd book, B borrows the 4th book, C borrows the 5th bookA borrows the 3rd book, B borrows the 5th book, C borrows the 1st bookA borrows the 3rd book, B borrows the 5th book, C borrows the 2nd bookA borrows the 3rd book, B borrows the 5th book, C borrows the 4th bookA borrows the 4th book, B borrows the 1st book, C borrows the 2nd bookA borrows the 4th book, B borrows the 1st book, C borrows the 3rd bookA borrows the 4th book, B borrows the 1st book, C borrows the 5th bookA borrows the 4th book, B borrows the 2nd book, C borrows the 1st bookA borrows the 4th book, B borrows the 2nd book, C borrows the 3rd bookA borrows the 4th book, B borrows the 2nd book, C borrows the 5th bookA borrows the 4th book, B borrows the 3rd book, C borrows the 1st bookA borrows the 4th book, B borrows the 3rd book, C borrows the 2nd bookA borrows the 4th book, B borrows the 3rd book, C borrows the 5th bookA borrows the 4th book, B borrows the 5th book, C borrows the 1st bookA borrows the 4th book, B borrows the 5th book, C borrows the 2nd bookA borrows the 4th book, B borrows the 5th book, C borrows the 3rd bookA borrows the 5th book, B borrows the 1st book, C borrows the 2nd bookA borrows the 5th book, B borrows the 1st book, C borrows the 3rd bookA borrows the 5th book, B borrows the 1st book, C borrows the 4th bookA borrows the 5th book, B borrows the 2nd book, C borrows the 1st bookA borrows the 5th book, B borrows the 2nd book, C borrows the 3rd bookA borrows the 5th book, B borrows the 2nd book, C borrows the 4th bookA borrows the 5th book, B borrows the 3rd book, C borrows the 1st bookA borrows the 5th book, B borrows the 3rd book, C borrows the 2nd bookA borrows the 5th book, B borrows the 3rd book, C borrows the 4th bookA borrows the 5th book, B borrows the 4th book, C borrows the 1st bookA borrows the 5th book, B borrows the 4th book, C borrows the 2nd bookA borrows the 5th book, B borrows the 4th book, C borrows the 3rd bookA total of 60 borrowing methods`

版权声明：本文为[please call me]所创，转载请带上原文链接，感谢。 https://pythonmana.com/2022/312/202211080319247215.html

- python基于PHP+MySQL的大学生健康管理系统
- python基于PHP+MySQL的大学生求职招聘网站
- python based on PHP+MySQL college students job recruitment website
- Linear sieve prime number template (Python/C++)
- python university student health management system based on PHP+MySQL
- Python八种数据导入方法，你掌握了吗？赶紧收藏
- Python的字符串和编码
- Python常用字符编码
- Python常见字符编码间的转换教程
- UE4 获取HTTP接口数据 （UE4与python通信）
- Python深度学习实战：测试神经网络
- Python Deep Learning in Action: Testing Neural Networks
- UE4 gets HTTP interface data (UE4 communicates with python)
- python3矩阵移动问题
- python基于PHP+MySQL 校园二手闲置商品交易系统
- python基于PHP+MySQL汽车展览会系统
- MATLAB算法实战应用案例精讲-【集成算法】集成学习模型stacking（附Python和R语言代码）
- 请教一个复杂文本处理问题Python
- python根据进程名获取进程id
- Ask a complex text processing problem Python
- 图解Python编程：从入门到精通系列教程（附全套速查表）
- 【开源分享】基于Python+OpenCV+PyQt5车牌识别(GUI界面)
- 基于Python+OpenCV车道线检测(直道和弯道)
- python每日一题【剑指 Offer 20. 表示数值的字符串】
- python每日一题【剑指 Offer 59 - II. 队列的最大值】
- python每日一题【剑指 Offer 35. 复杂链表的复制】
- 每日一题python【剑指 Offer 59 - I. 滑动窗口的最大值】
- python每日一题【剑指 Offer 58 - II. 左旋转字符串】
- python每日一题【剑指 Offer 30. 包含 min 函数的栈】
- python每日一题【剑指 Offer 24. 反转链表】
- 每日一题python【剑指 Offer 09. 用两个栈实现队列】
- 浅谈Python中列表创建与列表元素增删
- Talking about list creation and list element addition and deletion in Python
- Leetcode第3题 无重复字符的最长子串lengthOfLongestSubstring（附java和python代码）
- LeetCode第5题 最长回文子串longestPalindrome（附java和python代码）
- LeetCode 第10题正则表达式匹配isMatch（java＆python实现）
- Leetcode 第6题 Z 字形变换convert（附java和python代码）
- python 常用包-用pip install -r requirements.txt导入
- LeetCode 第2题两数相加（附java和python代码）
- LeetCode 第8题目 字符串转换整数 (myAtoi)（附java和python代码）
- LeetCode 第7题整数反转reverse（附java和python代码）
- 读书笔记：python+vue实战派
- python 生成本地项目特定文件夹下依赖文件 requirements.txt 的方式
- 特定文件夹下 简单安装隔离的 python 虚环境
- python 性能优化实例练习
- Python求英文文本的平均互信息
- Python-Flask快速上手
- Python牛客刷题笔记
- Python finds the average mutual information of English text
- python脚本重写yaml
- Python NotImplementedError: cannot instantiate ‘PosixPath‘ on your system
- Python小白学习到什么程度才可以做自动化
- Python performance optimization example exercise
- python script rewrite yaml
- Python NotImplementedError: cannot instantiate 'PosixPath' on your system
- How far can a Python novice learn to do automation?
- python-(6-3-1)爬虫---requests入门
- 关于python序列对象浅谈
- Python定义函数
- Python语法入门介绍