Aug-15-2020, 06:28 PM
I am trying to understand the program written here for Find all possible unique subsets from a set of distinct integers
https://www.w3resource.com/python-exerci...cise-4.php
here in the code i am not sure what
https://www.w3resource.com/python-exerci...cise-4.php
here in the code i am not sure what
return self.subsetsRecur(current, sset[1:]) +self.subsetsRecur(current + [sset[0]], sset[1:])it does, when i see the output i tend to think that python first execute the all calls of
self.subsetsRecur(current, sset[1:])then execute
self.subsetsRecur(current + [sset[0]], sset[1:])and concatenating the results, but i think i may be wrong, because i dont know where this results can be stored ? we are not using much of variables to store, when i see the output values for each calls it is bit confusing, can anyone please confirm or explain this part. Thank you.
class py_solution: def sub_sets(self, sset): return self.subsetsRecur([], sorted(sset)) def subsetsRecur(self, current, sset): print('before if ',current,sset) if sset: print(' if ',current,sset) return self.subsetsRecur(current, sset[1:]) +self.subsetsRecur(current + [sset[0]], sset[1:]) print('After if ', current,sset) return [current] print(py_solution().sub_sets([4,5,6]))The results with prints :
before if [] [4, 5, 6] if [] [4, 5, 6] before if [] [5, 6] if [] [5, 6] before if [] [6] if [] [6] before if [] [] After if [] [] before if [6] [] After if [6] [] before if [5] [6] if [5] [6] before if [5] [] After if [5] [] before if [5, 6] [] After if [5, 6] [] before if [4] [5, 6] if [4] [5, 6] before if [4] [6] if [4] [6] before if [4] [] After if [4] [] before if [4, 6] [] After if [4, 6] [] before if [4, 5] [6] if [4, 5] [6] before if [4, 5] [] After if [4, 5] [] before if [4, 5, 6] [] After if [4, 5, 6] [] [[], [6], [5], [5, 6], [4], [4, 6], [4, 5], [4, 5, 6]]