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

Scissors - Gloria V. #44

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion stacks_queues/linked_list.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

class EmptyListError(Exception):
pass

Expand Down
105 changes: 94 additions & 11 deletions stacks_queues/queue.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,36 @@
from typing import ItemsView


INITIAL_QUEUE_SIZE = 20

class QueueFullException(Exception):
pass
def __init__(self, *args):
if args:
self.message = args[0]
else:
self.message = None

def __str__(self):
print('calling str')
if self.message:
return 'QueueFullException, {0}'.format(self.message)
else:
return 'QueueFullException has been raised'


class QueueEmptyException(Exception):
pass
def __init__(self, *args):
if args:
self.message = args[0]
else:
self.message = None

def __str__(self):
print('calling str')
if self.message:
return 'QueueEmptyException, {0}'.format(self.message)
else:
return 'QueueEmptyException has been raised'

class Queue:

Expand All @@ -23,39 +48,97 @@ def enqueue(self, element):
In the store are occupied
returns None
"""
pass
if ((self.rear +1) % INITIAL_QUEUE_SIZE ==self.front):
raise QueueFullException('This will break it')

elif (self.front == -1):
self.front = 0
self.rear = 0
self.store[self.rear] = element
self.size = self.size + 1
else:

# next position of rear
self.rear = (self.rear + 1) % INITIAL_QUEUE_SIZE
self.store[self.rear] = element
self.size = self.size + 1

def dequeue(self):
""" Removes and returns an element from the Queue
Raises a QueueEmptyException if
The Queue is empty.
The Queue is empty. In the circular Queue it remove the item at the front
"""
Comment on lines 66 to 70

Choose a reason for hiding this comment

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

👍

pass

if self.empty():
raise QueueEmptyException("The Stack is empty")

# condition for only one element
elif (self.front == self.rear):
tmp=self.store[self.front]
self.front = -1
self.rear = -1
self.size -=1
return tmp

else:
tmp = self.store[self.front]
self.front = (self.front + 1) % INITIAL_QUEUE_SIZE
self.size -=1
return tmp

def front(self):

Choose a reason for hiding this comment

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

👍

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


def size(self):

Choose a reason for hiding this comment

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

👍

""" Returns the number of elements in
The Queue
"""
pass

return self.size
# self if a reference to the current class when in a class method you need to append self to it.
def empty(self):
""" Returns True if the Queue is empty
And False otherwise.
"""
pass
if self.size == 0:
return True
return False
Comment on lines +108 to +110

Choose a reason for hiding this comment

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

This is a bit simpler

Suggested change
if self.size == 0:
return True
return False
return self.size == 0



def __str__(self):

Choose a reason for hiding this comment

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

👍

""" 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.

"""
pass
tmp = []
if(self.front == -1):
print ("Queue is Empty")

elif (self.rear >= self.front):
for i in range(self.front, self.rear + 1):
tmp.append(self.store[i])


else:
for i in range(self.front, INITIAL_QUEUE_SIZE):
tmp.append(self.store[i])
for i in range(0, self.rear + 1):
tmp.append(self.store[i])


if ((self.rear + 1) % INITIAL_QUEUE_SIZE == self.front):
print("Queue is Full")

return str(tmp)





39 changes: 35 additions & 4 deletions stacks_queues/stack.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
from stacks_queues.linked_list import LinkedList

class StackEmptyException(Exception):
pass
'''
https://towardsdatascience.com/how-to-define-custom-exception-classes-in-python-bfa346629bca
'''
def __init__(self, *args):
if args:
self.message = args[0]
else:
self.message = None

def __str__(self):
print('calling str')
if self.message:
return 'StackEmptyException, {0}'.format(self.message)
else:
return 'StackEmptyException has been raised'
'''
raise StackEmptyException # MyCustom Error is being raised without any arguments, so None will be sent to the message attribute in the object. The str method will be called and will print the message 'MyCustomEror message has been raised'
raise StackEmptyException('We have a problem')
'''



class Stack:

Choose a reason for hiding this comment

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

👍 The stack methods look good


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


def pop(self):
""" Removes an element from the top
Expand All @@ -21,13 +42,23 @@ def pop(self):
The Stack is empty.
returns None
"""
pass
try:
return self.store.remove_first()

except LinkedList.EmptyListError():
raise StackEmptyException("The Stack is empty")





def empty(self):
""" Returns True if the Stack is empty
And False otherwise
"""
pass
if self.store.empty():
return True
return False

def __str__(self):
""" Returns the Stack in String form like:
Expand Down