예제 #1
0
 def test_find(self):
     r = DLList([1,2,'cat'])
     a = r._find(2)
     #Check it's the listNode object
     #also check it's the right value 
     self.assertTrue(isinstance(a,DListNode))
     self.assertEqual(a.value,'cat')
예제 #2
0
 def testpop_exception(self):
     r=DLList([1,2,'boy','cat',9])
     try:
         a = r.pop(20)
         self.assertTrue(False,'No Exception or/ right type of exception raised')
     except IndexError:
         pass
예제 #3
0
 def test_insert_Exception(self):
     r = DLList([1,2,'cat'])
     try:
         r.insert(4,'dog')
         self.assertTrue(False,'no Exception/or tight type of exception raised')
     except IndexError:
         pass
예제 #4
0
 def testIndex_Exception(self):
     r=DLList([1,2,'boy','cat',9])
     try:
         b=r.index('seed')
         self.assertTrue(False,'No Exception/or Right Type of Error Raised')
     except ValueError:
         pass
예제 #5
0
 def testRemove_Exception(self):
     r=DLList([1,2,'boy','cat',9])
     try:
         r.remove('seed')
         self.assertTrue(False,'No excpetion /or right type of Exception is raised')
     except ValueError:
         pass
예제 #6
0
def menu_dllist():
    dllist = DLList.DLList()
    option =""
    while option != '0':
        print("""
              1 Add to DLList
              2 Remove from DLList
              3 Test Palindrome
              4 Print DLList
              5 Reverse DLList
              0 Return to main menu
            """)
        option = input()
        if option == "1":
            print("Add an element")
            DLList.append(input())
        elif option == "2":
            print("Select index to remove")
            DLList.remove(input())
        elif option == "3":
            dllist.isPalindrome()
        elif option == "4":
            return print(dllist)
        elif option == "5":
            dllist.reverse()
예제 #7
0
 def test_find(self):
     r = DLList([1,2,'cat'])
     try:
         a = r._find(3)
         self.assertTrue(False,'no AssertionError rasied when needed for _find')
     except AssertionError:
         pass
예제 #8
0
 def testAppend(self):
     r = DLList()
     r._append(1)
     r._append(2)
     r._append('cat')
     r._append(4)
     self.assertEqual(str(r),str([1,2,'cat',4]))
예제 #9
0
 def test_List(self):
     points = 0.5
     s = DLList.DLList()
     try:
         s.remove(0)
     except IndexError:
         points += 0.5
     else:
         print("List remove is not correct")
     try:
         s.add(0, "a")
         s.add(1, "c")
         s.add(1, "b")
         self.assertAlmostEqual(s.remove(1), "b")
         s.add(1, "d")
         self.assertAlmostEqual(s.remove(1), "d")
         s.add(0, "e")
         s.add(3, "f")
         self.assertAlmostEqual(s.remove(2), "c")
         self.assertAlmostEqual(s.remove(2), "f")
         self.assertAlmostEqual(s.remove(1), "a")
         self.assertAlmostEqual(s.remove(0), "e")
         points += 1
     except:
         print("List is not correct")
     finally:
         print(f"List {points} Points")
예제 #10
0
 def test_set_Exception(self):
     r = DLList([1,2,'cat'])
     try:
         r[3]
         self.assertTrue(False,'No Exception or right type of exception raised')
     except IndexError:
         pass
예제 #11
0
 def __init__(self, key, data=None):
     self.__key = key
     if data is None:
         self.__data = key
     else:
         self.__data = data
     self.__adj = dll.DLinkedList()
예제 #12
0
 def test_del_exception(self):
     r = DLList([1,2,3,'cat'])
     try:
         del r[5]
         self.assertTrue(False,'no exception/ or right type of exception raised')
     except IndexError:
         pass
예제 #13
0
 def testSetitem(self):
     r=DLList([1,2,'cat'])
     r.__setitem__(2,0)
     self.assertEqual(str(r),str([1,2,0]))
     r.__setitem__(0,0)
     r.__setitem__(1,0)
     self.assertEqual(str(r),str([0,0,0]))
예제 #14
0
 def label(this, node=0):
     if node == 0:
         node = this.nodes()[0]
     g.home = node
     node.d = 0
     queue = DLList.DLList()
     queue.append(node)
     while (len(queue) > 0):
         n1 = queue.pop()
         for n2 in this.out_nodes(n1):
             if not hasattr(n2, 'd'):
                 n2.d = n1.d + 1
                 queue.append(n2)
예제 #15
0
def main():
    covid19 = covid19_Simulation()
    al = DLList.DLList()
    for population in range(covid19.people):
        person = Person()
        al.append(person)
    #With lockdown
    g1 = AdjacencyList.AdjacencyList(covid19.people)
    #Without lockdown
    g2 = AdjacencyList.AdjacencyList(covid19.people)

    covid19.getInteractionGraph(g1, al)
    covid19.initialInfectedNodes(g1, al)
    covid19.simulation(g1, 0, al)
    matplot.show()
예제 #16
0
    def test_iter(self):
        r = DLList()
        r._append(0)
        r._append(1)
        r._append(2)
        r._append(3)
        r._append(4)
        r._append(5)        

        i = 0
        _iter = r.__iter__()
        while True:
            try:
                
                a = _iter.__next__()
                self.assertEqual(i,a)
                i = i +1
            except StopIteration:
                return
            except:
                self.assertTrue(False,'Iterating Is not working properly')
예제 #17
0
    def test_reverse(self):
        points = 0.5
        s = DLList.DLList()
        try:
            for i in range(6, 0, -1):
                s.append(i - 1)
            s.reverse()
            points += 0.5
            for i in range(6):
                self.assertAlmostEqual(s.get(i), i)

            points += 1
        except:
            print("Reverse is not correct")
        finally:
            print(f"Reverse: {points} Points")
예제 #18
0
    def loadCatalog(self, fileName: str):
        #Graphs
        self.indexKeys = ChainedHashTable.ChainedHashTable()
        self.bookCatalog = DLList.DLList()  #or arraylist

        with open(fileName, encoding='utf-8') as f:
            start_time = time.time()
            count = 0  #line number
            for line in f:
                (key, title, group, rank, similar) = line.split("^")
                b = Book.Book(key, title, group, rank, similar)
                self.bookCatalog.append(b)

                self.indexKeys.add(b.key, count)
                #self.indexKeys.add(key, line)
                count += 1
            elapsed_time = time.time() - start_time
            print(
                f"Loaded {self.bookCatalog.size()} books into bookCatalog in {elapsed_time} seconds"
            )
        #self.similar_graph()

    #def similar_graph(self,k : int):
        self.similarGraph = AdjacencyList.AdjacencyList(
            self.bookCatalog.size())

        with open(fileName, encoding='utf-8') as f:
            start_time = time.time()
            count = 0
            for line in f:
                (key, title, group, rank, similar) = line.split("^")
                l = similar.split()
                for k in range(1, len(l)):
                    j = self.indexKeys.find(l[k])
                    #print(j)
                    if j != None:  #is not
                        self.similarGraph.add_edge(count, j)
                count += 1
            elapsed_time = time.time() - start_time
            print(
                f"Loaded {self.similarGraph.n} books into Graph in {elapsed_time} seconds"
            )
예제 #19
0
 def loadCatalog(self, fileName: str):
     '''
         loadCatalog: Read the file filenName and creates the array list with all books.
             book records are separated by  ^. The order is key, 
             title, group, rank (number of copies sold) and similar books
     '''
     self.bookCatalog = DLList.DLList()
     with open(fileName) as f:
         # The following line is the time that the computation starts
         start_time = time.time()
         for line in f:
             (key, title, group, rank, similar) = line.split("^")
             s = Book.Book(key, title, group, rank, similar)
             self.bookCatalog.append(s)
         # The following line is used to calculate the total time
         # of execution
         elapsed_time = time.time() - start_time
         print(
             f"Loading {self.bookCatalog.size()} books in {elapsed_time} seconds"
         )
예제 #20
0
 def test_ispalindrome(self):
     points = 0.5
     s = DLList.DLList()
     try:
         self.assertAlmostEqual(s.isPalindrome(), True)
         s.append("a")
         self.assertAlmostEqual(s.isPalindrome(), True)
         points += 0.5
         s.append("b")
         self.assertAlmostEqual(s.isPalindrome(), False)
         s.append("c")
         self.assertAlmostEqual(s.isPalindrome(), False)
         s.append("b")
         self.assertAlmostEqual(s.isPalindrome(), False)
         s.append("a")
         self.assertAlmostEqual(s.isPalindrome(), True)
         points += 1
     except:
         print("Is_palindrome is not correct")
     finally:
         print(f"Is Palindrome: {points} Points")
예제 #21
0
 def testRemove_mid(self):
     r=DLList([1,2,'boy','cat',9])
     r.remove('boy')
     self.assertEqual(str(r),str([1,2,'cat',9]))
예제 #22
0
 def testRemove_start(self):
     r=DLList([1,2,'boy','cat',9])
     r.remove(1)
     self.assertEqual(str(r),str([2,'boy','cat',9]))
예제 #23
0
    def test_Constructor_Exception(self):

        try:
            r = DLList(55)
        except TypeError:
            pass
예제 #24
0
 def testInsert_end(self):
     r=DLList([1,2,'cat',3])
     r.insert(4,10)
     
     self.assertEqual(str(r),str([1,2,'cat',3,10]))
예제 #25
0
    def testpop_start(self):

        r=DLList([1,2,'boy','cat',9])
        a = r.pop(0)
        self.assertEqual(a,1)
        self.assertEqual(str(r),str([2,'boy','cat',9]))
예제 #26
0
 def testpop_end(self):
     r=DLList([1,2,'boy','cat',9])
     a = r.pop()
     self.assertEqual(a,9)
     self.assertEqual(str(r),str([1,2,'boy','cat']))
예제 #27
0
 def testInsert_mid(self):
     r=DLList([1,2,'cat',3])
     r.insert(2,3)
     self.assertEqual(str(r),str([1,2,3,'cat',3]))
예제 #28
0
 def testIter_visual(self):
     r=DLList([0,1,2,3,4,5,6,7])
     for i in r:
         print (i)
예제 #29
0
 def testIndex_mid(self):
     r=DLList([1,2,'boy','cat',9])
     b=r.index('boy')
     self.assertEqual(b,2)
예제 #30
0
 def testLength(self):
     r = DLList([1,2,'cat'])
     self.assertEqual(len(r),3)
예제 #31
0
 def testIndex_start(self):
     r=DLList([1,2,'boy','cat',9])
     b=r.index(1)
     self.assertEqual(b,0)
예제 #32
0
 def testGetitem(self):
     r = DLList([1,2,'cat'])
     s = r.__getitem__(2)
     self.assertEqual(s,'cat')
     s = r.__getitem__(1)
     self.assertEqual(s,2)
예제 #33
0
 def testIndex_end(self):
     r=DLList([1,2,'boy','cat',9])
     b=r.index(9)
     self.assertEqual(b,4)
예제 #34
0
    def testpop_mid(self):

        r=DLList([1,2,'boy','cat',9])
        a = r.pop(2)
        self.assertEqual(a,'boy')
        self.assertEqual(str(r),str([1,2,'cat',9]))
예제 #35
0
 def testInsert_start(self):
     r=DLList([1,2,'cat'])
     r.insert(0,0)
     
     self.assertEqual(str(r),str([0,1,2,'cat']))
     self.assertEqual(4,len(r))