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
33 changes: 17 additions & 16 deletions linked_list.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python
from __future__ import unicode_literals


class Node(object):
Expand All @@ -7,32 +8,33 @@ def __init__(self, data, nextNode=None):
self.nextNode = nextNode

def __str__(self):
return self.data
return str(self.data)
Copy link

Choose a reason for hiding this comment

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

You might want to provide some way of differentiating a node that contains a value from the value itself. But this is a vast improvement over what you had before. Good. (same goes for __repr__ below)


def __repr__(self):
return self.data
return repr(self.data)


class LinkedList(object):
def __init__(self, firstNode=None):
self.firstNode = firstNode

def insert(self, newNode):
# insert newNode at beginning of list
def insert(self, val):
# insert val at beginning of list
self.newNode = Node(val)
Copy link

Choose a reason for hiding this comment

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

Will you ever use newNode outside the context of this one method? If not, is there any reason to hook it up to the instance as an attribute like this? You could simply bind newNode locally, without attaching it to self and achieve the same results with fewer lasting side-effects.

if not self.firstNode:
self.firstNode = newNode
self.firstNode = self.newNode
else:
newNode.nextNode = self.firstNode
self.firstNode = newNode
self.newNode.nextNode = self.firstNode
self.firstNode = self.newNode
Copy link

Choose a reason for hiding this comment

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

You are still 'asking permission rather than forgiveness' here.


def pop(self):
# pops first value from list and returns it
if self.size() == 0:
return "THE LIST! IT'S EMPTY!!"
raise ValueError("The list is empty")
Copy link

Choose a reason for hiding this comment

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

Much better. This is usable. Still though, you are asking permission. Don't ask if you can return a value, just try it and clean up when you're done if it fails.

else:
obsoleteNode = self.firstNode
self.firstNode = self.firstNode.nextNode
return obsoleteNode.data
return obsoleteNode.data.encode('utf-8')

def size(self):
# returns length of list
Expand All @@ -46,6 +48,8 @@ def size(self):
def search(self, val):
# return node containing 'val' in list, if present (else None)
currentNode = self.firstNode
if currentNode.data is None:
raise ValueError()
while currentNode.data != val:
if currentNode.nextNode is None:
return None
Expand All @@ -56,15 +60,12 @@ def search(self, val):
def remove(self, node):
# remove node from list, wherever it might be
if self.size() == 0:
return "THE LIST! IT'S EMPTY!!"
raise ValueError("The list is empty")
else:
prevNode = None
currentNode = self.firstNode
foundNode = False
while not foundNode:
if currentNode == node:
foundNode = True
elif currentNode is None:
while currentNode is not node:
if currentNode is None:
Copy link

Choose a reason for hiding this comment

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

Though this is much much cleaner, it's still a bit over-done. As you move forward through the other linked-list-related data structures, see if you can't find more efficient, more readable ways of accomplishing this task.

raise ValueError()
else:
prevNode = currentNode
Expand All @@ -80,6 +81,6 @@ def display(self):
display = "("
currentNode = self.firstNode
while currentNode is not None:
display += currentNode.data + ", "
display += currentNode.data.encode('utf-8') + ", "
currentNode = currentNode.nextNode
return display + ")"
29 changes: 0 additions & 29 deletions stack.py

This file was deleted.

69 changes: 24 additions & 45 deletions test_linked_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,105 +3,84 @@
import pytest


def test_node_data():
newNode = Node("Bob")
assert newNode.data == "Bob"


def test_node_add():
bob = Node("Bob")
newList = LinkedList()
newList.insert(bob)
newList.insert("Bob")
assert newList.firstNode.data == "Bob"


def test_next_node_none():
bob = Node("Bob")
newList = LinkedList()
newList.insert(bob)
newList.insert("Bob")
assert newList.firstNode.nextNode is None


def test_add_second_node():
joe = Node("Joe")
bob = Node("Bob")
newList = LinkedList()
newList.insert(bob)
newList.insert(joe)
newList.insert("Bob")
newList.insert("Joe")
assert newList.firstNode.nextNode.data == "Bob"


def test_pop():
joe = Node("Joe")
bob = Node("Bob")
newList = LinkedList()
newList.insert(bob)
newList.insert(joe)
newList.insert("Bob")
newList.insert("Joe")
assert newList.pop() == "Joe"


def test_pop_size():
joe = Node("Joe")
bob = Node("Bob")
newList = LinkedList()
newList.insert(bob)
newList.insert(joe)
newList.insert("Bob")
newList.insert("Joe")
newList.pop()
assert newList.size() == 1


def test_size():
joe = Node("Joe")
bob = Node("Bob")
newList = LinkedList()
newList.insert(bob)
newList.insert(joe)
newList.insert("Bob")
newList.insert("Joe")
assert newList.size() == 2


def test_search_fail():
joe = Node("Joe")
bob = Node("Bob")
newList = LinkedList()
newList.insert(bob)
newList.insert(joe)
newList.insert("Bob")
newList.insert("Joe")
assert newList.search("Fred") is None


def test_search_success():
joe = Node("Joe")
bob = Node("Bob")
newList = LinkedList()
newList.insert(bob)
newList.insert(joe)
assert newList.search("Bob") == bob
newList.insert("Bob")
newList.insert("Joe")
assert newList.search("Bob") is not None


def test_remove():
joe = Node("Joe")
bob = Node("Bob")
newList = LinkedList()
newList.insert(bob)
newList.insert(joe)
newList.remove(bob)
newList.insert("Bob")
newList.insert("Joe")
newList.remove(newList.search("Bob"))
assert newList.search("Bob") is None


def test_remove_empty():
newList = LinkedList()
assert newList.remove("Bob") == "THE LIST! IT'S EMPTY!!"
with pytest.raises(ValueError):
newList.remove("Bob")


def test_pop_empty():
newList = LinkedList()
assert newList.pop() == "THE LIST! IT'S EMPTY!!"
with pytest.raises(ValueError):
newList.pop()


def test_remove_fail():
joe = Node("Joe")
bob = Node("Bob")
newList = LinkedList()
newList.insert(bob)
newList.insert(joe)
newList.insert("Bob")
newList.insert("Joe")
with pytest.raises(ValueError):
newList.remove("Fred")
52 changes: 0 additions & 52 deletions test_stack.py

This file was deleted.