Skip to content

Instantly share code, notes, and snippets.

@noqcks
Created September 4, 2014 19:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noqcks/549580a8f2ba560d0d10 to your computer and use it in GitHub Desktop.
Save noqcks/549580a8f2ba560d0d10 to your computer and use it in GitHub Desktop.
w1d3
### 1. Tells the Numbers of Characters
def count_letters(string)
array = string.delete(" ").split("")
hash = Hash.new(0)
array.each do |letter|
letter = letter.downcase
hash[letter] += 1
end
puts hash.inspect
end
count_letters("This is a stupid string that I'm using")
### 2. Returns the indice positions of characters
def count_indices(string)
array = string.delete(" ").split("")
hash = Hash.new { |hash,key| hash[key] = []}
array.each_with_index do |letter, index|
hash[letter.downcase.to_sym] << index
end
puts hash.inspect
end
count_indices("This is a stupid string that I'm using")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment