Skip to content

Instantly share code, notes, and snippets.

@joshuastr
Created April 29, 2016 14:59
Show Gist options
  • Save joshuastr/10e5ec311d2837da8c428cabe39803cf to your computer and use it in GitHub Desktop.
Save joshuastr/10e5ec311d2837da8c428cabe39803cf to your computer and use it in GitHub Desktop.
def count_letters(str)
data = {}
str.split('').each do |l|
puts l
next if l == ' '
if data[l].nil?
data[l] = 1
else
data[l] += 1
end
end
p data
end
#count_letters("counting these letters")
def letter_count_index(str)
data = Hash.new {|h,k| h[k]=[]}
str.split('').each_with_index do |item, index|
data[item] << index
end
p data
end
letter_count_index("counting these letters")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment