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
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
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
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
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')
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")
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
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]))
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]))
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()
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
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
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)
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()
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")
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" )
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" )
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')
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")
def testGetitem(self): r = DLList([1,2,'cat']) s = r.__getitem__(2) self.assertEqual(s,'cat') s = r.__getitem__(1) self.assertEqual(s,2)
def testLength(self): r = DLList([1,2,'cat']) self.assertEqual(len(r),3)
def testIter_visual(self): r=DLList([0,1,2,3,4,5,6,7]) for i in r: print (i)
def test_Constructor_seq(self): r = DLList([1,2,'cat']) self.assertEqual(str(r),str([1,2,'cat']))
def testRemove_start(self): r=DLList([1,2,'boy','cat',9]) r.remove(1) self.assertEqual(str(r),str([2,'boy','cat',9]))
def test_Constructor_Exception(self): try: r = DLList(55) except TypeError: pass
def testIndex_end(self): r=DLList([1,2,'boy','cat',9]) b=r.index(9) self.assertEqual(b,4)
def testIndex_start(self): r=DLList([1,2,'boy','cat',9]) b=r.index(1) self.assertEqual(b,0)
def testRemove_mid(self): r=DLList([1,2,'boy','cat',9]) r.remove('boy') self.assertEqual(str(r),str([1,2,'cat',9]))
def testIndex_mid(self): r=DLList([1,2,'boy','cat',9]) b=r.index('boy') self.assertEqual(b,2)
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]))