Skip to content
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
69 changes: 59 additions & 10 deletions python/linked_list.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,68 @@
class LinkList:
# write your __init__ method here that should store a 'head' value which the first Node in the LinkedList and a 'length' value which is the total number of Nodes in the LinkedList
class LinkList():
def __init__(self, head=None):
self.head = head
self.length = 1

Choose a reason for hiding this comment

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

Since head defaults to none, it's possible to start with a length of 0, so something with the ternary operator like:

Suggested change
self.length = 1
self.length = 0 if head == None else 1

may work

print(self.head)

Choose a reason for hiding this comment

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

It's probably best practice to remove unnecessary print statements before requesting to pull

Copy link
Author

Choose a reason for hiding this comment

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

Hey, I just saw these! Thank you for the feedback, these all help a lot.

# we're going to store the length of the Linked List here

def add(self, data):
# write your code to ADD an element to the Linked List
pass
self.length += 1
#for node in range(self.length-1)
node = self.head

Choose a reason for hiding this comment

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

Don't forget to check if self.head is None or not! Currently, making a LinkList with the default head value of None and adding to it would break the program, since you'd be calling None.next

while node.next != None:
node = node.next
node.next = data

def remove(self, data):
# write your code to REMOVE an element from the Linked List
pass
self.length -=1

Choose a reason for hiding this comment

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

Don't forget to check if the length is equal to 0! You don't want to be having a length of -1

node = self.head
if node == data:
self.head = node.next
else:
while data != node:
previous = node
node = node.next
if node.next == None:
previous.next = None
else:
previous.next = node.next

def get(self, element):
node = self.head
for index in range(self.length-1):
if node == element:
return index
else:
node = node.next

def get(self, element_to_get):
# write you code to GET and return an element from the Linked List
pass

# ----- Node ------
class Node:
# store your DATA and NEXT values here
pass
def __init__(self, value):
self.value = value
self.next = None

a = Node('a')
b = Node('b')
c = Node('c')
d = Node('d')

test = LinkList(a)
test.add(b)
test.add(c)
test.add(d)


print(test.head.value)
print(test.head.next.value)
print(test.head.next.next.value)
print(test.head.next.next.next.value)

print(test.get(c))
print('-'*25)
Comment on lines +46 to +63

Choose a reason for hiding this comment

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

I like that you're testing out your LinkList, but it's best form to delete these before pull requesting, or at least putting them inside a if(name == 'main') statement so it doesn't run if someone wants to import your LinkList

Choose a reason for hiding this comment

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

Also, this type of testing is perfect for a separate unittest file, and ideally that's where it should be. But I understand if you were running out of time and just wanted a quick and dirty way to test.


# print(test.head.value)
# print(test.head.next.value)
# print(test.head.next.next.value)
# print(test.head.next.next.next.value)
35 changes: 26 additions & 9 deletions python/queue.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
class Queue:
# write your __init__ method here that should store a 'total' value which is the total number of elements in the Queue and a 'queue' value which is an array of stored values in the Queue
def __init__(self):
self.length = 0
self.queue = []

def enqueue(self):
# write your code to add data to the Queue following FIFO and return the Queue
pass

def dequeue(self, data):
# write your code to removes the data to the Queue following FIFO and return the Queue
pass
def enqueue(self,data):
self.queue.append(data)
self.length += 1

def dequeue(self):
self.queue.pop(0)
self.length -= 1
Comment on lines +12 to +13

Choose a reason for hiding this comment

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

Don't forget to check if the length is already 0!


def size(self):
# write your code that returns the size of the Queue
pass
return self.length

q = Queue()
a = 'a'
b = 'b'
c = 'c'
d = 'd'

print(q.queue)
q.enqueue(a)
q.enqueue(b)
q.enqueue(c)
q.enqueue(d)
print(q.queue, q.length)
q.dequeue()
print(q.queue, q.length)
33 changes: 26 additions & 7 deletions python/stack.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,33 @@
class Stack:
def __init__(self):
self.length = 0
self.stack = []

# write your __init__ method here that should store a 'total' value which is the total number of elements in the Stack and a 'stack' value which is an array of stored values in the Stack

def push(self):
def push(self, data):
# write your code to add data following LIFO and return the Stack
pass

def pop(self, data):
self.stack = [data] + self.stack
self.length += 1
def pop(self):
# write your code to removes the data following LIFO and return the Stack
pass
self.stack.pop(0)
self.length -= 1
Comment on lines +14 to +15

Choose a reason for hiding this comment

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

Don't forget to check if self.length = 0!


def size(self):
# write your code that returns the size of the Stack
pass
return self.length

s = Stack()
a = 'a'
b = 'b'
c = 'c'
d = 'd'

print(s.stack)
s.push(a)
s.push(b)
s.push(c)
s.push(d)
print(s.stack, s.length)
s.pop()
print(s.stack, s.length)