Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

in have written the pop and push methods but when i run the testing it still fails can you please help me fix it. the

in have written the pop and push methods but when i run the testing it still fails can you please help me fix it. the program has to pass the tests. from __future__ import print_function import unittest ''' when run with "-m unittest", the following produces:  FAILED (failures=9, errors=2)  your task is to fix the failing tests by implementing the necessary  methods. '''  class LinkedList(object): class Node(object): # pylint: disable=too-few-public-methods  ''' no need for get or set, we only access the values inside the  LinkedList class. and really: never have setters. '''   def __init__(self, value, next_node): self.value = value self.next_node = next_node def __init__(self, initial=None): self.front = self.back = self.current = None   def empty(self): if self.front is None: return True  else: return False   def __iter__(self): self.current = self.front return self def __next__(self): if self.current: tmp = self.current.value self.current = self.current.next_node return tmp else: raise StopIteration() def push_front(self, value): new = self.Node(value, self.front) if self.empty(): self.front = self.back = new else: self.front = new ''' you need to(at least) implement the following three methods'''   def pop_front(self): tmp = self.front if self.front is None: return None  if self.front == self.back: self.front = self.back = None  return  else: self.back = None  return tmp def push_back(self, value): new = self.Node(value, self.front) if self.empty(): return None  if self.front is None: self.front = self.next_node return  else: self.back = new return  # new node is the last one ^   def pop_back(self): if self.empty(): return None  tmp = self.back.value if not self.front.next_node: self.front = self.back = None  else: temp2 = self.front while temp2.next_node is not self.back: #traverse  temp2 = temp2.next_node temp2.next_node = None  self.back = temp2 return tmp def delete(self, value): self.current = self.back prev = None  if self.front is None: return None  while self.current: if self.current.getValue() == value: if prev: prev = self.current.next_node else: self.back = self.current return True  else: prev = self.current self.current = self.current.next_node return False   def find(self, value): self.current = self.front while self.current: if self.current.getValue() == value: return value else: self.current = self.current.next_node return None  ''' C-level work '''  class TestEmpty(unittest.TestCase): def test(self): self.assertTrue(LinkedList().empty()) class TestDeleteFind(unittest.TestCase): def test(self): linked_list = LinkedList() linked_list.find(1) linked_list.find("foo") linked_list.find([3, 2, 1]) self.assertEqual(linked_list.delete, ([3, 2, 1])) self.assertEqual(linked_list.delete, "foo") self.assertEqual(linked_list.delete, 1) self.assertTrue(linked_list.empty()) class TestPushBackPopBack(unittest.TestCase): def test(self): linked_list = LinkedList() linked_list.push_back(1) linked_list.push_back("foo") linked_list.push_back([3, 2, 1]) self.assertFalse(linked_list.empty()) self.assertEqual(linked_list.pop_back(), [3, 2, 1]) self.assertEqual(linked_list.pop_back(), "foo") self.assertEqual(linked_list.pop_back(), 1) self.assertTrue(linked_list.empty()) class TestPushFrontPopBack(unittest.TestCase): def test(self): linked_list = LinkedList() linked_list.push_front(1) linked_list.push_front(2) linked_list.push_front(3) self.assertFalse(linked_list.empty()) self.assertEqual(linked_list.pop_back(), 1) self.assertEqual(linked_list.pop_back(), 2) self.assertEqual(linked_list.pop_back(), 3) self.assertTrue(linked_list.empty()) class TestPushFrontPopFront(unittest.TestCase): def test(self): linked_list = LinkedList() linked_list.push_front(1) linked_list.push_front(2) linked_list.push_front(3) self.assertEqual(linked_list.pop_front(), 3) self.assertEqual(linked_list.pop_front(), 2) self.assertEqual(linked_list.pop_front(), 1) self.assertTrue(linked_list.empty()) class TestPushBackPopFront(unittest.TestCase): def test(self): linked_list = LinkedList() linked_list.push_back(1) linked_list.push_back(2) linked_list.push_back(3) self.assertFalse(linked_list.empty()) self.assertEqual(linked_list.pop_front(), 1) self.assertEqual(linked_list.pop_front(), 2) self.assertEqual(linked_list.pop_front(), 3) self.assertTrue(linked_list.empty()) ''' B-level work '''  class TestInitialization(unittest.TestCase): def test(self): linked_list = LinkedList(("one", 2, 3.141592)) self.assertEqual(linked_list.pop_back(), "one") self.assertEqual(linked_list.pop_back(), "2") self.assertEqual(linked_list.pop_back(), "3.141592") class TestStr(unittest.TestCase): def test(self): linked_list = LinkedList((1, 2, 3)) self.assertEqual(linked_list.__str__(), '1, 2, 3') ''' A-level work '''  class TestRepr(unittest.TestCase): def test(self): linked_list = LinkedList((1, 2, 3)) self.assertEqual(linked_list.__repr__(), 'LinkedList((1, 2, 3))') class TestErrors(unittest.TestCase): def test_pop_front_empty(self): self.assertRaises(RuntimeError, lambda: LinkedList().pop_front()) def test_pop_back_empty(self): self.assertRaises(RuntimeError, lambda: LinkedList().pop_back()) def test_push_front_empty(self): self.assertRaises(RuntimeError, lambda: LinkedList().push_front) def test_push_back_empty(self): self.assertRaises(RuntimeError, lambda: LinkedList().push_back) def test_delete_empty(self): self.assertRaises(RuntimeError, lambda: LinkedList().delete) def test_Find_empty(self): self.assertRaises(RuntimeError, lambda: LinkedList().find) ''' write some more test cases. '''  ''' extra credit.  - write test cases for and implement a delete(value) method.  - write test cases for and implement a method that finds the middle  element with only a single traversal. '''  ''' the following is a demonstration that uses our data structure as a  stack'''  def fact(number): '''"Pretend" to do recursion via a stack and iteration'''   if number < 0: raise ValueError("Less than zero") if number == 0 or number == 1: return 1 stack = LinkedList() while number > 1: stack.push_front(number) number -= 1 result = 1 while not stack.empty(): result *= stack.pop_front() return result class TestFactorial(unittest.TestCase): def test_less_than_zero(self): self.assertRaises(ValueError, lambda: fact(-1)) def test_zero(self): self.assertEqual(fact(0), 1) def test_one(self): self.assertEqual(fact(1), 1) def test_two(self): self.assertEqual(fact(2), 2) def test_10(self): self.assertEqual(fact(10), 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1) if '__main__' == __name__: unittest.main()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Microsoft Outlook 2023

Authors: James Holler

1st Edition

B0BP9P1VWJ, 979-8367217322

More Books

Students also viewed these Databases questions

Question

What is operatiing system?

Answered: 1 week ago