Thursday, March 6, 2014

A maths/math/physics question

So I'm watching season 1 of The Killing on Netflix.  The show is great by the way, if you're into suspenseful murder mystery type stuff.  Of course they mislead you in 15 different directions only to each time stomp out all your suspicions and provide a near perfect alibi for your most recent suspect.  I still haven't finished season 1, but I'm going at my own pace, so NO SPOILERS please.  You won't find them in this blog, so I'd appreciate not finding them in the comments...

Anyhow, in Season 1 detective Holder is joking around reading a math question out of a school algebra book.  It goes something like this:

Holder: A biker goes blah blah distance at 8 miles per hour and then the same fool he bikes back at 10 miles per hour.  So what's the average round trip rate?
Jack: I don't know
Holder: And I don't give a shit.
I'm assuming the majority of you are in the same camp as Jack and/or Holder (don't know, don't care).  I however, am not, and I love math, almost as much as I love drinking!  OK, I lied, I love math more, but that's kind of scary to some people, so I'll stick with my original stance.

Anyhow, I had this drunken conversation with my brother last night, who I made watch the first 3 episodes, and we agreed, the answer, just like the killer in the show, is not the obvious suspect, which would be 9 miles per hour, or at this point, Jasper.  In order to calculate the correct answer, I quickly wrote up a few lines of python to determine the correct answer, while my brother was trying to think of extreme cases, such as what if you change the problem from 8 and 10 to say 1 and 60?

So, here's the sloppy python code I wrote up:

import sys
try:
    speed1 = int(sys.argv[1])
except:
    speed1 = 8.0
try:
    speed2 = int(sys.argv[2])
except:
    speed2 = 10.0
# speed in mph
time1 = 1.0 / speed1
time2 = 1.0 / speed2
avg = 2.0 / (time1 + time2)
print(avg)
Running the script with no parameters tells us that the answer is 8.88889 miles per hour.  It's a bit counter-intuitive, until you start to think in terms of extremes.  Running the script with 1 and 60 as the speeds, a person might expect an average somewhere near 30, however, that would be pretty far from accurate.  In fact, running the script as such:

python avg_speed_same_distance.py 1 60
1.96721311475
It's not even over 2 miles per hour.  In fact, if you ride the first leg of the trip at 1 mile per hour, you can NEVER make the round trip over 2 miles per hour.  Take the most extreme case you can think of, where you traverse the reverse trip in 0 time, basically you travel at infinity miles per hour, you will still only have averaged 2 miles per hour.  For instance, you travel 1 mile at 1 mile per hour, that's exactly 1 hour.  Now, turn around, and instantly complete the reverse trip.  Now you've traveled 2 miles, but it still took you an hour, so the round trip average is 2 miles per hour.

Just to assure myself that the distance was irrelevant, I changed up the code, so that the distance is just randomly chosen on each run of the script.

import random
import sys
distance = random.randint(1, 1000)
try:
    speed1 = int(sys.argv[1])
except:
    speed1 = 8.0
try:
    speed2 = int(sys.argv[2])
except:
    speed2 = 10.0
# speed in mph
time1 = distance / speed1
time2 = distance / speed2
avg = (distance * 2.0) / (time1 + time2)
print(avg)
As I suspected, it doesn't matter, you get the same results for the same parameters, every time, even with varying distance.

TLDR; The answer is C.  The answer is always C.

Tuesday, October 11, 2011

Beers with BitBucket

Thursday night I'll be having a beer with the BitBucket team in San Francisco.  If you weren't aware, bitbucket is a great tool for storing your source code online.  You can use Mercurial or just recently Git as well.

Anyhow, tonight I'm finally killing off that handle of Vodka and moving on to Rum, which is preferable to me anyway.

Stage 1: planning

Before I write any bad code, I plan to at least jot down some thoughts on the flow of the program, and the algorithm for how the game is played.

Assumptions:

There could be 1 or 2 players.

One or both may be a human, and the other may be a computer.

There could be a single player mode played against a clock or just for kicks.

The number of cards might be 24, or 52, or 32, or some multiple of 2.

We'll account for a max of 52 cards.

KISS:

1 player, forever alone.  24 cards.

The flow:

Player is presented with all the cards, face down.  Player can then turn over and see 2 cards.  If they match, they stay face up, otherwise they go back to face down.  The player is supposed to remember their position and what was on them, hence the title of the game.

The game is over when the player has turned all the cards face up.

The algorithm:

Create the deck.
We assumed 24 cards, so 12 different cards, in pairs.
Randomly spread these cards around the board.
Start the clock on the first click from the user.
Follow the flow outlined above.


Now that we've gotten the super duper hard stuff out of the way.  Let's mess up some code.  I'll be programming this in python, because I'm drunk.  Much like some of my Hispanic friends who revert to spanish when heavily intoxicated, I revert to python, or perhaps C, but no one wants to read C code.

Monday, October 10, 2011

Note to self: Obtain breathalyzer

While I haven't quite hit the Ballmer Peak I continue to drink.  Lest I turn out another Windows ME in a bout of blackout drunkenness, I am sticking to vodka, and avoiding whiskey.  One thing I've learned in my life is that it's ok to keep it simple.  Wikipedia says I learned it here, but I was never in the Air Force, so obviously the Keep It Simple Stupid mantra has legs beyond the armed forces.

For my first project I plan to write from scratch a program that will test the user's ability in a field that smarter scientists than I have claimed can be negatively affected by alcohol.  If you've ever woken up from a night of heavy drinking and thought to yourself, "What did I do last night?" Well, you've probably experienced some form of memory loss.  My game will test and perhaps even improve that skill: Memory.

If you can remember your childhood, perhaps you can recall a simple card game of flipping cards over to find the matching card.  This will be that game, but now instead of having to drunkenly remember where you put the cards, it'll always be available right on your computer, in some folder, somewhere.