Algorithm related problems

Time:2021-3-3

1. Permutation problem

You need to use Python’s itertools module

import itertools
a=[1,2,3]

#Take, arrange (mathematical formula: A32)
for i in  itertools.permutations (a, 2): # 2 is taken twice. A can be a string or a list
    print(i)

'''
(1, 2)
(1, 3)
(2, 1)
(2, 3)
(3, 1)
(3, 2)
'''

#Combination (mathematical formula: C32)
# for i in itertools.combinations(a,2):
#     print(i)
'''
(1, 2)
(1, 3)
(2, 3)
'''

#Permutation with put back (Cartesian product)
# for i in itertools.product(a,repeat=2):
#     print(i)

'''
(1, 1)
(1, 2)
(1, 3)
(2, 1)
(2, 2)
(2, 3)
(3, 1)
(3, 2)
(3, 3)
'''

#There are combinations to put back
# for i in itertools.combinations_with_replacement(a, 2):
#     print(i)
'''
(1, 1)
(1, 2)
(1, 3)
(2, 2)
(2, 3)
(3, 3)
'''