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 printallcombinations method per the comments in the method? Note it should not use Python's native itertools but rather the nextcombination method.
import collections
class Combination:
def initself values, subsetlen:
setvalues sortedsetvalues
self.combinationset listsetvalues
self.subsetlength subsetlen
self.currentcombination listselfcombinationset:subsetlen
def resetcombinationself:
self.currentcombination listselfcombinationset:self.subsetlength
def getcombinationself:
return listselfcurrentcombination
def setcombinationself combo:
setcombo sortedsetcombo
if lensetcombo self.subsetlength:
return
for value in setcombo:
if value not in self.combinationset:
return
self.currentcombination listcombo
def printallcombinationsself:
#TODO: Make sure you start at the beginning see the
# resetcombination method
#TODO: Print combination
#TODO: Generate a next Combination
#TODO: Repeat It helps if your nextcombination method returns a boolean
self.resetcombination
while True:
printselfcurrentcombination
if not self.nextcombination:
break
# pass
def nextcombinationself:
#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 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 resetcombination 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 pk is less than value of pk
#use startPos to track pk
startPos
#create temporary combinationset list to compare against currentcombination
if lenselfcombinationset lenselfcurrentcombination:
tempCombinationSet self.combinationsetlenselfcurrentcombination:
else:
tempCombinationSet self.combinationset
for i in rangelenselfcurrentcombination:
if self.currentcombinationi tempCombinationSeti:
startPos i
break
elif self.currentcombinationi tempCombinationSeti:
self.resetcombination
return False
#determine amount to increment sublist by
step tempCombinationSet tempCombinationSet
#increment startPos and sublist after
if self.currentcombinationstartPos tempCombinationSetlentempCombinationSet:
self.currentcombinationstartPos step
if self.currentcombinationstartPos self.currentcombinationlenselfcurrentcombination:
#increment everything after startPos
for i in rangestartPos lenselfcurrentcombination:
self.currentcombinationi self.currentcombinationi step
return True
elif self.currentcombinationstartPos tempCombinationSetlentempCombinationSet:
self.resetcombination
printselfcurrentcombination
return False
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
