def test_2_2(self): mylist = UnorderedList() numbers = [31, 77, 17, 93, 26, 54] for number in numbers: mylist.add(number) # The linked list should contain 6 items overall self.assertEqual(mylist.size(), 6) # Let n equal 3 as we want the node at indice 3 which # will enable us to reach through to the last node in the list. newNode = mylist.findFromNth(3) self.assertEqual(newNode.getData(), 93) # Test that we can request the first item in the list newNode = mylist.findFromNth(0) self.assertEqual(newNode.getData(), 31) # Test that we can request the last item in the list newNode = mylist.findFromNth(mylist.size() - 1) self.assertEqual(newNode.getData(), 54) # Test that we can't request something by passing in a non-existent indice newNode = mylist.findFromNth(7) self.assertEqual(newNode.getData(), None)
def unorderedListPopTest(): print("pop") # ol = UnorderedList() # ol.append(1) # ol.append(2) # ol.add(-1) # while not ol.isEmpty(): # print(ol.pop()) # printList(ol) ol = UnorderedList() ol.append('A') ol.pop(0) printList(ol)
def unorderedListInsertTest(): ol = UnorderedList() ol.insert(0, 'G') printList(ol) ol.append('A') ol.append('B') ol.append('C') ol.append('E') ol.insert(3, 'D') ol.insert(0, 'F') printList(ol)
class StackLinkedList: def __init__(self): '''Stack()创建一个新的空栈。不需要参数,并返回一个空栈''' self.items = UnorderedList() def push(self, item): '''Push(item)将新项添加到堆栈的顶部。它需要参数 item 并且没有返回值''' self.items.add(item) def pop(self): '''pop()从栈顶删除项目。它不需要参数,返回 item。栈被修改''' return self.items.pop() def peek(self): """返回栈顶的项,不删除它。它不需要参数。堆栈不被修改。""" return self.items.index(0) def isEmpty(self): """测试看栈是否为空。它不需要参数,返回一个布尔值。""" return self.items.isEmpty() def size(self): """返回栈的项目数。它不需要参数,返回一个整数。""" return self.items.size() def __str__(self): return str(self.items)
def test_2_6(self): mylist = UnorderedList() # Purposely add in additional letters letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'] for letter in letters: mylist.add(letter) # Purposely create an endless loop by changing the next node after 'e' to be 'c' mylist.change_next('e', 'c') # Test that c is in fact at the head of the loop self.assertEqual(mylist.find_loop_head(), 'c')
def unorderedListAppendTest(): ol = UnorderedList() print("append") printList(ol) ol = UnorderedList() ol.append('B') ol.append('C') ol.append('B') printList(ol)
def main(): myList = UnorderedList() for i in xrange(NUM): value = random.randint(1, 100) myList.add(value) print "\n\nPrinting in List...\n" myList.traversal() print "\n\nPrinting in Reverse...\n" printReverse(myList.head) return
def size(self): return UnorderedList.size(self)
def remove(self,item): return UnorderedList.remove(self,item)
# 3.9.1 The Node Class from node import Node temp = Node(93) temp.get_data() from unorderedList import UnorderedList my_list = UnorderedList() my_list.add(31) my_list.add(77) my_list.add(17) my_list.add(93) my_list.add(26) my_list.add(54) my_list.search(17) my_list.remove(17) my_list.search(17)
''' This is a program that Classifies Processor Jobs. Description: This programs takes a list of CPU instructions and classifies them according to their lengths. Author: Marc Batete ''' import random from ListNode import Node from unorderedList import UnorderedList fp = open("processor_jobs.dat", "r") short_jobs = UnorderedList() medium_jobs = UnorderedList() long_jobs = UnorderedList() while True: line = fp.readline() if line == "": break line = line.strip() word = line.split() value = word[0] if value < 200: short_jobs.add(value) elif value > 200 and value < 1000: medium_jobs.add(value) else: long_jobs.add(value) short_jobs.traversal() medium_jobs.traversal() long_jobs.traversal()
import sys from ListNode import Node from unorderedList import UnorderedList SHORT = 200 MEDIUM = 1000 jobListShort = UnorderedList() jobListMedium = UnorderedList() jobListLong = UnorderedList() try: fp = open("processor_jobs.dat", "r") except IOError: sys.exit("\n\n\nUnable to Open File!!!\n\n\n") while True: line = fp.readline() if line == "": break line = line.strip() field = line.split() data = float(field[0]) if data <= SHORT: jobListShort.add(data) elif data <= MEDIUM:
import sys from ListNode import Node from unorderedList import UnorderedList jobList= UnorderedList() try: fp = open("processor_jobs.dat", "r") except IOError: sys.exit("\n\n\nUnable to Open File!!!\n\n\n") while True: line = fp.readline() if line == "": break line = line.strip() field = line.split() data =float(field[0]) jobList.add(data) jobList.traversal() print "Total execution Time is: ", jobList.Sum()
def __init__(self): '''Stack()创建一个新的空栈。不需要参数,并返回一个空栈''' self.items = UnorderedList()
''' This is a short application of the use of the node class Author: Marc Batete ''' import random from ListNode import Node from unorderedList import UnorderedList mylist = UnorderedList() #value = raw_input(" enter a number: ") while True: value = raw_input(" enter a number: ") if value == "": break else: mylist.add(value) mylist.traversal()
def isEmpty(self): return UnorderedList.isEmpty(self)
def test_2_1(self): mylist = UnorderedList() numbers = [31, 77, 17, 93, 93, 26, 26, 26, 54, 54, 54] for number in numbers: mylist.add(number) # The linked list should contain 11 items overall self.assertEqual(mylist.size(), 11) duplicate_results = mylist.find_duplicates() # The duplicates should be: # 93 occurs a second time # 26 occurs a third time # 54 occurs a third time self.assertEqual(duplicate_results[93], 2) self.assertEqual(duplicate_results[26], 3) self.assertEqual(duplicate_results[54], 3) mylist.remove_duplicates() # The size of the Unordered Linked list should be 6 self.assertEqual(mylist.size(), 6) # There should still be an occurrence of every digit: # 31, 77, 17, 93, 26, 54 self.assertTrue(mylist.search(31)) self.assertTrue(mylist.search(77)) self.assertTrue(mylist.search(17)) self.assertTrue(mylist.search(93)) self.assertTrue(mylist.search(26)) self.assertTrue(mylist.search(54)) # There should be no duplicates remaining, running the find_duplicates function # should still leave the linkedlist untouched mylist.remove_duplicates() self.assertEqual(mylist.size(), 6)
from unorderedList import UnorderedList if __name__ == "__main__": ml = UnorderedList() ml.add(1) ml.add("haha") ml.add("wut") print(ml) print(ml.size()) #print(ml.pop(3)) print(ml.end_item()) print(ml.append("this is the end")) print(ml) print(ml.append("this is the final end")) print(ml) ml.insert(12,0) print(ml) print(ml.insert(12,6)) print(ml)
def unorderedListRemoveTest(): print("remove") ol = UnorderedList() ol.add('A') ol.add('B') ol.add('C') ol.add('D') ol.add('E') ol.add('F') ol.add('G') ol.remove('A') ol.remove('B') printList(ol)
''' Playing with Unordered lists of nodes ''' import random from ListNode import Node from unorderedList import UnorderedList NUM = 20 myList = UnorderedList() for i in xrange(NUM): value = random.randint(1, 100) #temp = Node(value) #print temp, temp.getData() myList.add(value) #exit() print myList.size() myList.traversal() myList.add(99) myList.add(75) myList.add(92) print
def unorderedListIndexTest(): ol = UnorderedList() ol.insert(0, 'A') ol.append('B') ol.append('C') ol.append('D') ol.append('E') ol.append('F') ol.append('G') print(ol.index('G')) print(ol.index('A')) print(ol.index('I'))
def test1(): mylist = UnorderedList() mylist.add(31) mylist.add(77) mylist.add(17) mylist.add(93) mylist.add(26) mylist.add(54) mylist.search(93) mylist.remove(17) mylist.remove(31) mylist.remove(54) mylist.append(9) mylist.insert(1,54) mylist.insert(0,5) mylist.index(4) mylist.pop() mylist.slice(0,2)
def unorderedListTest(): ol = UnorderedList() ol.add('A') ol.add('B') print('a', ol.search('a')) print('B', ol.search('B'))