Creating linked-list in python without using native lists -


i'm trying implement dynamic singly linked-list in python without native lists. results of unit tests batch of code unfortunately coming failures. pycharm hasn't flagged syntactical errors , i'm @ loss parts of code wrong.

import unittest  class linked_list:      front = rear = none     current = none #used in iterator      class node:          def __init__(self, value, next):             self.value = value             self.next = next      def empty(self):         return self.empty      def push_front(self, value):         x = self.node(value, self.front)         self.front = x         if not self.rear:             self.rear = x         return x      def push_back(self, value):         if self.empty():             self.front = self.rear = self.node(value, none)         else:             x = self.node(value, none)             self.rear.next = x             self.rear = x         x = self.node(value, self.rear)         self.rear = x         return x      def pop_front(self):         if self.empty():             raise runtimeerror("empty list")         x = self.front.value         self.front = self.front.next         if not self.front:             self.rear = none         return x      def pop_back(self, value, next):         if self.empty():             raise runtimeerror("empty list")         y = self.rear.value         x = self.front         while x.next != self.rear:             x = x.next         self.rear = x         return y 

the linked list unit tests:

class test_linked_list (unittest.testcase):     def test_none(self):         self.asserttrue(linked_list().empty())     def test_pop_front_empty(self):         self.assertraises(runtimeerror, lambda: linked_list().pop_front())     def test_pop_back_empty(self):         self.assertraises(runtimeerror, lambda: linked_list().pop_back())     def test_push_back_pop_front(self):         ll = linked_list()         ll.push_back(1)         ll.push_back(2)         ll.push_back(3)         self.assertfalse(ll.empty())         self.assertequals(ll.pop_front(), 1)         self.assertequals(ll.pop_front(), 2)         self.assertequals(ll.pop_front(), 3)         self.asserttrue(ll.empty())     def test_push_front_pop_front(self):         ll = linked_list()         ll.push_front(1)         ll.push_front(2)         ll.push_front(3)         self.assertequals(ll.pop_front(), 3)         self.assertequals(ll.pop_front(), 2)         self.assertequals(ll.pop_front(), 1)         self.asserttrue(ll.empty())     def test_push_front_pop_back(self):         ll = linked_list()         ll.push_front(1)         ll.push_front(2)         ll.push_front(3)         self.assertfalse(ll.empty())         self.assertequals(ll.pop_back(), 1)         self.assertequals(ll.pop_back(), 2)         self.assertequals(ll.pop_back(), 3)         self.asserttrue(ll.empty())     def test_push_back_pop_back(self):         ll = linked_list()         ll.push_back(1)         ll.push_back("foo")         ll.push_back([3,2,1])         self.assertfalse(ll.empty())         self.assertequals(ll.pop_back(),[3,2,1])         self.assertequals(ll.pop_back(), "foo")         self.assertequals(ll.pop_back(), 1)         self.asserttrue(ll.empty()) 

edit:

below errors/failures debugger:

failure traceback (most recent call last):   file "g:\cs2_assignment1\assignment 1.py", line 101, in test_push_back_pop_back     self.assertfalse(ll.empty()) assertionerror: <bound method linked_list.empty of <assignment 1.linked_list instance @ 0x038599b8>> not false   failure traceback (most recent call last):   file "g:\cs2_assignment1\assignment 1.py", line 72, in test_push_back_pop_front     self.assertfalse(ll.empty()) assertionerror: <bound method linked_list.empty of <assignment 1.linked_list instance @ 0x03859878>> not false   failure traceback (most recent call last):   file "g:\cs2_assignment1\assignment 1.py", line 91, in test_push_front_pop_back     self.assertfalse(ll.empty()) assertionerror: <bound method linked_list.empty of <assignment 1.linked_list instance @ 0x03859d50>> not false 

a test-first methodology great way write code. unittest tells errors are. if can't fix them visual inspection, can construct temporary example scripts use step through code in debugger or sprinkle prints see what's going on.

i used unittest.main() run , got several errors. 1 was: typeerror: pop_back() missing 2 required positional arguments: 'value' , 'next' , sure enough, method had many parameters def pop_back(self, value, next):. removed them.

after going through remaining ones, script passes unit tests is:

import unittest  class linked_list:      front = rear = none     current = none #used in iterator      class node:          __slots__ = ['value', 'next']          def __init__(self, value, next):             self.value = value             self.next = next      def empty(self):         return not self.front      def push_front(self, value):         x = self.node(value, self.front)         self.front = x         if not self.rear:             self.rear = x      def push_back(self, value):         if self.empty():             self.front = self.rear = self.node(value, none)         else:             x = self.node(value, none)             self.rear.next = x             self.rear = x      def pop_front(self):         if self.empty():             raise runtimeerror("empty list")         x = self.front.value         self.front = self.front.next         if not self.front:             self.rear = none         return x      def pop_back(self):         if self.empty():             raise runtimeerror("empty list")         y = self.rear.value         if not self.front.next:             self.front = self.rear = none         else:             x = self.front             while x.next not self.rear:                 x = x.next             x.next = none             self.rear = x         return y   class test_linked_list (unittest.testcase):     def test_none(self):         self.asserttrue(linked_list().empty())     def test_pop_front_empty(self):         self.assertraises(runtimeerror, lambda: linked_list().pop_front())     def test_pop_back_empty(self):         self.assertraises(runtimeerror, lambda: linked_list().pop_back())     def test_push_back_pop_front(self):         ll = linked_list()         ll.push_back(1)         ll.push_back(2)         ll.push_back(3)         self.assertfalse(ll.empty())         self.assertequals(ll.pop_front(), 1)         self.assertequals(ll.pop_front(), 2)         self.assertequals(ll.pop_front(), 3)         self.asserttrue(ll.empty())     def test_push_front_pop_front(self):         ll = linked_list()         ll.push_front(1)         ll.push_front(2)         ll.push_front(3)         self.assertequals(ll.pop_front(), 3)         self.assertequals(ll.pop_front(), 2)         self.assertequals(ll.pop_front(), 1)         self.asserttrue(ll.empty())     def test_push_front_pop_back(self):         ll = linked_list()         ll.push_front(1)         ll.push_front(2)         ll.push_front(3)         self.assertfalse(ll.empty())         self.assertequals(ll.pop_back(), 1)         self.assertequals(ll.pop_back(), 2)         self.assertequals(ll.pop_back(), 3)         self.asserttrue(ll.empty())     def test_push_back_pop_back(self):         ll = linked_list()         ll.push_back(1)         ll.push_back("foo")         ll.push_back([3,2,1])         self.assertfalse(ll.empty())         self.assertequals(ll.pop_back(),[3,2,1])         self.assertequals(ll.pop_back(), "foo")         self.assertequals(ll.pop_back(), 1)         self.asserttrue(ll.empty())  if __name__ == '__main__':     unittest.main() 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -