usingMaths.com
From Theory to Practice - Math You Can Use.







<< PreviousNext >>

Code for Selection with Repetitions in Python



Possible Selections With Repetition.

Imagine being given the opportunity to pick six (6) exercise books from a book-shop having 10 different brands of exercise books - Ben 10, Chelsea F.C., Superman, Tottenham F.C., Indomitables, Manchester City F.C., Spider Man, Power Rangers, Liverpool F.C. and Bat Man exercise books.
If you happen to be a big Power Rangers fan, nothing stops you from selecting all 6 exercise books to be Power Rangers exercise books.
But you can as well decide to pick only 3 Power Rangers exercise books and make up for the other 3 with any other brands of exercise book.



Code for Repetitive Selection in Python

The algorithm for Selection with Repetition will be a lot similar to that of combination.

  1. Beginning with the first member in the mother set, match it separately with every member - including itself - until the required Selection group-size (r) is reached.
  2. When every possible Selection with this member is exhausted, move to the next member in the mother set and repeat Step I.

This is how our Repetitive Selection code in Python will work.

Create a new Python module file;
Call it Selection.py.
Type out the adjoining Python code for Selection with Repetition.







Python Code for Selection.py Module

# Sure enough this is a module

# define a class
class Choice:

    def __init__(self):
        self.i = 0

    def groupSelection(self, candidates, size):
        self.words = candidates
        self.r = size
        self.complete_group = []
        self.i = 0
        
        self.recursiveFillUp([])

        return self.complete_group

    # pick elements recursively
    def recursiveFillUp(self, temp):
        picked_elements = [[] for k in range(len(self.words))]
        j = self.i
        while j < len(self.words):
            picked_elements[j].extend(temp)
            picked_elements[j].append(self.words[j])
            # recoil factor
            if self.i >= len(self.words):
                self.i = j
                
            # satisfied yet?
            if len(picked_elements[j]) == self.r:
                self.complete_group.append(picked_elements[j])
            elif len(picked_elements[j]) < self.r:
                self.recursiveFillUp(picked_elements[j])
            
            j += 1
        
        j -= 1
        if picked_elements[j] != None and len(picked_elements[j]) == self.r:
            self.i += 1 
        

Main Class

#!/usr/bin/python
from Selection import Choice


# Use the combination module/class
goods = ["0""1""2""3""4""5""6""7""8""9"]

pick = Choice()
result = pick.groupSelection(goods, 6)

# print choices and operation
print("\n", pick.words, " selection ", pick.r, ":\n")

# print out selections nicely
i = 0
for group in result:
    i += 1
    print(i, ": ", group)
    
print("\n\nNumber of ways is "len(result), ".\n")



<< PreviousNext >>