forked from CrumpLab/LabJournalWebsite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJournal.Rmd
More file actions
595 lines (458 loc) · 13.3 KB
/
Journal.Rmd
File metadata and controls
595 lines (458 loc) · 13.3 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
---
title: "Journal"
output:
html_document:
toc: true
toc_float: true
collapsed: false
number_sections: false
toc_depth: 1
#code_folding: hide
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(message=FALSE,warning=FALSE, cache=FALSE)
```
### problem 1
Do simple math with numbers, addition, subtraction, multiplication, division
```{r}
#List of computations
3+2
1+2
3-2
4*6
5/3
(1+6+4)/5
```
# problem 2
put numbers into variables, do simple math on the variables
```{r}
zoren <- 1
abc <- 2
zoren + abc
x <- c(1,2,3)
y <- c(5,6,7)
x+y
x*y
x/y
```
# problem 3
a code that will put numbers form 1 to a 100 in a variable using for loop
```{r}
z<- length(100) # start with an empty length variable and then plug in 100 into it
for (x in 1:100){
z[x] <- x
}
print(z)
```
Same code using a seq function
```{r}
z<- seq(1,100,1)
print(z)
```
# problem 4
here I am finding a sum from 1 to 100
```{r}
sum(1:100)
```
# problem 5
Writing a function to find the sum of all integers between any two intergers.
```{r}
func_addingintergers <- function(a,b) {
return(sum(seq(a,b,1)))
}
func_addingintergers(1,10)
func_addingintergers(87,90)
```
# problem 6
listing only odd numbers from 1 to 100
Same code using a seq function
```{r}
z<- seq(1,100,2)
print(z)
```
# going over a lesson on 2/4/2019
```{r}
a<- 1
b<-"Hello World"
d<-"3"
print(a)
class(d)
vector<- c(1,2,3,4,5,6,7,8)
vector[2:4]
vector[c(2,2,2,2,2)]
vector[vector <6] # show the numbers less than 7
vector[vector>=6] # greater or equal to 6
vector[vector!=4] # show numbers that are not 5
vector[8]<-3000 # put 3000 in the place of 8th number
vector
vector[1:2] <- 10
class(vector)
vector[7]<- "j" # put a number in the 7th place in the vector
vector
a<-c("1","2","3")
class(a)
b<- a
class(b)
as.numeric(b) # store charecters 123 as a number
a<- "ljjasjdflsjasf;lasdjflk;asdjfl;asdjfl"
strsplit(a,split="") # if you had a paragraph and you wanted to split the paragraph or put a period in R
a[1]
b[(1)][5]
example_list <- list(a=11, b=4, d=c(1,2,3,4))
example_list
data.frame() # particular way of structuring data
f<- matrix(0,nrow = 5, ncol = 3) # creating 5 rows and 3 columns
first_names <- c("asdf", "Asdfasd","asdfasdf")
ages<-c(3343,23122,67456)
grades<-c(89,56,99)
everybody <- data.frame(first_names,ages,grades)
everybody$ages[2] <- 12
```
# promblem 7
Generating prime numbers from a list of numbers from 1 to 1000
```{r}
x <- seq(1:1000)
y <- (0)
for (i in 2:999) {
y<- c(x[y<=i],x[x>i & x%% i >0])
x<-y
}
y
```
# Problem 8
Generating random numbers from 1 to 100
```{r}
sample(1:100, 100, replace = T)
```
# problem 9
```{r}
sample(1:100, 25, replace = T)
```
# problem 10a
```{r}
z<-c(10,11,12,13,14,15,16,17,18,19)
mean_zoren <- function(x) {
mean_func <-0 # I need to initialize something if I want to use it in something else
for (i in 1:length(x)) {
mean_func<-mean_func + x[i]
}
mean_func<- mean_func/length(x)
return(mean_func)
}
mean_zoren(z)
```
# problem 10b
writing my own function for the mean
```{r}
mean_function <- function(z)
return(sum(z)/length(z))
test_numbers<-c(4,30,12,10)
mean_function(test_numbers)
```
# problem 10c
finding median
```{r}
z<-c(5,6,7,8,9,10)
mode_function <- function(z) {
while(length(z) > 2) {
loc <- match(c(max(z),min(z)),z)
z<-z[-loc]
}
if (length(z) == 2) {
z <- (z[1]+z[2])/2
}
return(z)
}
mode_function (z)
```
# problem 10d
descriptive statistics - the range
```{r}
z<-c(20,21,22,23,24,25,26,27,28,29,30)
my_range<- function(z) {
range_function <- max(z)-min(z)
return(range_function) }
my_range(z)
```
# problem 10e
descriptive statistics - the standard deviation
```{r}
z<-c(20,21,22,23,24,25,26,27,28,29,30)
my_StandardDeviation<- function(z) {
mean_function <- mean(z)
var <- 0
for (i in 1:length(z)){
var<- var + (mean_function - x[i])^2
var<- var/length(z)
StDev <- sqrt(var)
}
return(StDev)
my_StandardDeviation(z)
}
```
# problem 11
counting how many variables are in the string
```{r}
my_string <- "this one is not that bad"
nchar(my_string)
```
# problem 12
counting how many words are in the string
```{r}
my_string <- "this one is not that bad"
count_words <- length(strsplit(my_string,' ')[[1]])
print(count_words)
```
# problem 13
counting how many sentences are in the string
```{r}
my_string <- "This class is very hands on. You learn by practice. Looking forward to disecting more problems."
count_sentences <- length(strsplit(my_string,"\\.")[[1]])
print(count_sentences)
```
# problem 14
counting how many times a specific character occurs in a string
```{r}
my_string <- "one small step for man, one giant leap for mankind"
find <-"a"
count_character <- length(which(strsplit(my_string,"")[[1]]==find))
print(count_character)
```
# problem 15
logical test to see whether a word can be found within the text of another string
```{r}
mytest_word <- "the"
mytest_sentence <- "It is a nice day in the neighborhood"
new_splitsentence <- strsplit(mytest_sentence," ")
mytest_word %in% new_splitsentence
```
# problem 16
Putting current computer time in milliseconds into a variable
```{r}
print(as.numeric(Sys.time())*1000, digits=15)
```
# problem 17
Measure how long a piece of code takes to run by measuring the time before the code is run, and after the code is run, and taking the difference to find the total time
```{r}
time_start <- as.numeric(Sys.time())
ed <- length(100)
for (i in 1:100) {
ed[i] <- i
}
time_end <- as.numeric(Sys.time())
time_run <- time_end - time_start
print(time_run)
```
# problem 18
Readin a .txt file or .csv file into a variable
```{r}
```
# Harder Problems
# Problem 1.
List the numbers from 1 to 100 with the following constraints. If the number can be divided by three evenly, then print Fizz instead of the number. If the number can be divided by five evenly, then print Buzz instead of the number. Finally, if the number can be divided by three and five evenly, then print FizzBuzz instead of the number
```{r, eval=FALSE}
numb <-(1:100)
for (j in 1:length(numb)) {
if(numb[j]%%3 == 0){
numb[j] <-"Fizz"
}
}
```
```{r}
result <-1
for (j in 2:100){
if(j %% 3 == 0){
if (j %% 5 == 0) {
result <-paste(result, "FizzBuzz", sep = ",") }
else {result <- paste(result,"Fizz", sep = ", ")
}
}
else if (j %% 5 == 0) {
result <- paste(result, "Buzz", sep = ", ")
}
else {
result <- paste(result, j, sep = ", ")
}
}
print(result)
```
# Problem 2
Take text as input, and be able to produce a table that shows the counts for each character in the text
```{r}
z <-"lets see what happens"
table(unlist(strsplit(z,split="")))
```
```{r}
digits <- c(9,8,7,6,5,4,3,2,1)
letters <- c("a","s","d","j","k","l","m","n","o")
table <- data.frame(digits,letters)
print(table)
```
# Problem 3
Test the random number generator for a flat distribution. Generate a few million random numbers between 0 and 100. Count the number of 0s, 1s, 2s, 3s, etc. all the way up to 100. Look at the counts for each of the numbers and determine if they are relatively equal.
```{r}
z<-runif(20000000,0,100)
hist(z)
```
# Problem 4
Create a multiplication table Generate a matrix for a multiplication table. For example, the labels for the columns could be the numbers 1 to 10, and the labels for the rows could be the numbers 1 to 10. The contents of each of the cells in the matrix should be correct answer for multiplying the column value by the row value.
```{r}
z< c(1,2,3,4,5,6,7,8,9)
for(i in 1:2){
for(j in 1:2){
print(i*j)
}
}
```
# Problem 5
Encrypt and Decrypt the Alphabet Turn any normal english text into an encrypted version of the text, and be able to turn any decrypted text back into normal english text.
```{r}
start_sequence <- c(99,399,130,140,550,20,20,30,20,40,50,2)
numbers <- unique(start_sequence)
scrambled_numbers <- sample(numbers)
encryption_key <- data.frame(numbers,scrambled_numbers)
encrypt_numbers <-function(input_sequence,key){
encrypted_sequence<-c()
for(i in 1:length(input_sequence)){
original_number <- input_sequence[i]
new_number <- key[key$numbers==original_number,]$scrambled_numbers
encrypted_sequence[i] <- new_number
}
return(encrypted_sequence)
}
encrypt_numbers(start_sequence,encryption_key)
```
# Problem 6
Snakes and ladders
```{r}
tot_sum<-0 # this problem tells how many times you roll a dice to get sum total of the numbers to equal 100 or more
number_of_rolls<-0
while(tot_sum < 100){
number_of_rolls <- number_of_rolls+1
tot_sum <-tot_sum+sample(c(1,2,3,4,5,6),1)
}
number_of_rolls
```
multiple simulations recorded and saved
```{r}
save_rolls <- c()
for(sims in 1:100){
total_sum<-0
number_of_rolls<-0
while(total_sum < 25){
number_of_rolls <- number_of_rolls+1
total_sum <-total_sum+sample(c(1,2,3,4,5,6),1)
}
save_rolls[sims] <- number_of_rolls
}
mean(save_rolls)
```
snakes and ladders
```{r}
snakesladders_board <- c(1,2,11,4,5,17,7,8,18,12,11,12,13,4,15,16,17,18,
8,20,21,20,23,16,25,26,27,28,29,30,31,32)
run_game <- function(a_board){
count <- 0
position <- 1
while(position < 25){
count <- count+1
position <- a_board[position+sample(seq(1,6),1)]
}
return(count)
}
mean(replicate(10000,run_game(snakesladders_board)))
```
# problem 7
Dice-rolling simulations. Assume that a pair of dice are rolled. Using monte carlo-simulation, compute the probabilities of rolling a 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, and 12, respectively
```{r}
calc_probability <- data.frame(c(2:12),replicate(11,0))
for (i in 1:1000) {
roll <- sum(sample(1:6, 2, replace = TRUE))
for (j in 2:12) {
if (roll == j) {
calc_probability[j-1,2] = calc_probability[j-1,2] + 1
}
}
}
calc_probability[,2] = calc_probability[,2]/1000
print(calc_probability)
```
# Problem 8
Monte Hall problem. The monte-hall problem is as follows. A contestant in a game show is presented with three closed doors. They are told that a prize is behind one door, and two goats are behind the other two doors. They are asked to choose which door contains the prize. After making their choice the game show host opens one of the remaining two doors (not chosen by the contestant), and reveals a goat. There are now two door remaining. The contestant is asked if they would like to switch their choice to the other door, or keep their initial choice. The correct answer is that the participant should switch their initial choice, and choose the other door. This will increase their odds of winning. Demonstrate by monte-carlo simulation that the odds of winning is higher if the participant switches than if the participants keeps their original choice
```{r}
doors<- c("A","B","C")
zdata= c()
for (i in 1:100)
{
prize <- sample (doors) [1]
pick <- sample (doors) [1]
open <- sample (doors [which(doors !=pick & doors !=prize)]) [1]
switchyes <- doors[which(doors != pick & doors != open)]
if(pick == prize) {zdata=c(zdata, "noswitchwin")}
if(switchyes == prize) {zdata=c(zdata, "switchwin")}
length(which(zdata == "switchwin"))
length(which(zdata == "noswitchwin"))
}
```
# Problem 9
100 doors problem. Problem: You have 100 doors in a row that are all initially closed. You make 100 passes by the doors. The first time through, you visit every door and toggle the door (if the door is closed, you open it; if it is open, you close it). The second time you only visit every 2nd door (door 2, 4, 6, etc.). The third time, every 3rd door (door 3, 6, 9, etc.), etc, until you only visit the 100th door.
```{r, eval=F}
doors_puzzle <- function(ndoors=100,passes=100) {
doors <- rep(FALSE,ndoors)
for (ii in seq(1,passes)) {
mask <- seq(0,ndoors,ii)
doors[mask] <- !doors[mask] }
return (which(doors == TRUE)) } doors_puzzle()
```
```{r}
doors<-rep(0,100)
for(i in 1:100) {
for(j in seq(i,100,by=i)){
if(doors[j]==0){
doors[j]<-1
} else {
doors[j]<-0
}
}
}
doors
```
# Problem 10
99 Bottles of Beer Problem In this puzzle, write code to print out the entire “99 bottles of beer on the wall”" song. For those who do not know the song, the lyrics follow this form:
X bottles of beer on the wall X bottles of beer Take one down, pass it around X-1 bottles of beer on the wall
Where X and X-1 are replaced by numbers of course, from 99 all the way down to 0
```{r}
z <- 99
first_sentence <- "bottles of beer on the wall,"
second_sentence <- "bottles of beer. Take one down, pass it around,"
third_sentence <- "bottles of beer on the wall!"
while (z > 0) {
each_sentence <- paste(z,first_sentence,z,second_sentence,z-1,third_sentence)
print(each_sentence)
z = z -1
}
```
# Problem 11
Random Tic-Tac-Toe Imagine that two players make completely random choices when playing tic-tac-toe. Each game will either end in a draw or one of the two players will win. Create a monte-carlo simulation of this “random” version of tic-tac-toe. Out 10,000 simulations, what proportion of the time is the game won versus drawn?
```{r, eval=F}
game <- function(){
# Init Board
board <- c(0,0,0,
0,0,0,
0,0,0)
winner = NULL
while(is.null(winner)){
#player 1:
next_move <- random_move(board)
board <- make_move(board, 1, next_move)
winner <- evaluate_win(board)
if(is.null(winner)){
#player 2:
next_move <- random_move(board)
board <- make_move(board, 2, next_move)
winner <- evaluate_win(board)
}
}
return(winner)
}
```