Tuesday, February 28, 2012

Ruby Programming Challenge: Scrabble Utility

This is another in a series of Ruby programming challenges that we do to keep the tech skills strong on our test team.

This challenge starts with some existing code.  This script was written to find the longest word that can be written using only the letters in the top of keys on a keyboard.  The script opens a web site with Scrabble words and reads in all the words and then processes against an algorithm (BTW, the algorithm can be written more efficiently). 

Here is the challenge:  Based on the existing code, enhance it (or scrap it completely) so that it useful as a Scrabble utility.  1.  Given a list of characters (“ASFAGJS”, for example), determine the longest Scrabble word that can be created (Scrabble rules:  each character can be used only once). 2.   Also, given the same list of characters, determine what word scores highest (http://en.wikipedia.org/wiki/Scrabble_letter_distributions#English).

BTW, depending on how you run this script, there is an inefficiency that you may want to solve.  Each time the existing code runs, it goes to the internet to get the word list.  If your script runs for more than one set of characters, it would be efficient if you get the list of words only once.

While working on this challenge, I expect you will need to be comfortable with ruby arrays and hashes.  If the solution comes easy for you, feel free to enhance things further – be creative!

Here is the existing code:

require 'open-uri'
def restrict(html, starting_regexp, stopping_regexp)
 start = html.index(starting_regexp)
 stop = html.index(stopping_regexp, start)
url = 'http://homepage.ntlworld.com/adam.bozon/Dictionary.htm'
page = open(url)
text = page.read; nil
words = restrict(text, /AA/, /ZYZZYVAS/)
array_words = words.split
@longest_word = ''
letters = ['Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P']
array_words.each do |word|
 @all_okay = true
 word.each_char.each do |character|            
   if (letters.include? character).to_s == 'false' then
     @all_okay = false
  if @all_okay then
    puts word
            if word.size > @longest_word.size then
              @longest_word = word
puts @longest_word

Tuesday, February 21, 2012

Cool Ruby Tools -- Sinatra and Web Applications

As I have written before, during the past year, our test team has been working with Ruby to grow our technical skills.  We have learned a lot and written a couple great testing utilities (Ruby Smalls - for small utilities).  We have published our Smalls as executables using the Ocra gem (a really slick packaging utility).  But there are times when deployed executables are not the best answer.  Recently, we have been looking at building our testing tools as web applications.  With Ruby, there is the grand daddy, Ruby on Rails, and there is the cool daddy, Sinatra.

Using Sinatra  (http://www.sinatrarb.com/), I  can build and run web-based applications that run Ruby code.  Sinatra enables me build pages and services with html and executable Ruby.  Sinatra is a good choice when you don't need the full framework and resources of a Rails project.  (Also, I have not learned Rails yet :))

After reviewing Sinatra with our architecture lead and a couple hours of playing, I can tell a couple things.  I have a lot to learn; I think it will be fun to learn; and there are cool possibilities.  For example, we could build a platform for our Ruby Smalls that anyone on our intranet could access and use, or we could use this as the basis of an automated test framework.

Time to get back to RubyMine and programming -- lots to do before work tomorrow.