Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
566 views
ubuntu2004
Kernel: Python 3 (system-wide)

COSC 130 - Homework 04

Kyle Anderson

Problem 1: Dot Product

def dot_product(x,y): sum=0 for i,j in zip(x,y): sum+=i*j return sum v1 = [38, 9, 40, 34, 20, 16, 42, 36, 12, 1, 23, 46, 31, 19, 30, 33, 16, 43, 24, 41] v2 = [43, 13, 35, 14, 26, 3, 36, 15, 42, 44, 45, 20, 17, 6, 47, 40, 38, 41, 31, 24] print(f'The dot product of v1 and v2 is {dot_product(v1,v2)}')
The dot product of v1 and v2 is 16220

Problem 2: Amortization

def count_payments(amount, rate, pmt): balance = amount count = 0 while balance > 0: balance = (balance * (1 + (rate/100))) - pmt balance = round(balance, 2) count += 1 return count
for i in range(5): pmt = 850 + (i * 50) print("{} monthly payments of ${} would be required.".format(count_payments(160000, 0.4, pmt), pmt))
351 monthly payments of $850 would be required. 312 monthly payments of $900 would be required. 281 monthly payments of $950 would be required. 256 monthly payments of $1000 would be required. 236 monthly payments of $1050 would be required.
print() for i in range(5): pmt = 850 + (i * 50) print("{} monthly payments of ${} would be required.".format(count_payments(160000, 0.35, pmt), pmt))
308 monthly payments of $850 would be required. 279 monthly payments of $900 would be required. 255 monthly payments of $950 would be required. 235 monthly payments of $1000 would be required. 219 monthly payments of $1050 would be required.

Problem 3: Minimum

def minimum(x): smallest = x[0] for i in x: if i < smallest: smallest = i return smallest list1 = [9.8, 7.4, 5.6, 4.8, 4.8, 5.3, 4.1, 9.6, 5.4] list2 = [3.4, 7.6, 8.7, 7.5, 9.8, 7.5, 6.7, 8.7, 8.4] list3 = ['St. Louis', 'Kansas City', 'Chicago', 'Little Rock', 'Omaha'] print(minimum(list1)) print(minimum(list2)) print(minimum(list3))
4.1 3.4 Chicago

Problem 4: Argmin

def argmin(x): index = -1 for i in range(len(x)): if index == -1 or x[i] < x[index]: # updating index to i index = i return index list1 = [9.8, 7.4, 5.6, 4.8, 4.8, 5.3, 4.1, 9.6, 5.4] list2 = [3.4, 7.6, 8.7, 7.5, 9.8, 7.5, 6.7, 8.7, 8.4] list3 = ['St. Louis', 'Kansas City', 'Chicago', 'Little Rock', 'Omaha'] print(argmin(list1)) print(argmin(list2)) print(argmin(list3))
6 0 2

Problem 5: Finding Smallest Elements

def find_smallest(x, n = None): x_copy = sorted(x) if(n == None) : return x_copy[0] else : if n > len(x_copy): n = len(x_copy) result = x_copy[0:n] return result
my_list = [39, 74, 28, 64, 17, 28, 54, 53] print(find_smallest(my_list)) print(find_smallest(my_list, 1)) print(find_smallest(my_list, 2)) print(find_smallest(my_list, 5)) print(find_smallest(my_list, 12))
17 [17] [17, 28] [17, 28, 28, 39, 53] [17, 28, 28, 39, 53, 54, 64, 74]
print(my_list)
[39, 74, 28, 64, 17, 28, 54, 53]

Problem 6: Find Unique Elements

def unique(lst): result = [] for i in range(len(lst)): if lst[i] not in result: result.append(lst[i]) result.sort() return result
int_list = [23, 16, 23, 12, 14, 23, 12, 19, 19] str_list = ['cat', 'dog', 'dog', 'cat', 'bat', 'frog', 'dog', 'frog'] print(unique(int_list)) print(unique(str_list))
[12, 14, 16, 19, 23] ['bat', 'cat', 'dog', 'frog']

Problem 7: Frequency Distribution

def freq_dist(mylist): mylist.sort() values = unique(mylist) counts = [] for element in values: freq = mylist.count(element) counts.append(freq) return (values, counts)
grades = ['A', 'D', 'A', 'C', 'B', 'F', 'A', 'D', 'C', 'B', 'F', 'A', 'C', 'B', 'A', 'B', 'B', 'C', 'B', 'F', 'D', 'D', 'A', 'C', 'B', 'B', 'D']
(values, counts) = freq_dist(grades) print("Unique Values:",values) print("Frequencies: ",counts)
Unique Values: ['A', 'B', 'C', 'D', 'F'] Frequencies: [6]

Problem 8: Weighted Means

def mean(x,w=None): if(w is None): return sum(x)/len(x) else: for i in range(len(x)): x[i] = x[i]*w[i] return sum(x)/sum(w)
x = [4,7,3,5,2,6,8,2,4,8] w = None print('Standard Mean: ',mean(x)) w = [2,1,3,1,2,3,1,4,2,1] print('Weighted Mean: ',mean(x,w))
Standard Mean: 4.9 Weighted Mean: 2.65