Kernel: Python 3 (system-wide)

COSC 130 - Homework 02

Kyle Anderson

Problem 1: Altering Lists

In [1]:
employees = ["Beth", "Drew", "Alex", "Chad"] years = [15, 8, 5, 2] employees.insert(1, "Gina") years.insert(1, 12) employees.insert(4, "Herb") years.insert(4, 3) employees.insert(8, "Iris") years.insert(8, 0) employees.insert(3, "Emma") years.insert(3, 6) print(employees) print(years)
Out[1]:
['Beth', 'Gina', 'Drew', 'Emma', 'Alex', 'Herb', 'Chad', 'Iris'] [15, 12, 8, 6, 5, 3, 2, 0]

Problem 2: Sorting and Slicing Lists

In [2]:
import random random.seed(12) random_list = random.sample(range(500), 99) print(f"Length of random_list: {len(random_list)}") print(f"First 10 elements of random_list: {random_list[:10]}") print(f"The sum of the elements in random list is {sum(random_list)}") sorted_list = random_list sorted_list.sort() print(f"First 10 elements of sorted_list: {sorted_list[:10]}") print(f"First 10 elements of random_list: {random_list[:10]}") bot_slice = sorted_list[:33] mid_slice = sorted_list[33:67] top_slice = sorted_list[67:] print(f"The smallest element of bot_slice is {min(bot_slice)}") print(f"The largest element of bot_slice is {max(bot_slice)}") print() print(f"The smallest element of mid_slice is {min(mid_slice)}") print(f"The largest element of mid_slice is {max(mid_slice)}") print() print(f"The smallest element of top_slice is {min(top_slice)}") print(f"The largest element of top_slice is {max(top_slice)}")
Out[2]:
Length of random_list: 99 First 10 elements of random_list: [242, 137, 336, 270, 341, 179, 73, 195, 5, 191] The sum of the elements in random list is 23739 First 10 elements of sorted_list: [0, 5, 9, 15, 26, 30, 31, 38, 42, 44] First 10 elements of random_list: [0, 5, 9, 15, 26, 30, 31, 38, 42, 44] The smallest element of bot_slice is 0 The largest element of bot_slice is 173 The smallest element of mid_slice is 179 The largest element of mid_slice is 318 The smallest element of top_slice is 319 The largest element of top_slice is 484

Problem 3: Magnitude of a Vector

In [3]:
vector = [11, 6, -8, 4, -13, 3] total = 0 for i in range(len(vector)): total += vector[i] * vector[i] mag_v = total ** 0.5 print("magnitude of vector = %.4f" % mag_v) unit_vector = [0] * len(vector) for i in range(len(vector)): val = vector[i] / mag_v unit_vector[i] = val rounded_unit_vector = [0] * len(vector) for i in range(len(vector)): rounded_unit_vector[i] = float("{:.4f}".format(unit_vector[i])) print("rounded_unit_vector: ", rounded_unit_vector) total = 0 for i in range(len(unit_vector)): total += unit_vector[i] * unit_vector[i] mag_u = total ** 0.5 print("magnitude of unit vector = %f" % mag_u)
Out[3]:
magnitude of vector = 20.3715 rounded_unit_vector: [0.54, 0.2945, -0.3927, 0.1964, -0.6381, 0.1473] magnitude of unit vector = 1.000000

Problem 4: Fibonacci Sequence

In [4]:
fib = [0, 1] for i in range(28): x = fib[-1] + fib[-2] fib.append(x) print(fib)
Out[4]:
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229]

Problem 5: Printing a Table

In [5]:
first = ["Anna","Beatrice","Charles","David","Emma"] last = ["Anderson","Brown","Clark","Daniels","Edwards"] major = ["Mathematics","Data Science","Biology","Chemistry","Computer Science"] credits = [110,83,64,35,104] gpa = [3.56,3.24,3.87,2.83,3.61] print("{0:10}{1:10}{2:16}{3:>10}{4:>10}".format("First","Last","Major","Credits","GPA")) print('-' * 56) for index in range(len(first)): print("{0:10}{1:10}{2:16}{3:>10}{4:>10.2f}".format(first[index], last[index], major[index], credits[index], gpa[index]))
Out[5]:
First Last Major Credits GPA -------------------------------------------------------- Anna Anderson Mathematics 110 3.56 Beatrice Brown Data Science 83 3.24 Charles Clark Biology 64 3.87 David Daniels Chemistry 35 2.83 Emma Edwards Computer Science 104 3.61

Problem 6: Calculating Sum of Squared Errors

In [6]:
length = [22.7, 22.4, 25.8, 21.3, 20.1, 22.1, 21.1, 25.3, 26.9, 26.9, 23.0, 23.8, 26.2, 20.4, 23.0, 21.9, 23.5, 27.8, 25.3, 25.9] weight = [9.2, 8.8, 10.7, 8.3, 6.2, 8.6, 7.2, 11.2, 10.5, 11.3, 9.6, 9.9, 10.9, 5.9, 9.5, 9.1, 9.7, 11.6, 10.2, 10.5] pred_weight = [] SSE = 0 for i in range(20): pred_weight.append(0.6 * length[i] - 5.2) diff = weight[i] - pred_weight[i] SSE += diff **2 print(pred_weight) print("Sum of Squared Errors(SSE)= %.4f" %SSE)
Out[6]:
[8.419999999999998, 8.239999999999998, 10.280000000000001, 7.579999999999999, 6.86, 8.059999999999999, 7.46, 9.98, 10.939999999999998, 10.939999999999998, 8.599999999999998, 9.079999999999998, 10.52, 7.039999999999998, 8.599999999999998, 7.939999999999999, 8.899999999999999, 11.48, 9.98, 10.34] Sum of Squared Errors(SSE)= 10.2236

Problem 7: Determining Monthly Payments

In [7]:
import math i = 0.4 n = 360 start = 100000 increment = 10000 for k in range(start,200001,increment): L = k PMT = round((L*i)/(1- math.pow(1+i,-n)),2) print('A loan of ${} would require monthly payments of ${}'.format(L,PMT))
Out[7]:
A loan of $200000 would require monthly payments of $80000.0

Problem 8: Adding Matrices

In [8]:
A = [] B = [] A.append([13,43, 28,22,41]) A.append([17,39,46,16,21]) A.append([41,34,31,25,14]) B.append([35,29,43,21,31]) B.append([48,26,19,17,23]) B.append([32,34,24,16,27]) AplusB = [] for row in range(3): temp = [] for col in range(5): temp.append(A[row][col]+B[row][col]) AplusB.append(temp) print("A") for row in range(3): print(A[row]) print("B") for row in range(3): print(B[row]) print("AplusB") for row in range(3): print(AplusB[row])
Out[8]:
A [13, 43, 28, 22, 41] [17, 39, 46, 16, 21] [41, 34, 31, 25, 14] B [35, 29, 43, 21, 31] [48, 26, 19, 17, 23] [32, 34, 24, 16, 27] AplusB [48, 72, 71, 43, 72] [65, 65, 65, 33, 44] [73, 68, 55, 41, 41]