## current position：Home>Introduction to Interesting Algorithms in Python - How Much Do You Know About Borrowing Solutions

# Introduction to Interesting Algorithms in Python - How Much Do You Know About Borrowing Solutions

2022-11-08 03:33:11【please call me】

### Problem description

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?

### Analysis

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")`

### Output

`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`

copyright notice

author[please call me],Please bring the original link to reprint, thank you.

https://en.pythonmana.com/2022/312/202211080319247215.html

## The sidebar is recommended

- 12 essential Python functions, beginners can get started after reading them
- [oeasy]python0011_ character number_ordinal_ord
- Python crawling CCTV video
- Detailed explanation of Luogu Thousand Questions | P1010 [NOIP1998 Popularization Group] Power [C++, Java, Python, Pascal Language]
- Huawei computer test real test practical application [competition code] - hardware resource allocation (with Python and Java code)
- Detailed explanation of Luogu Thousand Questions | P1005 [NOIP2007 Improvement Group] Matrix Number Game [C++, Java, Python Language]
- Detailed explanation of Luogu Thousand Questions | P1003 [NOIP2011 Improvement Group] Laying the Carpet [C++, Java, Python Language]
- Detailed explanation of Luogu Thousand Questions | P1006 [NOIP2008 Improvement Group] Passing Notes [C++, Java, Python Language]
- Detailed explanation of Luogu Thousand Questions | P1001 A+B Problem [C++, Pascal, Kotlin, Python, Java, JavaScript, Ruby, PHP, Rust, Go, C#, Scala, Perl]
- Detailed explanation of Luogu Thousand Questions | P1008 [NOIP1998 Popularization Group] Triple Hit [C++, Java, Python Language]

## guess what you like

Detailed explanation of Luogu Thousand Questions | P1009 [NOIP1998 Popularization Group] Sum of factorials [C++, Java, Python, Pascal language]

[oeasy]python0011_ character number_ordinal_ord

Multiple methods of full permutation implemented in python

Local configuration Anaconda+Python+PyCharm+PyTorch deep learning environment

[Environmental Construction] Win10 Python 3.6.4 Installation Tutorial Beginners Easy to Understand

[Environment building] Anaconda installation tutorial Python version correspondence Win10 is easy to understand

【Technology Sharing】Using Python to Realize Xiaoliuren Calculation of auspicious time

Python OpenCV recognizes by color area

python3.7 upgrade pip

Python3 failed to install skimage package

## Random recommended

- python3.7scrapy install scrapy with pip command
- python3.7 'pip3' is not an internal or external command, nor is it a runnable program or batch file
- How to use filters and algorithms to restore pictures and decontaminate pictures in image processing/computer vision/python environment [with code]
- Image processing/computer vision/python environment/machine learning k-nearest neighbor method (KNN), template matching algorithm for data set recognition
- Python from entry to practice - 02 list of basic data types
- Python from entry to practice----01 basic data types
- Image processing/computer vision/python environment/how to deal with noise with four different filters [with code, effective for personal testing]
- Private and public members in python
- Python learning functions, lists and tuples, dictionaries
- How to install python3.11 on windows and use python3.11 to run a quantitative trading strategy written by backtrader and increase the speed by about 20%?
- Mastering Python in 100 Days (Data Analysis) - Day 63: Pandas Using Custom Function Cases
- "Python core technology and actual combat" indispensable custom functions and anonymous functions [functional programming important map(), filter(), reduce()]
- Is it hard to make games in Python - let's see how much fun I make
- The most complete in history - Python installation tutorial
- #Scientific Research Foundation# Python Beginner's Notes for Self-Use Part 4 Tuples, Dictionaries and Sets
- #Scientific research building base# Python beginner's self-use notes Part 3 Conditional Statements and Loop Statements
- Nested loop in Python
- openGauss 2.1.0 upgrade 3.1.0 encountered Exception: [GAUSS-52200] : When compiling python error
- Selenium+python to do web-side automated testing framework and example detailed tutorial
- Python Regular Expression
- I wrote a financial data crawler in Python, and I did the work of the interns for a week in half an hour
- Python project combat - bank ATM system (3)
- Python decimal to binary function_python hexadecimal to binary
- Python toolbox series (12)
- python automatic send mail script
- [Study Notes] Introduction to Deep Learning: Theory and Implementation Based on Python
- How python handles program exceptions
- Python Volunteer Management System Based on PHP+MySQL
- python is based on PHP+MySQL university asset management system
- Python is based on PHP+MySQL reading sharing platform
- Python student association management system based on PHP+MySQL
- Notes for python importing other modules under windows
- What exactly does Python's __pycache__ cache?Can the historical running results be cached?- A little research on PyCache
- Compare similarity of two vectors - python
- Boutique flower sales shopping mall applet based on Python
- python get all files in current directory
- python gets computer CPU, motherboard, BIOS, hard disk serial number, host name, IP, Mac address
- python get computer unique identifier
- Python converts strings to hexadecimal (encryption and decryption)
- python based on PHP+MySQL college students job recruitment website