FAQ: Recursion: Python - Stack Over-Whoa!


This community-built FAQ covers the “Stack Over-Whoa!” exercise from the lesson “Recursion: Python”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Recursion: Python

FAQs on the exercise Stack Over-Whoa!

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!


My bad, it was wrong the codes in the deleted post. See the one below.

The following codes in 5. Stack overflow Whoa! explanations part are incorrect and generated IndexErrors.

def power_set(set):
power_set_size = 2**len(set)
result =

for bit in range(0, power_set_size):
sub_set =
for binary_digit in range(0, len(set)):
if((bit & (1 << binary_digit)) > 0):
return result

Traceback (most recent call last):
File “recursion.py”, line 16, in
File “recursion.py”, line 9, in power_set
IndexError: list index out of range

The line sub_set.append(set[bit]) is wrong because “bit” has the number of index from 2len(set) which is way more than the actual number of items in “set”. The correct codes I believe should be sub_set.append(set[binary_digit]) for that line and the entire set of codes can be updated as
def power_set(set):
power_set_size = 2
result =

for bit in range(0, power_set_size):
sub_set =
for binary_digit in range(0, len(set)):
if ((bit & (1 << binary_digit)) > 0):
if set[binary_digit] not in sub_set:
return result
Output - [, [‘a’], [‘b’], [‘a’, ‘b’], [‘c’], [‘a’, ‘c’], [‘b’, ‘c’], [‘a’, ‘b’, ‘c’]]


For those who are struggling on what is happening step by step from the base case through the recursion, here it is (I wish somebody did this before me, it took me some minutes to crack it, so i hope this will help):

few abbreviations to make reading easier:
“[empty] is the base case”
“power_set_without_first = pswf”
“without_first = wf”

I.- when the list is just [empty]:
pswf = [empty]
wf = [empty]
pswf + wf = [empty]

II.- when the list is [‘NYU’]:
pswf = [empty] (we now this from the previous recursion)
wf = [[‘NYU’] + [empty]] = [‘NYU’]
pswf + wf = [‘NYU’] (here is actually [‘NYU’, ‘empty’])

III.- when the list is [‘STANFORD’, ‘NYU’]:
pswf = [‘NYU’, [empty]]
(i think the trick here is understanding that is a list with NYU and ‘empty’, so later in “with first” there is will be a case when you sum the [my_list[0]] with [empty]. You can prove this, by replacing the base case with [1] instead of [empty] in the code and see what happens.
wf = [[[‘STANFORD’] + [empty]], [[‘STANFORD’] + [‘NYU’]]]
wf = [[‘STANFORD’], [‘STANFORD’, ‘NYU’]]
pswf + wf = [[empty], [‘NYU’], [‘STANFORD’], [‘STANFORD’, ‘NYU’]]

IV.- when the list is [‘UCLA’, ‘STANFORD’, ‘NYU’]:
pswf = [[empty], [‘NYU’], [‘STANFORD’], [‘STANFORD’, ‘NYU’]]
wf = [[[‘UCLA’] + [empty], [‘UCLA’] + [‘NYU’], [‘UCLA’] + [‘STANFORD’], ['UCLA] + [‘STANFORD’, ‘NYU’]]]
wf = [[‘UCLA’], [‘UCLA’, ‘NYU’], [‘UCLA’, ‘STANFORD’], [‘UCLA’, ‘STANFORD’, ‘NYU’]]
pswf + wf = [[empty], [‘NYU’], [‘STANFORD’], [‘STANFORD’, ‘NYU’], [‘UCLA’], [‘UCLA’, ‘NYU’], [‘UCLA’, ‘STANFORD’], [‘UCLA’, ‘STANFORD’, ‘NYU’]]

and this go on to the final case with ‘MIT’ included.

Please if anybody finds an error on this, feel free to reply.