-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadvanced_solutions.txt
75 lines (56 loc) · 1.56 KB
/
advanced_solutions.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
##############
Curve fit
##############
import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import curve_fit
%matplotlib inline
def log_function(x, a, b):
return a * np.log(x) + b
x_data, y_data = np.loadtxt("C:\\Users\\Peter\\Desktop\\sample_data2.txt", skiprows=1, unpack=True)
popt, pcov = curve_fit(log_function, x_data, y_data)
print(popt)
x_fit = np.arange(1, 10, 0.01)
y_fit = log_function(x_fit, popt[0], popt[1])
plt.plot(x_data, y_data, marker="x", markersize=10, linewidth=0)
plt.plot(x_fit, y_fit, linewidth=2)
plt.xlabel("Weight of African Swallow (oz)", fontsize=12)
plt.ylabel("Carrying Capacity (lb)", fontsize=12)
plt.show()
##############
Task 1:
##############
import numpy as np
from math import ceil
max_lim = 10000
# Array represents numbers 1 to maxlim. 1 is prime zero is not. Add 1 so that indices match numbers.
array = np.ones(max_lim + 1)
# Zero and one are not prime
array[0] = 0
array[1] = 0
for i in range(2, max_lim):
#if a number is marked as prime
if array[i] == 1:
#all of its multiples are not prime
j = 2
while i * j < max_lim+1:
array[j * i] = 0
j += 1
print(np.sum(array))
##############
Task 2:
##############
import numpy as np
inputfile = open("sample_data3.txt", 'r')
names = []
for line in inputfile:
names.append(line.strip())
names.sort()
total = 0
for counter, name in enumerate(names):
sum = 0
for letter in name:
sum += (ord(letter.lower())-96)
word_score = (counter+1) * sum
total += word_score
print(total)