Announcing: Approximate Speed 1.0

My friends and I enjoy auto racing, and every year, we make the pilgrimage to the Indy 500. This year, one friend wanted a way to determine the average speed of a driver on the track. Instead of stop watches and maths, I decided to write an app to do the work for us.

Approximate Speed is an iOS that, given a fixed distance, can be used to determine the average speed of an object. Check out more information about Approximate Speed, or get it on the App Store.

Announcing: Approximate Speed 1.0

Announcing: Structure 1.0

Before Swift, projects that I worked on used SQLite directly. I could have done the same with Swift, but it felt like I could benefit from writing a wrapper. I used this as a means to teach myself Swift, as well as create something that I could reuse throughout my projects.

Enter Structure! Structure is by no means a complete SQLite wrapper. This framework performs the basic CRUD operations I need, as well as maintain some semblance of thread safety. It also provides simple data access and value transformations.

If you’re curious, check out the GitHub page. There is some simple documentation and lots of tests to get a feel for how the framework works.

Announcing: Structure 1.0

Monitoring Processes on the Command Line

Updated 2012-11-06 with an alternative.

Sometimes you have a long running process that is firing off many commands in a row and you don’t get much output from the command. I like to have an idea of what’s happening, so I employ a simple Bash script that I’ve hand typed a million times. It keeps just the data I want on screen and auto-updates ever couple of seconds, so I can keep an eye on it while I’m working.

while true
do
    # Some command goes here
    sleep 2
    clear
done

If you have any modern shell, you can type this in with the line breaks. Now I have a clear console that is showing me just the data I want.

How do I use this? Right now, I have an apt-mirror post mirror script that rsyncs multiple directories. This is an automated process that runs quietly, so I can use ps -ef | grep rsync to monitor the process. I’ve also used this to monitor massive file creation / deletions, so the command could be ls -l | wc -l to count the files in the director. I use Hylafax, so I can monitor the modems with faxstat -a.

An Alternative

After posting this article, it was brought to my attention that I could just use the watch command. As with a lot of complex technologies, you tend to learn to do something one way and stay oblivious to other possible ways. watch is part of the procps project, so instead of the above loop, you could use:

watch "ps -ef | grep rsync"

This by default runs the given command every 2 seconds. Check out the man page for more options.

Unfortunately, watch is not part of the default UNIX stack on OS X. It seems to be available in Homebrew, but since it isn’t already on my system, I’ll probably stick with the bash script. Old habits die hard.

Monitoring Processes on the Command Line

“Cheating” at Words With Friends

It’s time to admin something. I cheat at Words With Friends. I only justify it with the fact that I at least wrote the code to help me cheat.

And cheat really isn’t the word for it. Sometimes I just stare at my letter rack for hours and come up with nothing. I wrote the following script just to get my brain going. You have two options with this script. Argument 1 always has to be your letter rack. You can use the “+” sign for the blank tile. Argument 2 is regular expression if you are looking for something specific.

If you have the tile rack aabbccd, you would run words-help aabbccd and get a dump of every combination. If you are looking for words with ba at the beginning of the word, you would run words-help aabbccd ^ba.

This script should work on anything UNIX-like that has a dictionary file at /usr/share/dict/words. You can edit the script as you need.

#!/usr/bin/env ruby

WORDS_FILE = '/usr/share/dict/words'

class Engine

  def initialize(file)
    @file = file

    read_dictionary
  end

  def go(letters, regex = nil) 
    @letters = letters.split ''
    @regex = Regexp.new(regex) unless regex.nil?
    @words = []

    # Find the words
    letter_combinations do |letter_combination|
      letter_combination.permutation.to_a.each do |letter_array|
        word = letter_array.join('')
        if @dictionary.key?(word)
          if @regex.nil?
            @words << word
          else
            @words << word if @regex.match(word)
          end
        end
      end
    end

    # Clean up
    @words.sort.uniq
  end

  private

  def letter_combinations
    max = (2 ** @letters.count) - 1
    1.upto(max) do |i|
      word = []
      mask = sprintf("%0#{@letters.count}b", i).split('')
      mask.each_with_index do |bit, i|
        word << @letters[i] if bit == '1'
      end

      # Substitute wild cards
      idx = word.index '+'
      if idx.nil?
        yield word
      else
        ('a'..'z').each do |l|
          word[idx] = l
          yield word
        end
      end
    end
  end

  def read_dictionary
    # Build the dictionary
    @dictionary = {}

    # Read the words
    File.open(@file, 'r') do |file|
      file.each_line do |line|
        line.strip!

        # Skip proper nouns
        next if line =~ /[A-Z]/

        # Skip 1 letter words
        next if line.length == 1

        # Add the word to the dictionary
        @dictionary[line] = 1
      end
    end
  end

end

if ARGV.length < 1 || ARGV.length > 2
  $stderr.puts "Letters are required"
  exit
end

e = Engine.new WORDS_FILE
e.go(ARGV[0], ARGV[1]).each { |word| puts word }

This script is brute force, so the more letters you set as Argument 1, the longer it will take to run. You’ve been warned.

“Cheating” at Words With Friends

Visualizing Problem 15 of Project Euler

I’ve been working through Project Euler as a coding / math exercise. Problem 15 asks to find the amount of paths that exist between the top-left and bottom-right points of a 20×20 grid, without backtracking. I decided to visualize it, because just running a command line program and waiting can be boring.

Of course, this problem is silly to brute force, but by watching the visualization and working on a pad of paper, I came up with a much faster solution to solving the problem.

The code is available on github. It’s Ruby and Tk. Some Ruby installs don’t compile with Tk, so if the script doesn’t load the tk requirement, Google around for the solution.

Also note, this is a quick hack. It leaks memory, it’s buggy, it might eat your processor and will run for a very long time.

Visualizing Problem 15 of Project Euler