-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathhomework1_week1.py
More file actions
128 lines (102 loc) · 3.29 KB
/
homework1_week1.py
File metadata and controls
128 lines (102 loc) · 3.29 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#------------------------------------------------
# Homework 1 of course "Python for Research"
# HarvardX - PH526xEdX, EdX
#-----------------------------------------------
#Exercise 1a--------------------------------------
import string
alphabet=string.ascii_letters
alphabet
#Exercise 1b--------------------------------------
sentence = 'Jim quickly realized that the beautiful gowns are expensive'
count_letters = {}
for i in range(len(alphabet)-1): #loops through the letters
if sentence.count(alphabet[i])>0:
count_letters[alphabet[i]] = sentence.count(alphabet[i])
#Exercise 1c--------------------------------------
def counter(input_string):
import string
alphabet=string.ascii_letters
count_letters = {}
for i in range(len(alphabet)-1): #loops through the letters
if input_string.count(alphabet[i])>0:
count_letters[alphabet[i]] = input_string.count(alphabet[i])
return(count_letters)
counter(sentence)
#Exercise 1d--------------------------------------
address_count=counter(sentence)
print(address_count)
#Exercise 1d--------------------------------------
inverse = [(value, key) for key, value in address_count.items()]
most_frequent_letter=max(inverse)[1]
print(most_frequent_letter)
#Exercise 2a-------------------------------------
import math
print(math.pi/4)
#Exercise 2b-------------------------------------
import random
random.seed(1)
def rand():
return(random.uniform(-1,1))
rand()
#Exercise 2c-------------------------------------
import math
def distance(x, y):#x and y are tuples
# define your func
dif= (x[0] - y[0], x[1] - y[1])
squared=dif[0]**2+dif[1]**2
return(math.sqrt(squared))
x=(0,0)
y=(1,1)
print(distance(x,y))
#Exercise 2d-------------------------------------
import random, math
random.seed(1)
def in_circle(x, origin):
return(distance(x,origin)<1)
print(in_circle((1,1),(0,0)))
#Exercise 2e-------------------------------------
R = 10000
x = []
inside = []
for i in range(R):
point = [rand(), rand()]
x.append(point)
inside.append(in_circle(point, (0,0)))
print(sum(inside) / R)
#Exercise 2f-------------------------------------
import math
print(sum(inside)/R-math.pi/4)
#Excercise 3a-------------------------------------
import random
random.seed(1)
def moving_window_average(x, n_neighbors=1):
n = len(x)
width = n_neighbors*2 + 1 #size of window
x = [x[0]]*n_neighbors + x + [x[-1]]*n_neighbors #enlarging x for the boundaries
# To complete the function,
# return a list of the mean of values from i to i+width for all values i from 0 to n-1.
x_moving_average=[]
window=[]
for i in range(n_neighbors,len(x)-n_neighbors):
#define window
window=x[i-n_neighbors:i+n_neighbors+1]
x_moving_average.append(float(sum(window)) / max(len(window), 1))
return(x_moving_average)
#Excercise 3b-------------------------------------
import random
random.seed(1)
R = 1000
x = []
Y = [[] for x in range(0,10)] #list of lists
for i in range(R):
x.append(random.uniform(0,1))
Y[0]=x
for n_neighbors in range(1,10):
Y[n_neighbors]=moving_window_average(x, n_neighbors)
#Excercise 3c-------------------------------------
ranges=[]
for n in range(0,10):
#minima.append(min(Y[n]))
#maxima.append(max(Y[n]))
ranges.append(max(Y[n])-min(Y[n]))
print(ranges)