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
509 lines (412 loc) · 11.3 KB
/
Journal.Rmd
File metadata and controls
509 lines (412 loc) · 11.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
---
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=TRUE)
```
# Problem 1
Do simple math with numbers, addition, subtraction, multiplication, division
```{r}
1+1
3-2
4*6
5/7
```
# Problem 2
Put numbers into variables, do simple math on the variables
```{r}
m <-1
```
# Problem 3
A). Write code that will place the numbers 1 to 100 separately into a variable using for loop.
```{r}
a<-0
for(i in 1:100){
a<-a + print(i)
}
```
B). Then, again using the seq function.
```{r}
seq(1,100)
```
# Problem 4
Find the sum of all the integer numbers from 1 to 100:
A). Use the sum() function on a vector of numbers
```{r}
b<-0
for(i in 1:100){
b<- b+sum(i)
}
```
B). How would you do this without using the sum function? For example, how could you use a for loop to accomplish this task?
```{r}
e<-0
for (i in 1:100){
e<-e + i
}
```
# Problem 5
Write a function to find the sum of all integers between any two values.
```{r}
SumFunction <- function (x) {
f<-0
for (i in x){
f<-f+i
}
return(f)
}
x<-1:100
SumFunction(x)
```
# Problem 6
List all of the odd numbers from 1 to 100:
A). Use the seq() function
```{r}
seq(1,100, by=2)
```
B). How could you do this without using the seq() function? Consider using the mod function %%, which evaluates whether or not there is a remainder when dividing one number by another.
```{r}
for(i in 1:100){
if (!i %%2)
next
print(i)
}
```
# Problem 7
List all of the prime numbers from 1 to 1000.
```{r}
PrimeFunction<-function (n){
for(i in 2:(n-1)) {
if ((n %% i) == 0) {
print('NOT Prime Number')
} else print('YES Prime Number')
return (n)
}
}
```
# Problem 8
Generate 100 random numbers.
```{r}
h<-runif(100)
h
```
# Problem 9
Generate 100 random numbers within a specific range (runif can do this)
```{r}
j<-runif(100,1,200)
j
```
# Problem 10
Write your own functions to give descriptive statistics for a vector variable storing multiple numbers. Write functions for the following without using R intrinsics:
A). mean
```{r}
MeanFunction <- function(x){
k<-0
l<- 0
for(i in x){
k <- k+sum(i)
l<-l+length(x)
}
return(k/l)
}
MeanFunction(x)
```
B). mode
```{r}
ModeFunction <- function(x){
m<-0
for(i in x){
m <- m+freq(x) }
return(m)
}
m
```
C). median
```{r}
MedianFunction <- function(x){
n<-0
p<-0
for(i in x){
n<-n+seq(i)
p<-p+((length(x)+1)/2)
}
return(p)
}
```
D). range
```{r}
RangeFunction <- function(x){
q<-0
r<-0
for(i in x){
q <-q+min(x)
r<-r+max(x)
}
return(r-q)
}
RangeFunction(x)
```
E). standard deviation
```{r}
StanDevFunction <- function(x){
s<-0
for(i in x){
s <-s+sqrt(var(x))
}
return(s)
}
StanDevFunction(x)
```
# Problem 11
Count the number of characters in a string variable (use strsplit() to split a character vector)
```{r}
z <- "dskjfenlrhelenhtrileriwtnhreut"
t<- unlist(strsplit(z,split=""))
length(t)
```
# Problem 12
Count the number of words in a string variable (use strsplit)
```{r}
y<-"Count the number of words in a string variable use strsplit"
u<- unlist(strsplit(y,split=" "))
length(u)
```
# Problem 13
Count the number of sentences in a string variable (consider splitting by the . character)
```{r}
x<-"Count the number of sentences in a string variable. Consider splitting by the period character."
v<- unlist(strsplit(x,split=" "))
length(v)
```
# Problem 14
Count the number of times a specific character occurs in a string variable:
A). Table() function can help count individual occurences
```{r}
w<-c(2,2,3,5,6,7,8,8,8,9,0,8,6,7,5,7,6,3,6,3,4)
ab<-table(w)
as.data.frame(ab)
```
B). How would you do this without the table function?
```{r}
w<-c(2,2,3,5,6,7,8,8,8,9,0,8,6,7,5,7,6,3,6,3,4)
ab<-as.data.frame(w)
ab
```
# Problem 15
Do a logical test to see if one word is found within the text of another string variable.
```{r}
bc<-"text"
cd<-"Do a logical test to see if one word is found within the text of another string variable"
bc%in%cd
```
# Problem 16
Put the 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
Before
```{r}
Bb<-print(as.numeric(Sys.time())*1000, digits=15)
```
After
```{r}
Aa<-print(as.numeric(Sys.time())*1000, digits=15)
```
Difference
```{r}
AaBb<-Aa-Bb
AaBb
```
# Problem 18
Read a .txt file or .csv file into a variable. Scan() is a general purpose text input function. read.csv() will read .csv files.
```{r}
read.csv("RandomRead.csv",header = TRUE,sep = ",")
```
# Problem 19
Output the contents of a variable to a .txt file
```{r}
ds <- c(2,8,6,4,0,1,4,7)
hi<-write.csv(ds, file = "RandomRead.csv")
hi
```
# Problem 20
Create a variable that stores a 20x20 matrix of random numbers
```{r}
ef<-runif(400)
fg<-matrix(ef,nrow = 20,ncol = 20)
fg
```
# Problem 21
Output any matrix to a txt file using commas or tabs to separate column values, and new lines to separate row values
```{r,eval=FALSE}
ij<-write.csv(ef,file = "RandomRead.csv", sep="\t", row.names = TRUE, col.names = NA)
ij
```
# HARDER PROBLEMS
# Problem 1
The FizzBuzz Problem
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}
a<-c(1:100)
z<-seq(3,100, by=3)
a[z]<-'FIZZ'
y<-seq(5,100, by=5)
a[y]<-'BUZZ'
x<-seq(15,100, by=15)
a[x]<-'FIZZBUZZ'
a
```
# Problem 2
A). Frequency Counts
Take text as input, and be able to produce a table that shows the counts for each character in the text. This problem is related to the earlier easy problem asking you to count the number of times that a single letter appears in a text. The slightly harder problem is the more general version: count the frequencies of all unique characters in a text.
```{r}
y<-"Frequency Counts"
table(unlist(strsplit(y,split="")))
```
B).Can you do this without using table? Attempt this problem using data.frame
```{r}
y<-c('F','r','e','q','u','e','n','c','y','C','o','u','n','t','s')
x<-c(1:15)
FCD<-data.frame(x,y)
FCD
```
# Problem 3
Test the Random Number Generator
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. For example, you could plot the counts in Excel to make a histogram. If all of the bars are close to being flat, then each number had an equal chance of being selected, and the random number generator is working without bias.
```{r}
b<-runif(1000000,min=0, max=100)
hist(b)
```
# 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}
for(i in 1:10){
for(t in 1:10){
w<-print(i*t)
}
}
matrix(w,nrow = 10,ncol = 10)
```
# 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. A simple encryption would be to scramble the alphabet such that each letter corresponds to a new randomly chosen (but unique) letter.
```{r}
b<- c('E','n','c','r','y','p','t', 'D','e','c','r','y','p','t','A','l','p','h','a','b','e','t')
b <- as.factor(b)
levels(b)
k<-b
levels(k)
k
```
# Problem 6
Snakes and Ladders
A). Your task here is to write an algorithm that can simulate playing the above depicted Snakes and Ladders board. You should assume that each roll of the dice produces a random number between 1 and 6. After you are able to simulate one played game, you will then write a loop to simulate 1000 games, and estimate the average number of dice rolls needed to successfully complete the game.
```{r}
### Simulation of one played game
m<-0
n<-0
while(m<25){
n<-n+1
m<-m+sample(c(1:6),1)
}
n
# A loop simulation of 1000 games and estimation of average number of dice rolls needed to successfully complete game
m<-0
n<-0
z<-0
for(i in 1:1000)
while(m<25){
n<-n+1
m<-m+sample(c(1:6),1)
}
z[i]<-n
mean(n)
```
# 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}
r<-0
q<-0
s<-0
for(i in 1:1000)
while(r<25){
q<-q+1
r<-r+sample(c(2:12),1)
s[i]<-q
}
q
```
# 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}
h<-0
g<-0
y<-0
for(i in 1:1000)
while(h<25){
g<-g+1
h<-h+sample(c(1:10),1)
y[i]<-g
}
g
```
# 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}
z<-0
p<-0
d<-0
for(i in 1:100)
while(z<100){
p<-p+1
z<-z+sample(c(1:100),1)
d[i]<-p
}
p
```
# 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}
v<-0
x<-0
s<-0
for(i in 0:99)
while(v<100){
x<-x+1
v<-v+x-1
s<-print(x+v)
}
s
```
# 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}
uk<-0
ty<-0
k<-0
for(i in 1:10000)
while(uk<25){
ty<-ty+1
uk<-uk+sample(c(1:10000),1)
k[i]<-ty
}
ty
```