Assignment 5
Due date: Thursday, February 13th at midnight (Thursday night).
Let's play a game called pass the broccoli. In this game \(n\) people, labelled \(1,...,n\), sit in a circle. Person 1 holds a pot of overcooked broccoli. He samples the broccoli, makes a face, and then randomly passes the pot of broccoli to either his right or his left. The person who receives the broccoli also samples the broccoli, and then also randomly passes the broccoli to her right or left. The game continues until all but one person has sampled the broccoli. The person who has not sampled the broccoli is the winner of the game.
In this assignment we will explore this game, to examine which seat a person should choose to win.
0) You must use version control ("git"), as you develop your code. We suggest you start, from the Linux command line, by creating a new directory, e.g. assignment5, cd into that directory and initialize a git repository ("git init"
) within it, and perform "git add ..., git commit"
repeatedly as you add to your code. You will hand in the output of "git log"
for your assignment repository as part of the assignment. You must have a significant number of commits representing the modifications, alterations and changes to your code. If your log does not show a significant number of commits with meaningful comments you will lose marks.
1) Create a file named Broccoli.Utilities.R
containing the following functions.
1a) Write a function whose purpose is to determine the next person to receive the broccoli. It should take two arguments, \(i\), the current holder of the broccoli, and \(n\), the number of players. The function should randomly determine whether the broccoli should be passed to the right or to the left, and return the number of the next person to have the broccoli. The sample()
function may be useful here.
1b) Write a function which determines if there is a winner to the game. You will need to determine how you are going to represent the status of each player in the game, and pass that information as an argument to the function. You may also pass the number of players as an argument, if you so desire. The function should return a boolean value, indicating whether or not the game has been won.
1c) Write a function which, given the number of players in the game, plays one round of the game. The function should return how many times the broccoli was passed during the game, and who the winner was. Player 1 always starts with the broccoli. The which()
and rep()
functions may be helpful here, depending on how you approach the problem.
2) Create an R script, named Broccoli.Analysis.R
, which:
- Sources the file
Broccoli.Utilities.R
.. - Takes an argument from the command line, the number of players in the game, \(n\).
- Plays pass the broccoli 1000 times, using the given number of players. The script should use one of the
*apply
family of functions. You may hard-code the number 1000. - The script should print out the mean number of times the broccoli was passed for the given number of players.
- The script should print out a sorted table of the number of times each player won the game.
- The script should create a histogram of the number of times the broccoli was passed in each game. Use the
hist
command to do this. Note that this will create a file calledRplots.pdf
in your assignment directory when you run the script. - If the command-line argument is not numeric the script should exit with an appropriate error message. The
as.numeric()
andis.na()
functions may be helpful here. - If the value of the command-line argument does not make sense the script should exit with an appropriate error message.
- If the number of command-line arguments is not 1 the script should exit with an error message.
Your script should output similarly to this:
$ Rscript Broccoli.Analysis.R 10 The average number of times the broccoli was passed, for 10 players, was 35.816.
8 6 5 2 4 9 10 3 7 125 120 118 116 114 112 111 98 86
$
Note that, starting with this assignment and for the rest of the semester, you will be expected to use coding best practices in all of the work that you submit. This includes, but is not limited to:
- Plenty of comments in the code, describing what you have done.
- Sensible variable names.
- Explicitly returning values, if the function in question is returning a value.
- Not using the print() function to return values.
- Proper indentation of code blocks.
- No use of global variables.
- Using existing R functionality, when possible.
- Creating modular code. Using functions.
- Never copy-and-pasting code!
Submit your Broccoli.Utilities.R
and Broccoli.Analysis.R
files, and the output of git log
from your assignment repository.
Both R code files must be added and committed frequently to the repository. To capture the output of git log
use redirection (git log > git.log
, and hand in the git.log
file).
Assignments will be graded on a 10 point basis. Due date is February 13th 2025 (midnight), with 0.5 penalty point per day off for late submission until the cut-off date of February 20th 2025, at 10:00am.