Unit Testing “Where’s The Dollar?”
I was given the following riddle today. I thought I’d right a unit test in C# to “prove” it.
Three guys enter a hotel to stay for a night. The desk clerk tells them that one room is $30 per night. This is perfect as there are three men — they each decide to pay $10 a piece. They each pay the money and head to their room. A few minutes later, the clerk realizes his mistake as there is a special on rooms that week and they are only $25 a night. He gathers the five dollars and gives it to the bellboy to return to the men. As the bellboy is riding the elevator, he thinks “There’s three men and five dollars, they’ll never be able to split it equally. I’ll just keep two and they can have three to split equally.” He keeps two dollars for himself and gives each of the men $1 dollar in return. Here’s the fun. You have three men who paid 10 dollars a piece and received one dollar back….this means they each paid nine dollars. The bellboy kept two dollars for himself. So, nine x 3 equals 27 plus the 2 dollars the bellboy kept equals 29 dollars — where’s the missing dollar?
The quirk here is that there is one dollar unaccounted for. I wrote a unit test that proves in fact that there is a dollar missing. Therefore, this riddle has no answer.
[Test]
public void Wheres_the_dollar()
{
// Initial Transaction
var FrontDesk = 30;
var GuyOne = 10;
var GuyTwo = 10;
var GuyThree = 10;
Assert.That(GuyOne + GuyTwo + GuyThree, Is.EqualTo(30));
// Correction
var BellBoy = 5;
FrontDesk -= 5;
Assert.That(GuyOne + GuyTwo + GuyThree - BellBoy, Is.EqualTo(FrontDesk));
// Reimbursement
BellBoy -= 3;
GuyOne -= 1;
GuyTwo -= 1;
GuyThree -= 1;
Assert.That(BellBoy + GuyOne + GuyTwo + GuyThree, Is.EqualTo(29));
}
The test passes, but obviously $30 cannot be turned into $29 without each bill’s location explained. Where is the flaw? Is it in the riddle, in the code, or in both?
Note: Consider this an exercise in turning real-world situations into code. 😉