Jan 132015


So my wife and I were riding the train this morning, and having finished the crossword puzzle, we gave the KenKen a shot. It definitely provided some fun, but more so it got me thinking about combinations and permutations…

Eventually I started wondering how many total permutations there are of the 6 by 6 grid of numbers, and how many of those constitute valid KenKen boards (each row and each column must have all of the numbers between 1-6).

I ended up writing this script to brute force it:


But it seems like there are more combinations than I’d guessed! …

$ ./kenken.py 1
Number of unique combinations for cardinality 1: 1
Good: 1
Bad: 0

$ ./kenken.py 2
Number of unique combinations for cardinality 2: 6
Good: 2
Bad: 4
$ ./kenken.py 3
Number of unique combinations for cardinality 3: 1680
Good: 12
Bad: 1668

I’m running it with cardinality of 4 now (4×4 grid of numbers between 1-4) and it’s checking board 3 billion something…

Next steps would be trying to think of a clever way to find out the number of valid combinations for a 6×6 grid. Either we can continue with a programatic approach, either by using multiprocessing or just throwing random combinations at it, or both…

OR we could probably divert to math. A quick Google search gave me this link that seems to explain it pretty well:


I guess the answer is 6! * 5! * 4! * 3! * 2! * 1!, or 24883200 (right? someone wanna double check me here?).

Long story short, it looks like there’s no shortage of viable KenKen boards!

Thanks Cornell Department of Mathematics…

Nov 202014

So you use Spotify and want a list of all of your playlists so you can come up with your #AOTY (album of the year). I’m sure there are other ways of doing this, but my rudimentary google searches produced no results, so here goes…


  • Spotify account
  • python
  • internets

First, sign up as a Spotify developer and create an application, here:


Once you’ve created an application, make note of the Client ID and Client Secret, and add a new Redirect URIs to some working website.

Make sure to hit SAVE! If you don’t, this won’t work!!!

Now onto the code part…

First we need to put our Client ID Client Secret and Redirect URI into environment variables to be read later:

$ export SPOTIPY_CLIENT_ID='your-client-id-here'
$ export SPOTIPY_CLIENT_SECRET='your-client-secret-here'
$ export SPOTIPY_REDIRECT_URI='http://samsandberg.com'

Now we need an access token – here’s Spotify’s full formal auth guide:


but for this exercise we’ll be using Spotipy (docs)

Install spotipy:

$ pip install spotipy

Next we need to determine our scope. Here’s a list of the available scopes:


For this exercise, we’ll be using playlist-read-private.

Now let’s use this to get our scope. My username is loisaidasam, so that’s the username you’ll see me use throughout this post:

>>> import spotipy.util as util
>>> util.prompt_for_user_token('loisaidasam', scope='user-library-read')

This should open a browser window and trigger you to authenticate against your newly made application. When you say yes, it’ll redirect you to your redirect uri, which you copy and paste back here:

Enter the URL you were redirected to: 

The next string it gives you is your token. Copy and paste that into a variable called token

Now, for getting your playlists…

Here’s the Spotify documentation describing how to read a user’s playlists:


but for this exercise we’ll be using Spotipy’s user_playlists() method:

We have to use limit/offset to get all of the results, so try this code:

>>> import spotipy
>>> s = spotipy.Spotify(token)
>>> offset = 0
>>> while True:
>>>     playlists = s.user_playlists('loisaidasam', offset=offset, limit=50)
>>>     if not playlists['items']:
>>>         break
>>>     for item in playlists['items']:
>>>         print item['name']
>>>     offset += len(playlists['items'])

Running this should print out all of your playlist names one by one.


Nov 032014


Recently I bought a three-pack of these cool Nike Dri-FIT Crew Socks and I guess they mold them specifically for your left and right foot?

In getting ready for work this morning, I grabbed a pair of these bad boys, and I noticed that more often than not, the laundry place where I get my laundry done seems to pair them correctly (meaning that each “L” sock is paired with a corresponding “R” sock). I was wondering if they’re just hyper-considerate laundry folders over there, or if maybe it was just a coincidence.

Not recalling my statistics, I wrote a brute-force script to figure out what percentage of the time they would be bundled “correctly” when bundled at random:


And I found the answer to be an astonishing 40%!

$ python socks.py 3 -n 100000
3 pairs of socks
100000 iterations
{False: 60011, True: 39989}
Good 39.99% of the time 

I don’t know about you, but I find that percentage to be super high! We’re saying that when choosing socks in random order, that almost half of the time they’ll end up being bundled “correctly” with three bundles of properly matched “R” and “L” socks!

Update: I finished getting ready and hopped on my bike, and as I was commuting into work I started thinking about my results, specifically wondering if I could come up with a statistical explanation, and I think I figured it out.

Steps and corresponding probabilities:

  1. Choose one sock at random (cool 100% of the time, hard to mess this one up)
  2. Choose a sock that matches (cool 60% of the time – of the 5 remaining socks, 3 should be the correct match, and 2 the wrong one)
  3. Choose another remaining sock at random (cool 100% of the time)
  4. Choose a sock that matches this sock (cool 66.666…% of the time – of the remaining 3 socks, 2 are the correct match and 1 is wrong)
  5. The last two socks will always match each other (100%)

Now using statistics, you multiply the probabilities of these events happening (right?):

1.00 * 0.60 * 1.00 * 66.666 * 1.00

or in fractions

3/5 * 2/3





Yay, math!

May 302014
Django logo

Django logo (Photo credit: Wikipedia)

My finding for today is that you can set up auto-complete for when you start typing something like this in your Django project:

./manage.py function_

but don’t remember exactly what you called that custom manage.py command.

Just download this baby and source it from your .bashrc file:


And you’re off to the races!

Enhanced by Zemanta

May 012014

Introducing… MULTIPLAYER 2048!


That fun game that just won’t die is back again, this time in multiplayer form!

To play along, join the #2048 channel on Freenode and start issuing commands like “up”/”down”/”left”/”right” (or “u”/”d”/”l”/”r” for short).

From a technical perspective, I forked the original version and modified the controls so that they no longer come from the keyboard. This multiplayer version instead uses Pusher to update clients with realtime events and a Hubot with an IRC adapter and a custom script that calls a Python/Flask webserver, stores the data in a Redis list, and triggers the Pusher updates.

I was on the front page of Hacker News for about five minutes before being booted (still learning the ropes on how HN posting works), so I guess it wasn’t a big hit. I was even in touch with the kind folks over at Pusher who bumped me up to a higher number of allowed simultaneous connections to prepare for HN traffic, but alas I never needed it. Thanks anyway Pusherinos!

Happy 2048ing!

Enhanced by Zemanta