Skip to content

Instantly share code, notes, and snippets.

@ashleygwilliams
Created June 19, 2013 00:02
Show Gist options
  • Save ashleygwilliams/5810622 to your computer and use it in GitHub Desktop.
Save ashleygwilliams/5810622 to your computer and use it in GitHub Desktop.
practice using nested data structures with each
# create a new hash that has this structure
# {
# "A" => "a"
# "B" => "b"
# ...
# "Z" => "z"
#}
#using a single each loop
#know that you can do (A..Z) and (a..z)
# make a hash that will store our final values
# create an each statement to iterate over the range A..Z
#alphabet = {}
#('A'..'Z').each do |letter|
# alphabet[letter] = letter.downcase
#end
#puts alphabet
# now make it so that it prints 2 letters
# e.g "A" => "aa"
#alphabet = {}
#('A'..'Z').each do |letter|
# alphabet[letter] = letter.downcase * 2
#end
#puts alphabet
#now make a hash that looks like this
# "A" => ['a']
#alphabet = {}
#('A'..'Z').each do |letter|
# alphabet[letter] = [letter.downcase]
#end
#
#puts alphabet
# now make a hash that looks like this
# "A" => ['a', 'a']
#alphabet = {}
#('A'..'Z').each do |letter|
# alphabet[letter] = [letter.downcase, letter.downcase]
#end
#puts alphabet
#now do the same thing, but only say letter.downcase once
#alphabet = {}
#('A'..'Z').each do |letter|
# alphabet[letter] = []
# 2.times do
# alphabet[letter].push(letter.downcase)
# end
#end
#puts alphabet
# now make a hash where it only puts the letter if the letter is the letter O
#alphabet = {}
#('A'..'Z').each do |letter|
# alphabet[letter] = []
# if letter == 'O'
# alphabet[letter].push(letter.downcase)
# end
#end
#puts alphabet
## now make a hash that only pushs if the letter is part of the the special letters array
#special_letters = ['a','l','e','x']
#alphabet = {}
#('A'..'Z').each do |letter|
# alphabet[letter] = []
# if special_letters.include?(letter.downcase)
# alphabet[letter].push(letter.downcase)
# end
#end
#puts alphabet
#make a hash that only add the lowercase letter if the letter is in the name
#name = "alex"
#alphabet = {}
#
#('A'..'Z').each do |letter|
# alphabet[letter] = []
# if (name.split('')).include?(letter.downcase)
# alphabet[letter].push(letter.downcase)
# end
#end
#puts alphabet
# make a hash that looks like this
# "A" => {"a"=>"a"}
#name = "alex"
#alphabet = {}
#
#('A'..'Z').each do |letter|
# alphabet[letter] = {}
# alphabet[letter][letter.downcase] = letter.downcase
#end
#
#puts alphabet
# now make a hash that only adds the key value pair if the letter is in name
# "A" => {"a"=>"a"}, "B" => {} ... "E" => {"e"=>"e"}..
#name = "alex"
#alphabet = {}
#('A'..'Z').each do |letter|
# alphabet[letter] = {}
# if name.split('').include?(letter.downcase)
# alphabet[letter][letter.downcase] = letter.downcase
# end
#end
#puts alphabet
# now make an array that gives a value to the nested key only if it is in name, give everything else a 0
# e.g. "A" => {"a" => "a"}, "B" => {"b" => 0}
#name = "alex"
#alphabet = {}
#('A'..'Z').each do |letter|
# alphabet[letter] = {}
# if name.split('').include?(letter.downcase)
# alphabet[letter][letter.downcase] = letter.downcase
# end
#end
#puts alphabet
# now make an array that assign a 1 as the value to the nested key if the letter appears in name, otherwise give a 0
#e.g. "A" => {"a" => 1}, "B" => {"b" => 0}
#name = "alex"
#alphabet = {}
#('A'..'Z').each do |letter|
# alphabet[letter] = {}
# if name.split('').include?(letter.downcase)
# alphabet[letter][letter.downcase] = 1
# else
# alphabet[letter][letter.downcase] = 0
# end
#end
#now assign the nested letter a count that reflects how many times that letter appears in the name
name = "williams"
alphabet = {}
('A'..'Z').each do |letter1|
alphabet[letter1] = {}
alphabet[letter1][letter1.downcase] = 0
name.split('').each do |letter2|
if letter1.downcase == letter2
alphabet[letter1][letter1.downcase] += 1
end
end
end
puts alphabet
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment