Monday, August 19, 2013

Coderbyte: Simple Symbols (Ruby)

PROBLEM

Have the function SimpleSymbols(str) take the str parameter being passed and determine if it is an acceptable sequence by either returning the string true or false. The str parameter will be composed of + and = symbols with several letters between them (ie. ++d+===+c++==a) and for the string to be true each letter must be surrounded by a + symbol. So the string to the left would be false. The string will not be empty and will have at least one letter.

SOLUTION

def SimpleSymbols(str)
    str_array = str.split('')
    if (/[a-zA-Z]/ =~ str) != nil
        str_array.each_with_index { |placeholder, index|
         return "false" if /[a-zA-Z]/ =~ str_array[0]
         return "false" if placeholder =~ /[a-zA-Z]/ and (str_array[index-1] != "+" or str_array[index+1] != "+")
        }
        return "true"
    else
        return "false"
    end
end
   
print SimpleSymbols(STDIN.gets)