Ruby Coding Contest Recap


Collapse Content

The Learneroo Ruby Coding Contest ran during US daytime hours, but programmers joined in from all over the world! These were the final contest winners who will receive cash prizes, sponsored by Atlassian Bitbucket:

  1. Manuel A. Valenzuela
  2. Brad Bicknell
  3. Alex

Many other participants won other prizes, such as RubyMine licenses, ebooks from Pragmatic Bookshelf, and membership on Learneroo.

Ruby Challenge Solutions

This post will discuss a couple challenges and solutions briefly, and in the future we may look at some additional challenges.

Ruby on Rails Challenge - This involved building a simple Rails app quickly. Stay tuned for a future tutorial that will guide Rails beginners through this process.

Ruby Robot Challenges - This series of challenges involved programming a Ruby Robot to clean the room. Half the challenges let you see the room, and half the challenges hid it.

The first challenge was trivial, but the other challenges were hard. Here's a fast and "hack-ish" way to pass the challenges that displayed the room: simply hard-code the steps the Robot should take in a String. Each turn of the game, simply follow the next turn described in the string. For example, the following code beats Robot Looks Around in 116 turns:

class Player

  def initialize
    @distance_from_home = 0
    @moves_ar = ("rrrrrrrrrdddllludddllluuuuulll" +
        "ddddddddddrrrrrullluuuuuuuull" +
        "dddddrrrrrrdrrrdddlllllluuuuuuuulllu")
    @move = 0
  end

  def do_something(robot)
    if robot.detects_dirt?
        robot.clean!
    else
        robot.walk!(get_direction)
        @move += 1
    end
  end

  def get_direction
    case @moves_ar[@move]
        when "r"
        return :right
        when "l"
        return :left
        when "d"
        return :down
        when "u"
        return :up
    end
  end

end

The Contest Challenges

These were the main challenges in the contest. Sum Numbers but 5 simply asked for the sum of large ranges of numbers, but you needed to perform some optimizations in order to solve the challenge in Ruby within the time limit. One simple optimization you could do would be to cancel out the positive and negative numbers instead of summing them. For example:

def do_stuff(a, b)
    if(a<0 && b>0)
        if(a.abs > b)
            b = -b-1
        else
            a = a.abs+1
        end
    end

    sum = 0
    (a..b).each do |n|
        sum += n unless n%5==0
    end
    puts sum
end

Of course, if you knew the formula to sum a range of numbers, you could use it to sum a range of numbers and then subtract the multiples of 5.

One of the contest runner-ups used this method in a clear manner:

def do_stuff(a, b)
    bigger_sum = (a+b) * (b-a+1) / 2
    a_5 = (a/5.0).ceil
    b_5 = (b/5.0).floor
    fives_sum = 5 * (b_5 + a_5) * (b_5 - a_5 + 1 ) / 2
    puts bigger_sum - fives_sum
end

The first-place winner of the contest solved this challenge with very concise code:

def do_stuff(a, b)
    puts (b+a)*(b-a+1)/2 - ((a..b).detect{|e| e % 5 == 0}..b).step(5).reduce(:+)
end

Missing Spaces asked you to detect if one string of text could be split up into words from a given list. It could be solved with recursion, such as the following:

def do_stuff(word)
    puts recursive_break(word)
end

WORDS = ["a", "able", "are", "area", "be", "car", "care", "ear", "hello", "not", "note", "or", "to", "the", "world"]

 def recursive_break(text)
    return true if text.nil? or text.length == 0
    (0..text.length).each do |i|
      word = text[0..i]
      if WORDS.include?(word)
        result = recursive_break(text[(i+1)..-1])
        return result if result
      end
    end
    return false
end

It could also be solved with a built-in regex, such as in the following super-concise code:

def do_stuff(word)
    puts !!(word =~ /^(a|or|to|be|are|car|the|not|ear|area|note|care|able|hello|world)+$/)
end

This solution is just a few characters, but it won't be able to be used in the following challenge, where you'll want to build off the recursive solution.

The Ruby contest is over but the challenges are still up, so you can still jump into any challenge and try to solve it!

Contact Us
Sign in or email us at [email protected]