|
| 1 | +# Code to generate all permutations of a list of elements |
| 2 | +def generate_permutations(elements): # This code generates all permutations |
| 3 | + if len(elements) == 0: # edge case: empty list |
| 4 | + return [[]] # base case: single permutation of empty list is a list with an empty list |
| 5 | + |
| 6 | + perms = [] # list to store all permutations |
| 7 | + for i in range(len(elements)): # iterate through each element |
| 8 | + current = elements[i] # select the current element |
| 9 | + remaining = elements[:i] + elements[i+1:] # get the remaining elements |
| 10 | + for p in generate_permutations(remaining): # recursively generate permutations of remaining elements |
| 11 | + perms.append([current] + p) # append current element to each permutation of remaining elements |
| 12 | + |
| 13 | + return perms |
| 14 | +# Example usage |
| 15 | +if __name__ == "__main__": |
| 16 | + elements = [1, 2, 3] |
| 17 | + permutations = generate_permutations(elements) # generates all permutation elements |
| 18 | + for perm in permutations: |
| 19 | + print(perm) # return the list of permutations |
| 20 | + print(f"Total permutations of {len(elements)} elements: {len(permutations)}") #Total number of permutations |
| 21 | + # This code generates all permutations taking a list of 3 elements as input. |
| 22 | + |
| 23 | +# code for permuting r elements from n elements (General formullacode for counting number of permutations, above resulted will be output if r=n) |
| 24 | +def generate_permutations_r(elements, r): # This code generates all permutations of r elements from the list of elements |
| 25 | + if r == 0: # edge case: permutation of length 0 |
| 26 | + return [[]] # base case: single permutation of length 0 is a list with an empty list |
| 27 | + if len(elements) < r: # edge case: not enough elements to permute |
| 28 | + return [] # no permutations possible |
| 29 | + |
| 30 | + perms = [] # list to store all permutations |
| 31 | + for i in range(len(elements)): # iterate through each element |
| 32 | + current = elements[i] # select the current element |
| 33 | + remaining = elements[:i] + elements[i+1:] # get the remaining elements |
| 34 | + for p in generate_permutations_r(remaining, r - 1): # recursively generate permutations of remaining elements with r-1 |
| 35 | + perms.append([current] + p) # append current element to each permutation of remaining elements |
| 36 | + |
| 37 | + return perms # return the list of permutations |
| 38 | +# Example usage |
| 39 | +if __name__ == "__main__": #calling the function to generate permutations of r elements |
| 40 | + elements = [1, 2, 3, 4] # list of elements |
| 41 | + r = 2 # number of elements to permute |
| 42 | + permutations_r = generate_permutations_r(elements, r) # generate permutations of r elements |
| 43 | + for perm in permutations_r: # console output |
| 44 | + print(perm) # This code generates all permutations of r elements from the list of elements. |
| 45 | +# print the total number of and permutation |
| 46 | + print(f"Total permutations of {r} elements from {len(elements)} elements i.e : {len(permutations_r)}") |
| 47 | + |
| 48 | + |
| 49 | +""" using the formula P(n,r) = C(n,r) * r! to calculate permutation. I will reuse the factorial and combination functions from the previous snippets.""" |
| 50 | + |
| 51 | +def fact(n): |
| 52 | + if (n==0 or n==1): |
| 53 | + return 1 |
| 54 | + else: |
| 55 | + return fact(n-1)*n |
| 56 | +def calculate_combination(n, r): |
| 57 | + if (r==0 or r==n): |
| 58 | + return 1 |
| 59 | + return calculate_combination(n-1, r-1) + calculate_combination(n-1, r) |
| 60 | +def calculate_permutation(n, r): |
| 61 | + return calculate_combination(n, r) * fact(r) |
| 62 | +if __name__ == "__main__": |
| 63 | + n = int(input("Enter the value of n: ")) |
| 64 | + r = int(input("Enter the value of r: ")) |
| 65 | + # This code calculates the number of combinations of r elements from n elements |
| 66 | + print(f"Total combinations C({n},{r}) is: {calculate_combination(n, r)}") |
| 67 | + #This code calculates the number of permutations of r elements from n elements using the formula P(n,r) = C(n,r) * r! |
| 68 | + print(f"Total permutations P({n},{r}) is: {calculate_permutation(n, r)}") |
| 69 | + |
| 70 | +# This code calculates the number of permutations of r elements from n elements using the formula P(n,r) = n! / (n-r)! |
| 71 | +def calculate_permutation_formula(n, r): |
| 72 | + return fact(n) // fact(n - r) |
| 73 | +if __name__ == "__main__": |
| 74 | + n = int(input("Enter the value of n: ")) |
| 75 | + r = int(input("Enter the value of r: ")) |
| 76 | + # This code calculates the number of permutations of r elements from n elements using the formula P(n,r) = n! / (n-r)! |
| 77 | + print(f"Total permutations P({n},{r}) using formula is: {calculate_permutation_formula(n, r)}") |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | + |
0 commit comments