FAQ: Recursion: Python - Stack Over-Whoa!


#1

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!


#3

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):
sub_set.append(set[bit])
result.append(sub_set)
return result

Traceback (most recent call last):
File “recursion.py”, line 16, in
print(power_set(test))
File “recursion.py”, line 9, in power_set
sub_set.append(set[bit])
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
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):
if set[binary_digit] not in sub_set:
sub_set.append(set[binary_digit])
result.append(sub_set)
return result
Output - [, [‘a’], [‘b’], [‘a’, ‘b’], [‘c’], [‘a’, ‘c’], [‘b’, ‘c’], [‘a’, ‘b’, ‘c’]]