FAQ: Stacks: Python - Stacks Python Size II


#1

This community-built FAQ covers the “Stacks Python Size II” exercise from the lesson “Stacks: Python”.

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

Computer Science

Linear Data Structures

FAQs on the exercise Stacks Python Size II

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!


#2

I got a message for the very last part that asked: Does the size return True when it is equal to 0? My answer was yes, but it didn’t think so. I got fed up and looked at the solution.

MY CODE

  # Define has_space() and is_empty() below:
  def has_space(self):
    if self.limit > self.size:
      return True
    else:
      return False
  
  def is_empty(self):
    if self.size == 0:
      return True
    else:
      return False

THE SOLUTION

 def has_space(self):
    return self.limit > self.size
  
  def is_empty(self):
    return self.size == 0

It’s clear to me that the solution is a better way to do it, but my code seemed to work for the has_space, but not for the is_empty. Why?


#4

Shouldn’t these be equivalent? Am I wrong? What do you think?


#5

Returning boolean literals is the naive approach to coding, which this course is well beyond, preferring return expressions over literals.

self.limit > self.size

self.size == 0

are both boolean expressions.


#6

I understand my method isn’t the best way and the solution is definitely better.

If i understand what you are saying. Technically my code should work (that it is equivalent), but it’s so redundant that the exercise didn’t accept it?


#7

Redundant might not be the appropriate word. Naive is the word I chose. The SCT author wants us to move away from the beginner style to the intermediate. Technically there is nothing wrong with your code as it stands, just that it uses literals instead of expressions.


#9

As I understand, Stacks not always have a limit. In this case, should function has_space take this option into consideration, so it does not raise an error when limit = None?