Question: I ' m stuck on creating the next _ combination method in Python using the attached algorithm. Could someone please help with translating this into
Im stuck on creating the nextcombination method in Python using the attached algorithm. Could someone please help with translating this into Python code?
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
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.
pass
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
