def radix_sort(a): """ ------------------------------------------------------- Performs a base 10 radix sort. Use: radix_sort(a) ------------------------------------------------------- Preconditions: a - a List of base 10 integers (List) Postconditions: Contents of a are sorted. ------------------------------------------------------- """ first_hash = HashSet(10) first_result = [] second_hash = HashSet(10) second_result = [] current = a._front previous = None n = 0 while current is not None and n < (a._count - 1): for j in range(0, 10): if str(current._data)[-1] == j: first_hash[j].append(current._data) previous = current current = current._next n += 1 if previous == None: for j in range(0, 10): if str(current._data)[-1] == j: first_hash[j].append(current._data) for i in range(len(first_hash)): for v in first_hash[i]: first_result.append(v) for n in range(0, 10): for m in first_result: if len(m) == 2: if m[0] == n: second_hash[n].append(m) else: second_hash[0].append(n) for i in range(len(second_hash)): for v in second_hash[i]: second_result.append(v) a = List() for q in second_result: a.append(q)
def create_randoms(): """ ------------------------------------------------------- Create a 2D list of Number objects. ------------------------------------------------------- Postconditions: returns lists: TEST lists of SIZE Number objects containing values between 0 and XRANGE (list of List) ------------------------------------------------------- """ lists = List() for i in range(TESTS): l = List() for j in range(SIZE): num = Number(random.randint(0, XRANGE)) l.insert(j, num) lists.insert(i, l) return lists
def create_sorted(): """ ------------------------------------------------------- Create a sorted list of Number objects. ------------------------------------------------------- Postconditions: returns: values - a sorted list of SIZE Number objects (List) ------------------------------------------------------- """ values = List() for i in range(SIZE + 1): values.insert(i, Number(i)) return values
def create_reversed(): """ ------------------------------------------------------- Create a reversed list of Number objects. ------------------------------------------------------- Postconditions: returns values - a reversed list of SIZE Number objects (List) ------------------------------------------------------- """ a = List() for i in range(0, SIZE): a.insert(0, Number(i)) values = a return values
def create_reversed(): """ ------------------------------------------------------- Create a reversed list of Number objects. ------------------------------------------------------- Postconditions: returns values - a reversed list of SIZE Number objects (List) ------------------------------------------------------- """ values = List() spot = 0 for i in range(SIZE - 1, -1, -1): values.insert(spot, Number(i)) spot += 1 return values
def read_foods_linked(file_variable): """ ------------------------------------------------------- Reads a file of food strings into a list of Food objects. Use: foods = read_food(file_variable) ------------------------------------------------------- Preconditions: file_variable - a file of food data (file) Postconditions: returns foods - a list of food objects (list of food) ------------------------------------------------------- """ file_variable.seek(0) foods = List() for line in file_variable: food = read_food(line) foods.append(food) return foods
def list_test(a): """ ------------------------------------------------------- Tests list implementation. Use: list_test(a) ------------------------------------------------------- Preconditions: a - list of data (list of ?) Postconditions: the methods of List are tested for both empty and non-empty lists using the data in a: is_empty, insert, remove, append, index, __contains__, find, max, min, __getitem__, __setitem__ ------------------------------------------------------- """ l = List() m = Movie("Dark City", 1998, None, None, None) # tests for the List methods go here # print the results of the method calls and verify by hand print("Empty list: {}".format(l.is_empty())) for i in a: l.append(i) print("Items append to list:") print("Len: {}".format(len(l))) l.insert(5, m) print("Insert value copy of Dark city title and year to index 5") if not l.is_empty(): print("Index of Dark City: {}".format(l.index(m))) print("Contains 'Dark City': {}".format(m in l)) print("Getitem index 2: {}".format(l[2])) else: print("list is empty.") print("Len: {}".format(len(l))) print("Max: {}".format(l.max())) print("Min: {}".format(l.min())) print("End result:") for j in l: print("{}".format(j), end="") return
def list_test(a): """ ------------------------------------------------------- Tests list implementation. Use: list_test(a) ------------------------------------------------------- Preconditions: a - list of data (list of ?) Postconditions: the methods of List are tested for both empty and non-empty lists using the data in a: empty, insert, remove, append, index, __contains__, find, max, min, __getitem__, __setitem__ ------------------------------------------------------- """ l = List() array_to_list(l, a) print("Is the list empty: {}".format(l.is_empty())) l.insert(0, Food("Junior Chickens",8, None, None)) print("Inserted in spot 0: {}".format(l[0])) print("Removed: {}".format(l.remove(Food("Junior Chickens",8, None, None)))) l.append(Food("Hot Dogs", 8, None, None)) print("appended: {}".format(l[-1])) print("index of hot dogs is: {}".format(l.index(Food("Hot Dogs", 8, None, None)))) b = Food("Pavlova", 10, True, 272) in l print("Does the list contain Pavlova: {}".format(b)) print("Max: {}".format(l.max())) print("Min: {}".format(l.min())) print("The item at spot 0 is: {}".format(l[0])) l[0] = Food("Eggplant", 8 , True, 100) print("change the item at spot 0: {}".format(l[0])) # tests for the List methods go here # print the results of the method calls and verify by hand return
def create_randoms(): """ ------------------------------------------------------- Create a 2D list of Number objects. ------------------------------------------------------- Postconditions: returns lists: TEST lists of SIZE Number objects containing values between 0 and XRANGE (list of List) ------------------------------------------------------- """ arrays = [] for j in range(0, TESTS): temp = random.sample(range(XRANGE), SIZE) a = List() for i in range(0, SIZE): a.append(Number(temp[i])) arrays.append(a) return arrays
""" ---------------------------------------------------- ---------------------------------------------------- Author: Qadeer Assan ID: 160257370 Email: [email protected] _updated_="2018-02-28" ---------------------------------------------------- """ from list_linked import List l1 = List() l2 = List() l1.append(0) l2.append(1) l1.append(2) l2.append(3) l1.append(4) l3, l4 = l1.split() for i in l3: print(i) for i in l4: print(i)
) def create_sorted(): """ ------------------------------------------------------- Create a sorted list of Number objects. ------------------------------------------------------- Postconditions: returns: values - a sorted list of SIZE Number objects (List) ------------------------------------------------------- """ a = [] a = List() for i in range(0, SIZE): a.append(Number(i)) return a def create_reversed(): """ ------------------------------------------------------- Create a reversed list of Number objects. ------------------------------------------------------- Postconditions: returns values - a reversed list of SIZE Number objects (List) ------------------------------------------------------- """