I have a dfs function (permutation) in which it takes long time to compute all possible values; I wish to create a generator such that every time I call a function get_value
it will provide a possible outcome. So in the example below when calling get_value
3 times the results should be:
['1', '2', '3', '4']
['1', '2', '4', '3']
my current execution:
class Solution:
def permutation(self, lst):
if len(lst) == 0:
return []
if len(lst) == 1:
return [lst]
l = []
for i in range(len(lst)):
m = lst[i]
remLst = lst[:i] + lst[i+1:]
for p in self.permutation(remLst):
l.append([m] + p)
return l
#def get_value():
# yield ???
if __name__=='__main__':
s = Solution()
r = s.permutation(['1','2','3','4'])
for p in r:
print (p)
#what I want is:
s = Solution()
v1 = s.get_value() #['1', '2', '3', '4']
v2 = s.get_value() #['1', '2', '4', '3']
#and so forth