Codehire Cup Solutions: Practice Challenge

A solution in Ruby to the Practice Challenge in the inaugural Codehire Cup held in September 2012 at the University of Adelaide.

In September 2012, Codehire held the inaugural Codehire Cup in Adelaide, featuring some relatively simple coding problems. Contestants could choose to use C#, Java, JavaScript, PHP, or Ruby to submit solutions through a web-based interface. Fastest contestant wins.

The Problem

The Practice Challenge read:

Given an input string (of no more than 300 characters in length), parse the string and count the occurrences of vowels and consonants. The data should be output in the format below (ie; JSON). Punctuation, numbers and any other characters should be ignored.




The Solution

The simplest solution is to use the built-in Ruby count method to count the vowels and consonants:

str = input.downcase
num_vowels = str.count "aeiou"
num_consonants = str.count "bcdfghjklmnpqrstvwxyz"

output << "{vowels:#{num_vowels},consonants:#{num_consonants}}"

Another Solution

It became somewhat of a contest to see who could squeeze a solution to this problem into the fewest number of characters. Bearing in mind that the input string may contain characters that are neither vowels nor consonants (such as whitespace or punctuation), my solution in 93 characters is set out below:


More Codehire Cup Solutions

Tags: Codehire Cup, coding contests