当前位置:网站首页>Example 001: the number combination has four numbers: 1, 2, 3, 4. How many three digits can be formed that are different from each other and have no duplicate numbers? How many are each?
Example 001: the number combination has four numbers: 1, 2, 3, 4. How many three digits can be formed that are different from each other and have no duplicate numbers? How many are each?
2022-07-05 08:21:00 【Lazy smile】
# example 001: Number combination # There are four numbers :1、2、3、4, How many different and unrepeated three digit numbers can be formed ? How many are each ?
Environmental Science :Pycharm2022 + Anaconda3
First use mathematical permutation and combination analysis, and then Python Realization And summarize the methods
Catalog
Analyze with mathematical permutation and combination
Analyze with mathematical permutation and combination
The first thing I think of is arranging the array composite , Because the title requires no repeated numbers , Therefore, it is the number of permutations . Definition of permutation : from n Among the different elements , Take whatever you like m(m≤n,m And n All are natural numbers , The same below ) Different elements are arranged in a certain order , It's called from n Take out... Of the different elements m An arrangement of elements ; from n Take out... Of the different elements m(m≤n) The number of all permutations of elements , It's called from n Take out... Of the different elements m Number of permutations of elements .
We can conclude that the permutation of this question is 4! / (4-3)! = 4! = 24 ( individual )
Python Realization
Method 1 : We can loop through 3 A digital , Because it is required not to repeat numbers , Therefore, make conditional judgment when outputting , Only output hundreds of digits, tens of digits, and numbers with different single digits .
arrange = 0 # Define the number of permutations
nums = range(1, 5)
# Method 1
for i in nums: # Hundreds of digits
for j in nums: # Ten digits
for k in nums: # Single digit
if ((i != j) and (j != k) and (k != i)): # Only output numbers without repetition
print(i, j, k)
arrange += 1
print(arrange)
The operation results are as follows :
1 2 3
1 2 4
1 3 2
1 3 4
1 4 2
1 4 3
2 1 3
2 1 4
2 3 1
2 3 4
2 4 1
2 4 3
3 1 2
3 1 4
3 2 1
3 2 4
3 4 1
3 4 2
4 1 2
4 1 3
4 2 1
4 2 3
4 3 1
4 3 2
24 individual
Process finished with exit code 0
Method 2 : Can be called directly itertools Of permutations() class ,# itertools, yes python A built-in module for , Powerful , Mainly used for efficient loop creation iterators . Be careful. , He did not return list, It is iterator
import itertools # itertools, yes python A built-in module for , Powerful , Mainly used for efficient loop creation iterators . Be careful. , He did not return list, It is iterator
arrange = 0 # Define the number of permutations
nums = range(1, 5)
for i in itertools.permutations(nums, 3): # ( Iterating elements , Iteration sequence length )
print(i)
arrange += 1
print(arrange)
The operation results are as follows :
(1, 2, 3)
(1, 2, 4)
(1, 3, 2)
(1, 3, 4)
(1, 4, 2)
(1, 4, 3)
(2, 1, 3)
(2, 1, 4)
(2, 3, 1)
(2, 3, 4)
(2, 4, 1)
(2, 4, 3)
(3, 1, 2)
(3, 1, 4)
(3, 2, 1)
(3, 2, 4)
(3, 4, 1)
(3, 4, 2)
(4, 1, 2)
(4, 1, 3)
(4, 2, 1)
(4, 2, 3)
(4, 3, 1)
(4, 3, 2)
24
The second method is used itertools.permutations class , Such contents are as follows: :
class permutations(object):
"""
Return successive r-length permutations of elements in the iterable.
permutations(range(3), 2) --> (0,1), (0,2), (1,0), (1,2), (2,0), (2,1)
"""
def __getattribute__(self, *args, **kwargs): # real signature unknown
""" Return getattr(self, name). """
pass
def __init__(self, range, *args, **kwargs): # real signature unknown; NOTE: unreliably restored from __doc__
pass
def __iter__(self, *args, **kwargs): # real signature unknown
""" Implement iter(self). """
pass
@staticmethod # known case of __new__
def __new__(*args, **kwargs): # real signature unknown
""" Create and return a new object. See help(type) for accurate signature. """
pass
def __next__(self, *args, **kwargs): # real signature unknown
""" Implement next(self). """
pass
def __reduce__(self, *args, **kwargs): # real signature unknown
""" Return state information for pickling. """
pass
def __setstate__(self, *args, **kwargs): # real signature unknown
""" Set state information for unpickling. """
pass
def __sizeof__(self, *args, **kwargs): # real signature unknown
""" Returns size in memory, in bytes. """
pass
permutations Class to create an iterator , return iterable All lengths in are r Project sequence , If you omit r, Then the length of the sequence is the same as iterable The number of items in is the same , A full permutation result will be returned .
边栏推荐
- Basic embedded concepts
- Shape template matching based on Halcon learning [9] PM_ multiple_ dxf_ models. Hdev routine -- [read and write XLD from DXF file]
- Ble encryption details
- C WinForm [get file path -- traverse folder pictures] - practical exercise 6
- Cinq détails de conception du régulateur de tension linéaire
- C, Numerical Recipes in C, solution of linear algebraic equations, LU decomposition source program
- Shape template matching based on Halcon learning [vi] find_ mirror_ dies. Hdev routine
- Explication de la procédure stockée pour SQL Server
- [trio basic tutorial 18 from introduction to proficiency] trio motion controller UDP fast exchange data communication
- Arduino uses nrf24l01+ communication
猜你喜欢
STM32 --- NVIC interrupt
Solutions to compilation warnings in Quartus II
Measurement fitting based on Halcon learning [i] fuse Hdev routine
Explain task scheduling based on Cortex-M3 in detail (Part 1)
Shape template matching based on Halcon learning [viii] PM_ multiple_ models. Hdev routine
STM32 tutorial triple ADC interleaved sampling
Summary -st2.0 Hall angle estimation
Volatile of C language
Circleq of linked list
QEMU STM32 vscode debugging environment configuration
随机推荐
Process communication mode between different hosts -- socket
亿学学堂给的证券账户安不安全?哪里可以开户
动力电池UL2580测试项目包括哪些
99 multiplication table (C language)
Working principle and type selection of common mode inductor
leetcode - 445. 两数相加 II
Void* C is a carrier for realizing polymorphism
C WinForm [view status bar -- statusstrip] - Practice 2
More than 90% of hardware engineers will encounter problems when MOS tubes are burned out!
[paper reading] the latest transfer ability in deep learning: a survey in 2022
MySQL MHA high availability cluster
实例007:copy 将一个列表的数据复制到另一个列表中。
Brief discussion on Buck buck circuit
Installation and use of libjpeg and ligpng
Bluebridge cup internet of things basic graphic tutorial - GPIO output control LD5 on and off
Synchronization of QT multithreading
Soem EtherCAT source code analysis attachment 1 (establishment of communication operation environment)
Stm32--- systick timer
Cinq détails de conception du régulateur de tension linéaire
Briefly talk about the identification protocol of mobile port -bc1.2