Skip to content

Instantly share code, notes, and snippets.

@dmerrick
Created January 4, 2010 20:37
Show Gist options
  • Save dmerrick/268838 to your computer and use it in GitHub Desktop.
Save dmerrick/268838 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby -wKU
# http://en.wikipedia.org/wiki/Quadratic_alphabet_code
require 'pp'
class TapCode
attr_reader :letters, :array
def initialize
# build array of letters
@letters = ('A'..'Z').to_a - ['K']
# this array will store chunks of 5 letters
@array = []
# split the array of letters into chunks of 5
size = 5
0.upto(size-1) do |n|
start = n*size
finish = start+size-1
@array[n] = @letters[(start..finish)]
end
end
# method to look up a letter and convert it to the Tap Code
def lookup(letter)
@array.map{|a| a.index(letter.upcase) }.each_with_index do |column,row|
# each of these looks is an array of size 5... i.e.
# D => [3, nil, nil, nil, nil]
# N => [nil, nil, 2, nil, nil]
# Z => [nil, nil, nil, nil, 4]
next if column.nil?
return "#{'.'*(row+1)} #{'.'*(column+1)}"
end
# anything else (like a space) is nil
return nil
end
# method to convert a string to a series of taps
def encode(message)
message.sub(/ /,'').split('').inject(""){|s,l| s + lookup(l) + " "}
end
# TODO: decode() method
end
if $0 == __FILE__
tap = TapCode.new
# print the array
pp tap.array
# print the mapping for each letter
tap.letters.each do |letter|
puts letter + ": " + tap.lookup(letter)
end
puts "hello world: " + tap.encode("hello world")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment