Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

My code has the following errors. Need help!!! Thank you! ( Plz help find the errors in these two member functions: def make_set(self, data); def

My code has the following errors. Need help!!! Thank you!

( Plz help find the errors in these two member functions: def make_set(self, data); def union_set(self, other_set))


======================================================================
FAIL: test_Find_Data (__main__.A1_PartBTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
 File "/workspaces/a1-g2-a1-mliu146/test_a1_partb.py", line 158, in test_Find_Data
   self.assertEqual(result, node1)
AssertionError: None !=

======================================================================
FAIL: test_Union_Set (__main__.A1_PartBTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
 File "/workspaces/a1-g2-a1-mliu146/test_a1_partb.py", line 95, in test_Union_Set
   self.assertEqual(list2.get_front(), None)
AssertionError: != None

----------------------------------------------------------------------
Ran 6 tests in 0.003s

FAILED (failures=2)

 

my code:

class Node:
    def __init__(self, data=None, setlist=None, next_node=None, prev_node=None):
        self.data = data
        self.setlist = setlist
        self.next_node = next_node
        self.prev_node = prev_node

    def get_data(self):
        return self.data

    def get_next(self):
        return self.next_node

    def get_previous(self):
        return self.prev_node

    def get_set(self):
        return self.setlist
       
class SetList:
    def __init__(self):
        self.front = None
        self.back = None
       
    def get_front(self):
        return self.front
   
    def get_back(self):
        return self.back
   
    def make_set(self, data):
        if self.front is None:
            new_node = Node(data, self)
            self.front = new_node
            self.back = new_node
            return new_node
        return None
   
    def union_set(self, other_set):
        if self.front is None:
            self.front = other_set.front
            self.back = other_set.back
            node = self.front
            while node is not None:
                node.set_list = self
                node = node.next_node
            return len(other_set)
        return 0
   
    def find_data(self, data):
        current_node = self.front
        while current_node is not None:
            if current_node.data == data:
                return current_node
            current_node = current_node.next_node
        return None
   
    def representative_node(self):
        return self.front
   
    def representative(self):
        if self.front is None:
            return None
        return self.front.data
   
    def __len__(self):
        count = 0
        node = self.front
        while node is not None:
            count += 1
            node = node.next_node
        return count
 

Unit test program:


import unittest
from a1_partb import SetList

class A1_PartBTestCase(unittest.TestCase):
    """These are the test cases for functions and classes of a1_partb"""
   
    def test_Init_And_Len(self):
        the_list=SetList()

        self.assertEqual(len(the_list),0)
        self.assertEqual(the_list.get_front(), None)
        self.assertEqual(the_list.get_back(), None)

    def test_Make_Set(self):
        list1 = SetList()
        list2 = SetList()

        node = list1.make_set("cat")
        self.assertEqual(list1.get_front(), node)
        self.assertEqual(list1.get_back(), node)
        self.assertEqual(node.get_next(), None)
        self.assertEqual(node.get_previous(), None)
        self.assertEqual(node.get_data(), "cat")
        self.assertEqual(node.get_set(), list1)
        self.assertEqual(len(list1), 1)

        node2 = list2.make_set("dog")
        self.assertEqual(list2.get_front(), node2)
        self.assertEqual(list2.get_back(), node2)
        self.assertEqual(node2.get_next(), None)
        self.assertEqual(node2.get_previous(), None)
        self.assertEqual(node2.get_data(), "dog")
        self.assertEqual(node2.get_set(), list2)
        self.assertEqual(len(list2), 1)

        node3 = list2.make_set("fish")
        self.assertEqual(node3,None)
        self.assertEqual(list2.get_front(), node2)
        self.assertEqual(list2.get_back(), node2)
        self.assertEqual(node2.get_next(), None)
        self.assertEqual(node2.get_previous(), None)
        self.assertEqual(node2.get_data(), "dog")
        self.assertEqual(node2.get_set(), list2)
        self.assertEqual(len(list2), 1)

    def test_Representative_Node(self):
        list1 = SetList()
        list2 = SetList()

        self.assertEqual(list1.representative_node(), None)

        self.assertEqual(list2.representative_node(), None)



        node = list1.make_set("cat")
        self.assertEqual(list1.representative_node(), node)
        self.assertEqual(list1.representative_node(),list1.get_front())
        self.assertEqual(list1.representative_node(),list1.get_back())

        node = list2.make_set("dog")
        self.assertEqual(list2.representative_node(), node)
        self.assertEqual(list2.representative_node(),list2.get_front())
        self.assertEqual(list2.representative_node(),list2.get_back())

    def test_Representative(self):
        list1 = SetList()
        list2 = SetList()
        self.assertEqual(list1.representative(), None)
        self.assertEqual(list2.representative(), None)
        node = list1.make_set("cat")
        self.assertEqual(list1.representative(), "cat")
        node = list2.make_set("dog")
        self.assertEqual(list2.representative(), "dog")


    def test_Union_Set(self):
        list1 = SetList()
        list2 = SetList()
        list3 = SetList()

        node1 = list1.make_set("cat")
        node2 = list2.make_set("dog")
        node3 = list3.make_set("fish")

        list1.union_set(list2)

        self.assertEqual(list2.get_front(), None)
        self.assertEqual(list2.get_back(), None)
        self.assertEqual(len(list2),0)

        self.assertNotEqual(list1.get_front(), list1.get_back())
        self.assertEqual(len(list1),2)

        self.assertEqual(node1.get_set(),list1)
        self.assertEqual(node2.get_set(),list1)


        rep = list1.representative_node()
        self.assertEqual(list1.representative(), rep.get_data())

        list3.union_set(list1)

        self.assertEqual(node1.get_set(),list3)
        self.assertEqual(node2.get_set(),list3)
        self.assertEqual(node3.get_set(),list3)

        self.assertEqual(list1.get_front(), None)
        self.assertEqual(list1.get_back(), None)
        self.assertEqual(len(list1),0)

        self.assertNotEqual(list3.get_front(), list3.get_back())
        self.assertEqual(len(list3),3)

        rep = list3.representative_node()
        self.assertEqual(list3.representative(), rep.get_data())

        list1.union_set(list3)

        self.assertEqual(node1.get_set(),list1)
        self.assertEqual(node2.get_set(),list1)
        self.assertEqual(node3.get_set(),list1)

        self.assertEqual(list3.get_front(), None)
        self.assertEqual(list3.get_back(), None)
        self.assertEqual(len(list3),0)

        self.assertNotEqual(list1.get_front(), list1.get_back())
        self.assertEqual(len(list1),3)

        rep = list1.representative_node()
        self.assertEqual(list1.representative(), rep.get_data())



    def test_Find_Data(self):

        list1 = SetList()
        list2 = SetList()
        list3 = SetList()

        node1 = list1.make_set("cat")
        node2 = list2.make_set("dog")
        node3 = list3.make_set("fish")

        list1.union_set(list2)

        list3.union_set(list1)

        result = list3.find_data("cat")
        self.assertEqual(result, node1)
        self.assertEqual(result.get_set(), list3)

        result = list3.find_data("dog")
        self.assertEqual(result, node2)
        self.assertEqual(result.get_set(), list3)

        result = list3.find_data("fish")
        self.assertEqual(result, node3)
        self.assertEqual(result.get_set(), list3)

if __name__ == '__main__':
    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

Step: 3

blur-text-image

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

Income Tax Fundamentals 2013

Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill

31st Edition

1111972516, 978-1285586618, 1285586611, 978-1285613109, 978-1111972516

More Books

Students also viewed these Programming questions