2018 Programming Skills: R
Assignment 3: Writing bigger programs
The assignment for this week is to build one or more bigger programs. For each program you build, you can
earn a certain number of points indicated by the diculty level. To get a 10 for this assignment you need to
earn at least 5 points in total. You can build up to 10 points’ worth of programs to make sure that you get
these 5 points (bonus points do not count towards the 10 points upper bound). We want to be able to use
your program as a function, so make sure that you wrap your program inside a function!
Things we will check when grading your assignment:
• Can we run your code without getting errors?
• Does the function do what it is supposed to do?
• Does the function provide informative error messages if we use bogus input (e.g., if we try to do a t-test
on two character vectors)
• Are you using a consistent style. in your code? Use the google style. guide!
• Are you using clear, to-the-point comments to explain your code? Do not comment each line of code,
but use comments to explain sections of code. Also, try to keep the comments as brief as possible.
Please name the R file(s) as follows: “LastName_FirstName_Assignmentnr.”. So, if Alan Smithee were to
make the 8th assignment, the R file should be named “Smithee_Alan_8.R. If you build multiple programs,
use multiple files. Hand in the .R or .txt file(s) with the CODE through blackboard before 17:00
on Friday 26-01.
1: Regression to the Mean
• Read: https://en.wikipedia.org/wiki/Regression_toward_the_mean
• Make a small simulation program that investigates Regression towards the mean.
• What is the eect of the error term?; i.e. is the eect of regression towards the mean larger,the same or
smaller if the measurment of some construct is less reliable (more error)?
• Make a plot that shows the eect.
Diculty level: 2
2: Hangman
• Read: http://en.wikipedia.org/wiki/Hangman_%28game%29
• Program the Hangman game.
• One more point is awarded if the game includes graphics.
Diculty level: 4 (+1 for graphics)
3: The Boxes Riddle
• Watch: https://www.youtube.com/watch?v=eivGlBKlK6M
• First think of a strategy!!
1
• The solution: https://www.youtube.com/watch?v=C5-I0bAuEUE&feature=youtu.be
• Make a simultion program of the boxes riddle that can be used to calculate the probability of succes for
a dierent number of boxes. Make a plot that depicts this relationship.
Diculty level: 3
4: T-Test
• Read: https://en.wikipedia.org/wiki/Student%27s_t-test
• Program a function that can be used to perform. a t-test. Think about what results the user would like
to get and how the user should use the function.
Diculty level: 2
5: Monty Hall Problem
• Watch: https://www.youtube.com/watch?v=4Lb-6rxZxx0
• Read: http://en.wikipedia.org/wiki/Monty_Hall_problem
• Make a function that simulates the monty hall problem n times and return the proportion where
switching would win you the car and the proportion where not switching would win you the car.
Diculty level: 2
6: Mann–Whitney U Test
• Read: http://en.wikipedia.org/wiki/Mann\T1\textendashWhitney_U_test
• The Mann-Whitney U test is the non-parametric equivalent of the t-test and is used when the assumption
of normality is violated. Program your own Mann-Whitney U function and compare it to the built-in
Mann-Whitney U test in R to see whether you get the same results.
Diculty level: 2
7: Let’s sort
Sorting is easy in R. You can just use: sort(x) But let’s try to build your own sort function. One
straighforward algorithm is called bubble-sort. Check: https://en.wikipedia.org/wiki/Bubble_sort.
Part One:
• Make a function that can sort a vector using this approach.
Part Two:
• Quick-sort is much quicker! It works as follows:
• https://www.youtube.com/watch?v=ywWBy6J5gz8&spfreload=10
• The code for this algorithm in on stack overflow, using a recursive function.
• http://stackoverflow.com/questions/17604579/quicksort-in-r-how-do-i-print-the-intermediate-steps
• Compare the speed of both approaches.
Diculty level: 2
2
8: Google Code Jam
Check: https://code.google.com/codejam/contest/351101/dashboard#s=p0
Use the following code to get you started:
input <- readLines(~/Downloads/A-small-practice.in.txt)
nr_cases <- as.numeric(input[1])
input_list <- list()
new_input <- input[-1]
# read in data to a list:
for(i in 1:nr_cases)
{
sel <- new_input[((i * 3)-2) : (i * 3)]
money <- as.numeric(sel[1])
nr_items <- as.numeric(sel[2])
items <- as.numeric(strsplit(sel[3], split=)[[1]])
input_list[[i]] <- list(money, nr_items, items)
}
# export data to text file that you can upload on the website
# here I call the function f_check (this gives an error), change the name to your own function :)
sink(file=A-small-practice.out.txt, split=TRUE)
for(i in 1:length(input_list))
{
cat(paste0(Case #, i, :), f_check(input_list[[i]][[1]], input_list[[i]][[3]]), \n)
}
sink()
• Store Credit, Diculty level: 1
• Reverse Words, Diculty level: 1
• T9 Spelling, Diculty level: 1
9: Prisoners Problem
• Read: http://www.cut-the-knot.org/Probability/LightBulbs.shtml
• The R code below implements the 100 prisoners problem, but the code is not very readable. Improve
the code, so it meets the style. criteria
• Investigate the relation between the number of days and the prisoners (by making the code more
general)
# - One prisoner is the counter
# - Other prisoners turn the light on only when they enter the cell for the first time
# - The ‘counter’ turns the light off (when it is on) and counts the times he sets the light off.
# - If the counter has counted until 99, he knows every prisoner has entered the cell at least one time
counter=1; x=rep(F,100); counts=0; turns=0; light=F
while(counts<100-1)
{
3
turns=turns+1
prisoner=sample(1:100,1,replace=T)
if (prisoner!=counter & x[prisoner]==F & light==F)
{ light=TRUE; x[prisoner]=1}
if (prisoner==counter & light==T)
{light=F;counts=counts+1}
}
turns
## [1] 11662
Diculty level: 1
10: Chi-Square Test
• Read: https://en.wikipedia.org/wiki/Chi-square_test
• Calculate the chi-square value, df and p-value and compare your results with the build in function in R.
Diculty level: 2
11: Caesar Cipher
• Read: https://en.wikipedia.org/wiki/Caesar_cipher
• Program a function that can be used to encrypt a character string using the caesar cipher and program
another function that can be used to decrypt a character string using the caesar cipher.
• Use the decrypt function to decrypt the following string that is encrypted using a caesar cipher and a
key of 13 uggcf://jjj.lbhghor.pbz/jngpu?i=qDj4j9JtKpD
Diculty level: 2
12: Blackjack
• Read: https://en.wikipedia.org/wiki/Blackjack
• Program the Blackjack game.
• One more point is awarded if the game includes a betting system
Diculty level: 4 (+1 for betting system)
13: Infinite Monkeys, Infinite Typewriters
• Read: https://en.wikipedia.org/wiki/Infinite_monkey_theorem
• Simulate one monkey, typing random letters on a typewriter in sequences of 5 letters (so, typing 5
letters, then a space, and so on) - turn this into a function that returns the number of letters typed
before making a coherent 5-letter word.
• Use a file that contains all english 5 letter words (e.g., http://www-cs-faculty.stanford.edu/~knuth/
sgb-words.txt)
• Run the function 1,000 times and make a nice plot with the results (WARNING: a 1,000 times will
take apprx 1 hour, so start with a much smaller number for testing your code).