Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maple Monica C. #42

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Maple Monica C. #42

wants to merge 4 commits into from

Conversation

mcatcruz
Copy link

Stacks and Queues

Thanks for doing some brain yoga. You are now submitting this assignment!

Comprehension Questions

Question Answer
What is an ADT? An abstract data type is an object whose description includes the operations it can perform, but not the implementation.
Describe a Stack A data structure that stores a list of data that can be accessed in Last-In-First-Out (LIFO) order. Y
What are the 5 methods in Stack and what does each do? Pop removes and returns an item from the top of the stack. Push adds an item on top of the stack. Is_empty checks if the stack is empty. Peek returns the item on top of the stack, but does not remove it. Size checks the length of the stack.
Describe a Queue A data structure that stores data that can be accessed in First-In-First-Out (FIFO) order.
What are the 5 methods in Queue and what does each do? Enqueue adds an item to the back of the queue. Dequeue removes and returns an item from the front of the queue. Empty checks if the queue is empty. Front returns an element from the front of the queue, but doesn't remove it. Size checks the length of the queue.
What is the difference between implementing something and using something? Implementation means creating something with another thing (e.g. Creating a stack with an array). Using is to apply something without necessarily knowing the details of how that something is created.

OPTIONAL JobSimulation

Question Answer
Did you include a sample run of your code as a comment?

Copy link

@kyra-patton kyra-patton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✨ Nice work Monica! I left suggestions on your __str__ implementations for both Stack and Queue.

Re: Implementation vs Using, implementation doesn't necessarily mean you are using another 'thing' as in one data type to define another data type, just that you are writing the code to make a function or larger piece of code work yourself. You are correct, using just means you are using code other people wrote.

Let me know what questions you have.

🟢


def enqueue(self, element):
""" Adds an element to the Queue
Raises a QueueFullException if all elements
In the store are occupied
returns None
"""
pass

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


def dequeue(self):
""" Removes and returns an element from the Queue
Raises a QueueEmptyException if
The Queue is empty.
"""
pass

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


def front(self):
""" Returns an element from the front
of the Queue and None if the Queue
is empty. Does not remove anything.
"""
pass
return self.store[self.front]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

def size(self):
""" Returns the number of elements in
The Queue
"""
pass
return self.size

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


def empty(self):
""" Returns True if the Queue is empty
And False otherwise.
"""
pass
return self.size == 0

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


def empty(self):
""" Returns True if the Queue is empty
And False otherwise.
"""
pass
return self.size == 0

def __str__(self):
""" Returns the Queue in String form like:
[3, 4, 7]
Starting with the front of the Queue and
ending with the rear of the Queue.
"""

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👀 See comment below ⬇️

@@ -12,7 +12,7 @@ def push(self, element):
""" Adds an element to the top of the Stack.
Returns None
"""
pass
self.store.add_first(element)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -21,18 +21,21 @@ def pop(self):
The Stack is empty.
returns None
"""

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if self.store.empty():
raise StackEmptyException
else:
return self.store.remove_first()

def empty(self):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


def __str__(self):
""" Returns the Stack in String form like:
[3, 4, 7]
Starting with the top of the Stack and
ending with the bottom of the Stack.
"""
pass
return self.store.__str__()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The __str__ method is an example of what we call operator overloading. Operator overloading is used for methods and operators such as str(), ==, and + where Python actually has a predefined way it will attempt to do these operations or methods on any object or data type. Methods like __str__, __eq__, and __add__ allow us to either extend or overwrite the predefined functionality with our own desired functionality.

When we implement the function, we use the dunders around the function name (ex. __str__ ) to indicate that we are about to do operator overloading. However, when we call the function, we can remove the dunders.

Suggested change
return self.store.__str__()
return str(self.store)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants