-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab_4.R
More file actions
73 lines (55 loc) · 1.32 KB
/
lab_4.R
File metadata and controls
73 lines (55 loc) · 1.32 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
# Lab 4
# Alexander Gadin
# Prob 1
x <- c(150, 180, 195, 210, 225, 240, 270)
fx <- c(0.15, 0.15, 0.1, 0.1, 0.25, 0.15, 0.1)
expected_value <- 0
for (i in 1:length(x)) {
expected_value <- expected_value + x[i] * fx[i]
}
print(expected_value)
varience <- 0
data_mean <- mean(x)
for (i in 1:length(x)) {
varience <- varience + fx[i]* (x[i]- data_mean)^2
}
print(varience)
# Prob 2
num <- sample(-50:50, 1)
for (i in num) { #like python syntax
if (i >= -10 && i <= 10) {
print("true")
} else {
print("Fale")
}
}
repeat { #could have not just done a while???
num <- sample(-50:50, 1)
print(num)
if (num >= -10 && num <= 10) {
print("yee")
break
}
}
#Prob 3
coin_s <- numeric(50)
for (i in 1:50) {
coin_s[i] <- sample(c(-1, 1), 1) # -1 for heads 1 for tails
} # sample is weird
print(coin_flips)
coin_flips <- rbinom(50, 1, 0.5)
coin_flips[coin_flips == 1] <- -1
coin_flips[coin_flips == 0] <- 1 # yes I looked up how to do this because I like iot this way
longest_streak <- 0
current_streak <- 0
for (i in 1:length(coin_flips)) {
if (coin_flips[i] == -1) { # head
current_streak <- current_streak + 1
if (current_streak > longest_streak) {
longest_streak <- current_streak
}
} else {
current_streak <- 0
}
}
print(paste("streak of heads:", longest_streak))