Hands On: Probability and Statistics -2
Table of Content:
Hands On: Probability and Statistics -2
Combinations and Permutations
Given an array, you should find the number of combinations and permutations when taken 2
elements at a time without a replacement that can be formed from the array of elements.
Function Description
Function Name: comb_perm()
1. Input:
arr - Numpy array
2. Output:
no_of_comb,no_of_perm : Integer
Sample Input For Custom Testing
4
A
B
C
D
Sample Output
(6, 12)
Explanation
No.of Combinations - 6
No. of Permutations - 12
Solution
from itertools import combinations from itertools import permutations import numpy as np import math def comb_perm(arr): #Write your code here ''' Input: arr : numpy array Return : no_of_comb,no_of_perm : Integer ''' n = len(arr) no_of_comb = len(list(combinations(arr, 2))) no_of_perm = math.factorial(n) // math.factorial(n-2) return no_of_comb,no_of_perm if __name__=='__main__': array1=[] n=int(input()) for i in range(n): array1.append(input()) narray1=np.array(array1) print(comb_perm(narray1))