def cumulative_sum(L): C = sll.List() Sum = 0 t = L.head while t != None: Sum += t.data C.append(Sum) t = t.next return C
def cumulative_sum(L): C = sll.List() t = L.head cumsum = 0 while t != None: cumsum += t.data C.append(cumsum) t = t.next return C
def zero2n(n): L = sll.List() if n < 0: return L for i in range(0, n + 1): L.append(i) return L
def smaller_than(L, x): smaller_list = sll.List() smaller_list.head = smaller_than_n(L.head, x) if smaller_list.head == None: smaller_list.tail = None else: t = smaller_list.head while t.next != None: t = t.next smaller_list.tail = t return smaller_list
def select_modified_quicksort(L, k): arr = [] t = L.head while (t != None): arr.append(t.data) t = t.next quicksort_one_call(arr, 0, len(arr) - 1) L = sll.List() for i in arr: L.append(i) return length_of_L(L, k)
for L in [L2a, L2b, L2c]: print('Input:',L,k,'Result:',greater_than(L,k)) L = L2c for k in range(10): print('Input:',L,k,'Result:',greater_than(L,k)) print('\nQuestion 3') L1 = [5,5,5,6,5,5,8] for i in range(len(L1)+1): L = L1[:i] print('Input list:',L,'Result:',all_equal(L)) print('\nQuestion 4') L4 = [7, 4, 1, 2, 8, 9, 3] for i in range(len(L4)+1): L = sll.List() L.extend(L4[:i]) print('Original list:',end=' ') L.print() print('Resulting list:',end=' ') swap_second_and_last(L) L.print() print('\nQuestion 5') L5 = sll.List() L5.extend([7, 5, 4, 1, 2]) print('Input list:',end=' ') L5.print() for k in range(10): print('k:',k,'Result:',find(L5,k))
if __name__ == "__main__": plt.close('all') print('Question 1') L1 = zero2n(-9) L1.print() L1 = zero2n(0) L1.print() L1 = zero2n(1) L1.print() L1 = zero2n(5) L1.print() print('Question 2') L2 = sll.List() L2.extend([3, 6, 1, 4, 0, 9, 7, 4, 8, 5, 9, 7, 19]) print(appears(L2, 0)) print(appears(L2, 10)) print(appears(L2, 19)) print('Question 3') L2 = sll.List() L2.extend([3, 6, 1, 4, 0, 9, 7, 4, 8, 5, 9, 7, 19]) print(appears_loop(L2, 0)) print(appears_loop(L2, 10)) print(appears_loop(L2, 19)) L1 = sll.List() L2 = sll.List() L2.append(2)
def sum_list(t): t = t.head sum_l = 0 while t != None: sum_l += t.data t = t.next return sum_l if __name__ == "__main__": plt.close('all') L1 = sll.List() L1.print() L1.draw() L2 = sll.List() L2.extend([3, 6, 1, 0, 9, 7, 4, 8, 5]) L2.print() L2.draw() L3 = sll.List() L3.append(2) L3.print() L3.draw() print('Question 1') print(first(L1))
else: d[i, j] = 1 + min(d[i, j + 1], d[i + 1, j + 1], d[i + 1, j]) return d if __name__ == "__main__": plt.close("all") draw_figs = False # Change to True to output figures to screen print('================ Question 1 ===============') L2 = [1, 7, 4, 3, 0, 9, 2, 5, 8, 6] print(smaller(L2, 3)) print(smaller(L2, 6)) print(smaller(L2, 9)) print('================ Question 2 ===============') L3a = sll.List() L3b = sll.List() L3c = sll.List() L3d = sll.List() L3b.extend([5]) L3c.extend([7, 8]) L3d.extend([3, 0, 9, 2, 5]) for L in [L3a, L3b, L3c, L3d]: # Show original lists L.print() if draw_figs: L.draw('Original list') for L in [L3a, L3b, L3c, L3d]: remove_second(L) for L in [L3a, L3b, L3c, L3d]: # Show modified lists
def random_list(n): L = sll.List() L.extend(list(np.random.randint(0, high=10 * n, size=n, dtype=int))) return L
def substract_data(L,n): if __name__ == "__main__": plt.close('all') print('~~~~~~~~~~~~~~~ Problem 1 ~~~~~~~~~~~~~~~') print(remove_consonants('')) # ' ' print(remove_consonants('Hello, World!')) # eo o print(remove_consonants('Diego Aguirre')) # ieo Auie print(remove_consonants('Computer Science')) # oue iee print('~~~~~~~~~~~~~~~ Problem 2 ~~~~~~~~~~~~~~~') print(cumulative_sum(0)) # 0 print(cumulative_sum(3)) # 6 print(cumulative_sum(10)) # 55 print('~~~~~~~~~~~~~~~ Problem 3 ~~~~~~~~~~~~~~~') print(sum_digits_even(222)) # 6 print(sum_digits_even(101)) # 0 print(sum_digits_even(102)) # 2 print(sum_digits_even(274)) # 6 print('~~~~~~~~~~~~~~~ Problem 4 ~~~~~~~~~~~~~~~') print(palindrome('Racecar')) # True print(palindrome('12121')) # True print(palindrome('Anne')) # False print(palindrome('Was it a cat I saw?')) # True print(palindrome('Cs2302')) # False print('~~~~~~~~~~~~~~~ Problem 5 ~~~~~~~~~~~~~~~') print(multiply(2,3)) # 6 print(multiply(10,12)) # 120 print(multiply(27,98)) # 2646 print('~~~~~~~~~~~~~~~ Problem 6 ~~~~~~~~~~~~~~~') print(num_ways(0)) # 1 print(num_ways(2)) # 2 print(num_ways(5)) # 13 print(num_ways(10)) # 274 print('~~~~~~~~~~~~~~~ Problem 7 ~~~~~~~~~~~~~~~') L1 = sll.List() L1.extend([3,6,1,0,9,7,4,8,5]) print('Before: ',end='') L1.print() L1.draw() remove_first_and_last(L1) print('After: ',end='') L1.print() # [6, 1, 0, 9, 7, 4, 8] L1.draw() print('~~~~~~~~~~~~~~~ Problem 8 ~~~~~~~~~~~~~~~') L2 = sll.List() L2.extend([5,2,8,4,1]) L2.print() L2.draw() return_kth_to_last(L2.head,1) # 1 return_kth_to_last(L2.head,2) # 4 return_kth_to_last(L2.head,5) # 5 print('~~~~~~~~~~~~~~~ Problem 9 ~~~~~~~~~~~~~~~') L3 = sll.List() L3.extend([3,2,1]) print('Original: ',end='') L3.print() L3.draw() reverse(L3) print('Reversed: ',end='') L3.print()# 1 -> 2 -> 3 L3.draw() print('~~~~~~~~~~~~~~~ Problem 10 ~~~~~~~~~~~~~~~') substract_data(L1,10) L1.print() # [-4, -9, -10, -1, -3, -6, -2] substract_data(L2,5) L2.print() # [0, -3, 3, -1, -4] substract_data(L3,20) L3.print() # [-19, -18, -17]
def middle(L): t, mid, advance = L.head, L.head, False while t != None: t = t.next if advance: mid = mid.next advance = ~advance return mid.data if __name__ == "__main__": plt.close('all') L3 = sll.List() L3.extend([3, 6, 1, 4, 0, 9, 7, 4, 8, 5, 9, 7, 9]) L3.print() L3.draw() print('Question 10') S1 = smaller_than(L3, 6) S1.print() S1.draw() print('Question 11') print(sum_last_n(L3, 6)) print(sum_last_n(L3, 66)) print('Question 12') L3 = sll.List()
print(crop(L)) print('Question 2') print(equal_neighbors([])) print(equal_neighbors([1])) print(equal_neighbors([1, 5, 6, 9, 2, 9, 9])) print(equal_neighbors([1, 5, 6, 9, 2, 9])) print(equal_neighbors([1, 5, 6, 9, 2, 2, 9, 7])) print('Question 3') print(sum_until([], 5)) print(sum_until([1, 5, 6, 9, 2, 3, 7, 8, 4, 2, 9], 6)) print(sum_until([1, 5, 6, 9, 2, 3, 7, 8, 4, 2, 9], 7)) print('Question 4') L4 = sll.List() for i in range(7): L4.print() print(next_to_last(L4)) L4.append(i * 10) print('Question 5') L5 = sll.List() for i in range(7): L5.print() print(all_equal(L5)) L5.append(10) L5.append(5) L5.print() print(all_equal(L5)) L5 = sll.List()
if __name__ == "__main__": plt.close("all") # Close all figures fig, ax = plt.subplots() nested_squares(ax,2,0,0,100) set_drawing_parameters_and_show(ax) fig2, ax2 = plt.subplots() nested_squares(ax2,5,0,0,100) set_drawing_parameters_and_show(ax2) print(list_n_to_0(0)) # [0] print(list_n_to_0(5)) # [5, 4, 3, 2, 1, 0] L= sll.List() L.extend([3,6,1,2,5]) L.draw() print(sum_first_n(L,4)) # 12 print(sum_first_n(L,10)) # 17 print(sum_until(L,3)) # 0 print(sum_until(L,1)) # 9 print(sum_until(L,10)) # 17 L1= sll.List() print(next_to_last(L1)) # None print(next_to_last(L)) # 2
# -*- coding: utf-8 -*- """ Created on Tue Sep 8 11:27:25 2020 @author: manuelgutierrez """ ''' Trace the execution of the following program. Draw the list after every change is performed. Verify your answers using the code in the List class. No answer submission is necessary. ''' import singly_linked_list as sll import matplotlib.pyplot as plt import math if __name__ == "__main__": L = sll.List() for i in range(5): L.head = sll.ListNode(i, L.head) if L.tail == None: L.tail = L.head L.tail.data = 9 L.head.data = 8 L.head.next.data = 4 L.head = L.head.next L.head.next = L.head.next.next L.head.next = L.tail L.append(-2) L.extend([2, 4, 0, 1]) L.print() print(L.head.data)