Skip to main content
SciNet
  • Home
  • All Courses
  • Calendar
  • Certificates
  • SciNet
    Main Site Documentation my.SciNet
  • CCDB
  • More
Close
Toggle search input
English
English Français
You are currently using guest access
Log in
SciNet
Home All Courses Calendar Certificates SciNet Collapse Expand
Main Site Documentation my.SciNet
CCDB
Expand all Collapse all
  1. EES1137 - Winter 2026
  2. Assignment 4

Assignment 4

Completion requirements
Opened: Thursday, 5 February 2026, 10:00 AM
Due: Thursday, 12 February 2026, 11:00 PM

Due date: Thursday, February 12th at midnight (Thursday night).


Let's demonstrate a magic trick based on a concept known as the Dynkin–Kruskal count. In this card trick, the magician allows a volunteer to shuffle a standard deck of playing cards. The cards are then lined up in a row, face up. The magician now leaves the room. The volunteer then picks one of the first 10 cards, say, and then moves down the line of cards by the amount of the chosen card. So, if a 4 is picked, the volunteer moves down the line by 4; if a 7 is picked the volunteer moves down the line of cards by 7. Aces are worth 1, face cards are worth 5. After choosing the first card, and moving down the line of cards, the volunteer moves down the line of cards by the value of the card that she landed on. This process is repeated until the volunteer can no longer go down the line of cards. The last card that the volunteer lands on is the card the magician is trying to guess.

The magician now returns to the room. Silently, he randomly picks a card from within the first 10 cards, and repeats the same process as the volunteer. The magician announces his final card, and the crowd is amazed to see it is the same card as the volunteer!

In this assignment we will explore this card trick. As we shall see, the probability of the magician getting the correct card is about 84.2%.


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. assignment4, 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 Magic.Utilities.R containing the following functions.

1a) Write a function which will take as an argument, at a minimum, the starting card of the trick. The function will play the game, and then return either the index or a string describing the last card that was found.

1b) Write a function that will

  • set up the magic trick,
  • randomly pick 2 cards from within the first 10 cards, one for the volunteer and one for the magician,
  • run through the trick twice, once for the volunteer, and once for the magician.

The function should return a boolean value, indicating whether or not the magician correctly 'guessed' the volunteer's final card. The sample and rep commands may be useful.


2) Create an R script, named Magic.Analysis.R, which:

  • Sources the file Magic.Utilities.R.
  • Takes an argument from the command line, the number of times to play the game, \(n\).
  • Performs the magic trick \(n\) times.  The script should use one of the *apply family of functions.
  • The script should print out the percentage of times the magician successfully 'guessed' the volunteer's final card.
  • If the command-line argument is not numeric the script should exit with an appropriate error message. The as.numeric() and is.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 Magic.Analysis.R 1000
  The number of times the magician got the correct final card was 842 out of 1000 or 84.2 percent.
$

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 Magic.Utilities.R and Magic.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 12th 2026 (midnight), with 0.5 penalty point per day off for late submission until the cut-off date of February 19th 2026, at 9:00am.

You are currently using guest access (Log in)
Data retention summary


All content on this website is made available under the Creative Commons Attribution 4.0 International licence, with the exception of all videos which are released under the Creative Commons Attribution-NoDerivatives 4.0 International licence.
Powered by Moodle