Created
September 4, 2014 19:46
-
-
Save noqcks/549580a8f2ba560d0d10 to your computer and use it in GitHub Desktop.
w1d3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### 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