Getting started with Ruby – Part 6

After having covered most of the basics of Ruby, lets get started on an actual problem, just like I had promised in my previous article – Getting started with Ruby – Part 5

I am taking a relatively simple problem that came in Google Codejam 2009 qualification round. Here is the direct link to the problem : Alien Language

After years of study, scientists at Google Labs have discovered an alien language transmitted from a faraway planet. The alien language is very unique in that every word consists of exactly L lowercase letters. Also, there are exactly D words in this language.

Once the dictionary of all the words in the alien language was built, the next breakthrough was to discover that the aliens have been transmitting messages to Earth for the past decade. Unfortunately, these signals are weakened due to the distance between our two planets and some of the words may be misinterpreted. In order to help them decipher these messages, the scientists have asked you to devise an algorithm that will determine the number of possible interpretations for a given pattern.

A pattern consists of exactly L tokens. Each token is either a single lowercase letter (the scientists are very sure that this is the letter) or a group of unique lowercase letters surrounded by parenthesis ( and ). For example: (ab)d(dc) means the first letter is either a or b, the second letter is definitely d and the last letter is either d or c. Therefore, the pattern (ab)d(dc) can stand for either one of these 4 possibilities: add, adc, bdd, bdc.


The first line of input contains 3 integers, L, D and N separated by a space. D lines follow, each containing one word of length L. These are the words that are known to exist in the alien language. N test cases then follow, each on its own line and each consisting of a pattern as described above. You may assume that all known words provided are unique.


For each test case, Output

Case #X: K

where X is the test case number, starting from 1, and K indicates how many words in the alien language match the pattern.


Small dataset

1 ≤ L ≤ 10 1 ≤ D ≤ 25 1 ≤ N ≤ 10 Large dataset

1 ≤ L ≤ 15 1 ≤ D ≤ 5000 1 ≤ N ≤ 500


Input Output

3 5 4 Case #1: 2
abc Case #2: 1
abc Case #3: 3
bca Case #4: 0

If you look at the problem, it’s pretty simple to comprehend. If you have done some reasonable amount of programming, and you have come across Regular Expressions, you should be easily able to identify the pattern here that we need to apply. I am sure there might be other solutions, but here I demonstrate my solution. I am not going to dive into regular expressions here, I would recommend you to do your own research on it if you don’t know them well. So here is our solution for the problem :

# gets is a function that returns input from the console as a string. Here we
# then call the `String#split` function which splits the string delimited by
# a space by default and will return an array of the values. However it will be
# an array of strings, and since we need integers, we can simply call the
# String#map OR String#collect function which will loop through the array and
# execute the block on each and return a new array with it. In this case since
# we call to String#to_i method, the map returns an array of
# 3 integers, which gets assigned to L, D, N and since we know the length of the
# array is 3, each L, D, N gets assigned to each element within the array.
L, D, N ={|i| i.to_i}

words = []

# To fetch D words each of length L.
D.times do |i|
    # Here we will get the words that exist in the alien language
    words[i] = gets

# To fetch N test cases.
N.times do |i|
    # Here we will get the test cases and convert them to a Regular Expression. The
    # String#gsub method is used to substitute a pattern with a given input within
    # the string and it returns a new string object with the substituted value.
    r ='(','[').gsub(')',']').to_s)
    count = 0
    words.each do |w|
        # Alternatively you could also use the syntax w =~ r, which practically
        # does the same thing.
        count+=1 if r.match(w)
    puts "Case ##{i+1}: #{count}\n"

# In order to execute the above code, save the code within a file say alien.rb.
# You can execute the program by calling : `ruby alien.rb` Then you can input the
# sample test content manually. Although a better approach would be to save your
# sample input within an input file, say sample_input.txt and use redirection to
# pass it to your program, example : `ruby alien.rb < sample_input.txt` The
# output in our program is output onto the console. If you wish to save that in
# a file, we can again use redirection to save the output in a file, example:
# ruby alien.rb < sample_input.txt > output.txt

The above solution will help you realize how simple & intuitive it is to code in Ruby :). I hope you liked this article. If you have any queries, suggestions or feedback please let me know in the comments below.

comments powered by Disqus