Short codes

With the advent of Twitter, shortened URLs have become all the rage: http://bit.ly/5dk33b, http://imgur.com/9G2rL, http://www.reddit.com/9dfo1. This kind of thing really isn’t all that hard to do.

$short_code = substr(preg_replace(
  array('/\//', '/\+/'),
  array('-', '_'),
  base64_encode(sha1(serialize($data, $salt), true))
), 0, 7);

Your $salt might be something as simple as a manually generated random string. If your data is time sensitive, it could be microtime(). Fancier two-way conversions are doable, in which you convert your primary key into a base-62 number, but I’ve yet to find a need for that.

Root latke waffles

  • 8 cups shredded root vegetables (e.g. yam, sweet potato, beet, carrot, onion, etc.)
  • 1 cup flour
  • ½ cup cornstarch
  • 1 tsp salt
  • ½ cup water
  • ¼ cup oil
  • spices to taste

Shred the vegetables in a food processor (if you try this by hand, you’re likely to be at it all day). Mix the dry and wet separately before combining. Fry them up in a waffle iron and serve hot with any kind of dressing that suits: ketchup, salad dressing, raw tahini. It’s all good.

Motivated hobbies

Start a project around a subject you feel passionate about. It’s surprising how quickly you can get things done when there’s internal motivation. My most recent project went from prototype to useful application in about 10 days. I have to say, there’s a bit of pride that goes along with reaching a presentable milestone.

Modern Ballots

One of the difficulties I’ve run into with my recent project is explaining what it is a back-end web service does. In response, I’ve put a small web app up at modernballots.com. This front-end lets you create elections, but has no direct knowledge of voting systems. The winner is calculated remotely through the election web service, which is publicly available for other programmers to use.

At the moment, the front-end only requests the winner as calculated by Schulze STV, but that’s just to make the interface simpler.

Election Web Service

First draft of the Election Web Service is up and running. I’ve implemented Plurality, IRV, Ranked Pairs, the Schulze Method, Plurality at Large, and STV.

The source code is up on GitHub for anyone interested in implementing additional voting systems or using them as Python libraries. For those that just want to send requests to a server, I have one responding to posts at vote.cognitivesandbox.com (note that the server only response to HTTP POSTs and works solely on JSON encoded requests).

Hopefully this takes some of the burden out of implementing polls and whatnot. You take care of storing the ballots, I’ll show you who won (and why).

Example Request

{
  "votingSystem": "stv",
  "ballots": [
    {"count": 4, "ballot": ["orange"]},
    {"count": 2, "ballot": ["pear", "orange"]},
    {"count": 8, "ballot": ["chocolate", "strawberry"]},
    {"count": 4, "ballot": ["chocolate", "sweets"]},
    {"count": 1, "ballot": ["strawberry"]},
    {"count": 1, "ballot": ["sweets"]}
  ],
  "winners": 3
}

Example Response

{
  "rounds": [
    {
      "tallies": {
        "orange": 4.0,
        "strawberry": 1.0,
        "chocolate": 12.0,
        "pear": 2.0,
        "sweets": 1.0
      },
      "quota": 6,
      "winners": ["chocolate"],
    },
    {
      "tallies": {
        "orange": 4.0,
        "strawberry": 5.0,
        "pear": 2.0,
        "sweets": 3.0
      },
      "quota": 5,
      "winners": ["strawberry"],
    },
    {
      "tallies": {
        "orange": 4.0,
        "sweets": 3.0,
        "pear": 2.0
      },
      "quota": 5,
      "loser": "pear"
    },
    {
      "tallies": {
        "orange": 6.0,
        "sweets": 3.0
      },
      "quota": 5,
      "winners": ["orange"],
    }
  ],
  "winners": ["orange", "strawberry", "chocolate"]
}

Spinning a clocklet disc

Imagine an infinitely small particle that keeps perfect time. Let’s call this particle a clocklet. We don’t have such particles, but they’re useful for considering the following.

clocklet-disc

Make a disc of clocklets about and start it spinning. Clocklet C0 is at the centre of the disc, C2 is on the edge, and C1 is half way in between. When the disc is spinning slowly, all three clocks agree on the time. C2 moves at twice the speed of C1. C0 spins, but does not otherwise move.

As we spin our disc faster and faster, the clocklets begin to differ due to an effect known as time dilation. Furthermore, C2 can never go faster than the speed of light even if C1 is moving faster than half the speed of light.

So what happens if it does? Assume that the disc spins fast enough such that C1 moves at 51% the speed of light. If the disc stops spinning, would C1 still be half way in between C0 and C2? Furthermore, does C0 differ at all from an independent clocklet outside of this disc?

I don’t have the background to answer these questions, but it’s certainly entertaining to think about.

Chained matching

Sometimes you want to find references to “foo bar” or things like that, but regex won’t cut it. Maybe “foo” is on a different line from “bar”. It can get a little hairy when you want to chain that together even further.

grep -ilR 'foo' * | xargs grep -il 'bar' | etc...

Chocolate oatmeal cups

Sometimes a recipe doesn’t turn out the way you’d expect. I have this one for oatmeal cookies that’s always turned out much flatter than I’d like. Instead of putting them on a cookie sheet, I tried using a muffin sheet such that they might hold their shape a little better. Little did I know that they’d puff up and collapse, creating chocolate oatmeal cups. Fantastic.

Ingredients

  • ½ cup margarine
  • 1 cup dry sweetener
  • 1½ tsp vanilla extract
  • 1 “egg” (I’m using Ener-G)
  • 1 tsp baking soda
  • ½ tsp salt
  • 1 cup rolled oats
  • ¼ cup cocoa
  • ½ cup flour

Method

  1. Like with all cookie recipes, mix the wet, sift together the dry, combine.
  2. Bake in a muffin tray at 350°F for 10 minutes.
  3. Let them cool down before putting them in the freezer for at least an hour.
  4. Fill with anything: jam, ice cream, peanut butter, etc.

Election API prototype

The project sputtered a few times before it really got going, but it seems well on its way. My first elections calculator was built to demonstrate the flaws of voting systems. This new one, however, is intended for the development community.

Most voting system implementations are just plain Plurality (aka first past the post), which is prone to a great many flaws. Instead of implementing better voting systems libraries in each language, why not provide a general web service API?

The tool speaks JSON and provides and is completely stateless. You send your full request (ballots, desired voting system, etc) and it returns the winner (or winners) and an explanation as to how it reached that conclusion. All data is stored locally in cookies; The only server communication is for the result calculation.

I’m only about half-way done: four more algorithms to go (Schulze, STV, CPO-STV and Schulze STV) and a fair bit of polishing on the UI. Late December? Early January? Something like that.

Update: It’s up and available.

French Onion Soup

I don’t think I’ve ever had onion soup before, so I’m kind of winging it on this one. The first attempt turned out tasty enough to share, as inauthentic as it may be.

Ingredients

  • ¼ cup margarine
  • 4 onions, sliced
  • 1 tsp sugar
  • 1 tbsp flour
  • 2½ cups water
  • 2½ cups onion broth
  • ½ cup red wine
  • croutons
  • vegan cheese, sliced (Earth Island Mozzarella works well, but I’m looking forward to trying Daiya’s)

Method

  1. Melt the margarine in a soup pot. Stir in the sugar. Add the onions and cook until caramelized (about 10 minutes).
  2. Stir in the flour until the mix thickens.
  3. Add the water, broth and wine. Bring to a boil, then simmer covered for 10 minutes.
  4. Ladle the soup into ramekins, placing a single layer or croutons on top, then multiple layers of the sliced cheese
  5. Bake at for 10 minutes at 220°C.