Permutation - What It Is.
In the unlikely scenario that the Teacher wanted to see just how any four pupils, in a class of six (6), could be seated on a four-person desk; what this Teacher would be doing in essence is called Permutation (nPr).
Code for Doing Permutation in Python
The algorithm for Permutation - nPr, possible ways of arrangement - will simply be based on that of combination.
All that is needed after combination is a rotation / shuffle of
the make-up / constituents of each possible combination result.
This shuffle simply involves interchanging the elements of the
combination group of size, r, to take all possible positions
starting from the extreme right to extreme left.
This is how our Permutation code in Python will work.
Create a new Python module file;
Call it Permutation.py
Type out the adjoining Python code for Permutation
(nPr).
Advice: You might want to keep the mother-class size (n)
and the group-size (r) small to avoid the permutation code taking too long.
As a rule-of-thump, DO NOT ASK QUESTIONS YOU DON'T WANT TO KNOW THE ANSWER TO.
Python Code for Permutation.py Module
# define a class
class Transposition(Combinatorial):
def __init__(self):
super().__init__()
# till the ground for shuffle to grind on
def possibleWordPermutations(self, candidates, size):
self.perm_store = []
self.possibleWordCombinations(candidates, size)
# illegal 'r' value
if len(self.comb_store) == 0 or self.r == 1:
self.perm_store = self.comb_store
else:
last_two = [[], []]
for i in range(len(self.comb_store)):
self.index = self.r - 1
# copy up last two elements of 'comb_store(i)'
last_two[0] = [self.comb_store[i][self.index], self.comb_store[i][self.index - 1]]
last_two[1] = last_two[0][::-1]
self.index -= 2
self.local_store = []
self.local_store.append(tuple(last_two[0]))
self.local_store.append(tuple(last_two[1]))
if self.r > 2:
self.shuffleWord(self.local_store, i)
self.perm_store.extend(self.local_store)
return self.perm_store
def shuffleWord(self, arg_store, i):
self.local_store = []
for option in (arg_store):
members = []
members.extend(list(option))
# add 'index' 'comb_store[i]' element to this list of members
members.append(self.comb_store[i][self.index])
shift_index = len(members)
# shuffle this pack of words
while shift_index > 0:
# skip if already in store
if (tuple(members) in self.local_store) == False:
self.local_store.append(tuple(members))
shift_index -= 1
if shift_index > 0 and members[shift_index] != members[shift_index - 1]:
# interchange these two neighbours
members[shift_index - 1], members[shift_index] = members[shift_index], members[shift_index - 1]
# Are there any elements left? repeat if yes
if self.index > 0:
self.index -= 1
self.shuffleWord(self.local_store, i)
Main Class
from Permutation import Transposition
# Use the combination module/class
goods = ["Eno", "Chidi", "Olu", "Ahmed", "Osas", "Gbeda"]
perm = Transposition()
result = perm.possibleWordPermutations(goods, 3)
# print choices and operation
print("\n", perm.words, " permutation ", perm.r, ":\n")
# print out permutations nicely
i = 0
for group in result:
i += 1
print(i, ": ", group)
print("\n\nNumber of ways is ", len(result), ".\n")