Skip to content

Instantly share code, notes, and snippets.

@allthetime
Forked from kvirani/README.md
Last active August 29, 2015 14:06
Show Gist options
  • Save allthetime/bfadabb58ee1ae9bbbf9 to your computer and use it in GitHub Desktop.
Save allthetime/bfadabb58ee1ae9bbbf9 to your computer and use it in GitHub Desktop.

Write a method count_letters that can tell us all the unique characters that exist in a string that is passed into the method. Furthermore, the method should also report back how many instances of each letter were found in the string.

Methods generally return one object. That object can be a number, a boolean, nothing (nil), a symbol, a string, and so on. However, we need to return an object that can represent all the characters found in the input string, and their respective counts.

What type of object should be returned back by count_letters? A Hash comes to mind!

So count_letters("lighthouse in the house...") would return:

{
  'l' => 1,
  'i' => 2,
  'g' => 1,
  'h' => 4,
  't' => 2,
  'o' => 2,
  'u' => 2,
  's' => 2,
  'e' => 3,
  'n' => 1,
  '.' => 3
}

Tip: Use .inspect on any object to turn it into a debug-friendly string... great for putsing objects like Hashes to the screen.

Also, note that we are skipping and not counting spaces.

Return indices instead

Instead of returning just the count of each letter, the requirement has changed to return all the indices (positions) in the string where each character is found. So for each letter, we're no longer returning just one number but rather multiple numbers (potentially)

What would the resulting hash look like with the example string above?

Update your method so that it works this way now.

Pro Tip: While you could keep track of the index in a variable as you loop through each letter in the string with Enumerable#each, there is also Enumerable#each_with_index that can streamline this a little bit.

require "pp"
def count_letters(string)
count = Hash.new(0)
indeces = Hash.new(nil)
result = Hash.new
split = string.split('').map{|x| x.downcase}
split.select!{|x| x != ' '}
split.each do |x|
count[x] += 1
end
split.each_with_index do |item,index|
indeces[item] = [] if indeces[item] == nil
indeces[item] << index
end
count.keys.each do |key|
result[key] = {
count:count[key],
indeces:indeces[key],
}
end
pp result
end
count_letters('The hammer hammered the nail on the head')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment