jweinst1
6/6/2015 - 6:12 AM

A Class and function that constructs all possible combinations of length = 2 for a given list. It uses a ring loop, where each function call

A Class and function that constructs all possible combinations of length = 2 for a given list. It uses a ring loop, where each function calls each other until all the pairs are constructed.

class combo_pair_maker (object):
	
	def __init__(self, list):
		self.list = list
		self.template, self.combinations = [], []
		self.dictindex = len(self.combinations) + 1
		self.second_digit, self.index_num = 1, 0
	def combomaker(self): #constructs each pair and appends to a list
		self.template = [self.list[self.index_num], self.list[self.second_digit]]
		self.combinations.append(self.template)
		self.template = []
		combo_pair_maker.combo_checker(self)
	def combo_checker(self): #confirms the indexes being paired
		if self.second_digit < len(self.list) - 1:
			self.second_digit += 1
			combo_pair_maker.combomaker(self)
		if self.second_digit == len(self.list) - 1:
			combo_pair_maker.index_checker(self)
	def index_checker(self):
		if self.index_num < len(self.list) - 2:
			self.index_num += 1
			self.second_digit = self.index_num + 1
			combo_pair_maker.combomaker(self)
		if self.index_num == len(self.list) - 2:
			return self.combinations
def pair_constructor(list):
	pairs = combo_pair_maker(list)
	combo_pair_maker.combomaker(pairs)
	return pairs.combinations