Question: May I please have some help with the logic in Python for creating the print _ all _ combinations method per the comments in the

May I please have some help with the logic in Python for creating the print_all_combinations method per the comments in the method? Note it should not use Python's native itertools but rather the next_combination method.
import collections
class Combination:
def __init__(self, values, subset_len):
set_values = sorted(set(values))
self.combination_set = list(set_values)
self.subset_length = subset_len
self.current_combination = list(self.combination_set[:subset_len])
def reset_combination(self):
self.current_combination = list(self.combination_set[:self.subset_length])
def get_combination(self):
return list(self.current_combination)
def set_combination(self, combo):
set_combo = sorted(set(combo))
if len(set_combo)!= self.subset_length:
return
for value in set_combo:
if value not in self.combination_set:
return
self.current_combination = list(combo)
def print_all_combinations(self):
#TODO: Make sure you start at the beginning (see the
# reset_combination method)
#TODO: Print combination
#TODO: Generate a next Combination
#TODO: Repeat (It helps if your next_combination method returns a boolean)
self.reset_combination()
while True:
print(self.current_combination)
if not self.next_combination():
break
# pass
def next_combination(self):
#TODO: Move from right to left in both the
# currentCombination and the combinationSet
# until the numbers do not match. (Hint, use negative indexing)
#TODO: Find startPos as 1 plus the position of the
# number from the current combination that did
# not match in the combinationSet. Fill in
# from left to right in the currentCombination
# starting at the position of the mismatch the
# numbers from the combinationSet starting at
# the startPos you just found
#TODO: If there was no mismatch start combination over at the
# first subset. (see the reset_combination method).
# It is helpful to return a boolean indicating if the combination has been reset.
# let p be a sequence representing the current permutation, with k representing the index
# starting from end of sequence, move to left until value of p[k] is less than value of p[k+1]
#use startPos to track p[k]
startPos =-1
#create temporary combination_set list to compare against current_combination
if len(self.combination_set)!= len(self.current_combination):
tempCombinationSet = self.combination_set[len(self.current_combination):]
else:
tempCombinationSet = self.combination_set
for i in range(len(self.current_combination)-1,-1,-1):
if self.current_combination[i]!= tempCombinationSet[i]:
startPos = i
break
elif self.current_combination[i]!= tempCombinationSet[i]:
self.reset_combination()
return False
#determine amount to increment sub-list by
step = tempCombinationSet[1]- tempCombinationSet[0]
#increment startPos and sub-list after
if self.current_combination[startPos]!= tempCombinationSet[len(tempCombinationSet)-1]:
self.current_combination[startPos]+= step
if self.current_combination[startPos]!= self.current_combination[len(self.current_combination)-1]:
#increment everything after startPos
for i in range(startPos +1, len(self.current_combination)):
self.current_combination[i]= self.current_combination[i-1]+ step
return True
elif self.current_combination[startPos]== tempCombinationSet[len(tempCombinationSet)-1]:
self.reset_combination()
print(self.current_combination)
return False

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!