Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help understanding code
#1
Hi,
I have started learning Python a week ago (I have previous experience with C++ and Java) and stumbled over piece of code I have trouble to understand. 
def power_set(l):
   if not l: 
      return [[]]
   return power_set(l[1:]) + [[l[0]] + x for x in power_set(l[1:])]
The code calculates the power set. For power_set({1,2,3,4}) the output is {{}, {1}, {2}, {1,2}, {3}, {1,3}, {2,3}, {1,2,3}, {4}, {1,4}, {2,4}, {1,2,4}, {3,4}, {1,3,4}, {2,3,4}, {1,2,3,4}}. While the if statement is clear, the last is not .

I would be thankful if someone could explain me the last line.
Reply
#2
So, nasty recursive code with part of it wrapped in a list comp... wonderful.

Let's try breaking that line up:
power_set(l[1:]) + [[l[0]] + x for x in power_set(l[1:])]
We have:
power_set(l[1:])
and
[[l[0]] + x for x in power_set(l[1:])]
The first part is easy.  power_set(l[1:]) is the powerset of the current sequence without the first entry.
So if you ask for the power set of [1,2,3,4] it gives the power set of [2,3,4].

The second part is the first character in the sequence + the previous expression.
So if you ask for the powerset of [1,2,3,4] it gives you [1] + each set in the powerset of [2,3,4].


So to try once again to put this into words:
""" The powerset of any sequence is equal to (the powerset of the sequence without the first entry) + (the first entry +  (each set in the powerset of the sequence without the first entry))."""

Not sure if that is any clearer.  I tried. =P
Reply
#3
Thank you very much. I have understood it now.
 
And yeah, it's the kind of recursive methods I wouldn't write myself voluntarily because of the hardness to read it (especially month later).
Reply
#4
sometimes, code like that can earn you elegance points from the Ph.D people ... even if the code is wrong.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Code understanding: Need help in understanding dictionary code jt123 0 470 Jul-09-2023, 01:13 PM
Last Post: jt123
  New to python/coding Need help on Understanding why this code isn't working. Thanks! mat3372 8 1,747 May-09-2023, 08:47 AM
Last Post: buran
  Understanding a piece of code Michael1 4 1,408 Jan-20-2022, 07:14 PM
Last Post: Michael1
  Beginner: I need help understanding few lines of a code. hop_090 1 1,691 Sep-07-2020, 04:02 PM
Last Post: Larz60+
  Extracting Rows From Data Frame and Understanding The Code JoeDainton123 0 1,439 Aug-03-2020, 04:08 PM
Last Post: JoeDainton123
  Help Understanding Code Variables 1 1,910 May-02-2019, 05:53 PM
Last Post: micseydel
  Need help understanding simple Array code. Please. stluwa 1 2,211 Apr-13-2019, 07:16 PM
Last Post: loomski
  Trouble Understanding Why This Code Works crocolicious 2 2,704 Apr-09-2019, 05:24 PM
Last Post: crocolicious
  Help Understanding Portion of Code caroline_d_124 3 2,714 Jan-15-2019, 12:12 AM
Last Post: caroline_d_124
  Understanding GerbMerge code djsb 1 2,392 Oct-27-2018, 02:04 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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