Python Forum
Having hard time understanding the function self-returning itself twice
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Having hard time understanding the function self-returning itself twice
#1
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
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]]
Reply


Messages In This Thread
Having hard time understanding the function self-returning itself twice - by jagasrik - Aug-15-2020, 06:28 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Hard time trying to figure out the difference between two strings carecavoador 2 752 Aug-16-2023, 04:53 PM
Last Post: carecavoador
  return next item each time a function is executed User3000 19 2,607 Aug-06-2023, 02:29 PM
Last Post: deanhystad
  Understanding a function ebolisa 3 864 Jul-14-2023, 06:03 PM
Last Post: snippsat
  Understanding venv; How do I ensure my python script uses the environment every time? Calab 1 2,463 May-10-2023, 02:13 PM
Last Post: Calab
  Why my function is returning None? PauloDAS 6 1,946 Jul-17-2022, 11:17 PM
Last Post: Skaperen
  time function does not work tester_V 4 3,179 Oct-17-2021, 05:48 PM
Last Post: tester_V
  Why recursive function consumes more of processing time than loops? M83Linux 9 4,414 May-20-2021, 01:52 PM
Last Post: DeaD_EyE
  Pausing and returning to function? wallgraffiti 1 2,205 Apr-29-2021, 05:30 PM
Last Post: bowlofred
  Can you end the Time.sleep function boier96 9 9,861 Jan-16-2021, 10:09 PM
Last Post: Serafim
  Why is the function returning None for a * b instead of number? omm 10 4,465 Nov-05-2020, 01:17 PM
Last Post: omm

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020