# Solving the classic “aaaabbbcca” coding challenge

`Input: “aaaabbbcca”Output: [(“a”, 4), (“b”, 3), (“c”, 2), (“a”, 1)]Write a function that converts the input to the output`
`def count_in_a_row(word)    counter = 1    result = Hash.new.compare_by_identity        for i in (1..word.length)        if word[i] == word[i-1]            counter += 1        else            result[word[i-1]] = counter            counter = 1        end    end        resultend`
`# Iterate the letters in the word, starting from the second one# # If the current letter is equal to the previous one, increase the counter# # If the current letter is different to the previous one,# # save the key (the letter) with the value (the counter) in the hash,# # and then reset the counter.# Return the hash`
`puts count_in_a_row("aaaabbbcca")Output:   {"a"=>4, "b"=>3, "c"=>2, "a"=>1}Expected: [("a", 4), ("b", 3), ("c", 2), ("a", 1)]`
`def format_output(raw_output)    formatted_output = "["    raw_output.each do |key, value|        formatted_output += "(\"" + key + "\", " + value.to_s + "), "    end    formatted_output.delete_suffix!(", ")    formatted_output += "]"endputs format_output(count_in_a_row("aaaabbbcca"))Output:   [("a", 4), ("b", 3), ("c", 2), ("a", 1)]Expected: [("a", 4), ("b", 3), ("c", 2), ("a", 1)]`

--

--

## Get the Medium app

I do Management and Infrastructure Engineering.