Created
July 6, 2014 21:39
-
-
Save kavinyao/5eaca2592f2a6dbb2f87 to your computer and use it in GitHub Desktop.
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
puts "Solution to 1:" | |
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10].each { |num| puts num} | |
puts | |
puts "Solution to 2:" | |
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10].each { |num| puts num if num > 5 } | |
puts | |
puts "Solution to 3:" | |
odds = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].select {|num| num % 2 == 1 } | |
puts odds | |
puts | |
puts "Solution to 4:" | |
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | |
arr.push(11) | |
arr.unshift(0) | |
puts arr | |
puts | |
puts "Solution to 5:" | |
arr.pop() | |
arr.push(3) | |
puts arr | |
puts | |
puts "Solution to 6:" | |
puts arr.uniq | |
puts | |
puts "Solution to 7:" | |
puts "Array is a sequential data structure where elements are accessed via indicies." | |
puts "Hash is an associate data structure where values are accessed via keys." | |
puts | |
puts "Solution to 8:" | |
old_style_hash = {:name => 'Tom', :age => 42, "non-symbol-key" => 1} | |
puts old_style_hash | |
new_style_hash = {name: 'Tom', age: 42, "non-symbol-key" => 1} | |
puts new_style_hash | |
puts | |
puts "Solution to 9:" | |
h = {a:1, b:2, c:3, d:4} | |
puts h[:b] | |
h[:e] = 5 | |
h.delete_if { |key, val| val < 3.5 } | |
puts h | |
puts | |
puts "Solution to 10:" | |
# gotcha: must surround hash literal with parentheses otherwise syntax error | |
puts ({key: [1, 2, 3]}) | |
puts [{key1: 1}, {key2: 2}] | |
puts | |
puts "Solution to 11:" | |
puts "ruby-doc.org seems okay to me." | |
puts | |
puts "Solution to 12:" | |
keys = [:email, :address, :phone] | |
contact_data = [["[email protected]", "123 Main st.", "555-123-4567"], | |
["[email protected]", "404 Not Found Dr.", "123-234-3454"]] | |
contacts = {"Joe Smith" => {}, "Sally Johnson" => {}} | |
contacts.keys.each do |name| | |
first_name = name.split[0].downcase | |
contact_data.each do |data| | |
if data[0].include?(first_name) | |
contacts[name] = Hash[keys.zip(data)] | |
break | |
end | |
end | |
end | |
puts contacts | |
puts | |
puts "Solution to 13:" | |
puts "Joe's email is #{contacts["Joe Smith"][:email]}" | |
puts "Sally's phone number is #{contacts["Sally Johnson"][:phone]}" | |
puts | |
puts "Solution to 14:" | |
puts "Already done in solution to 12" | |
puts | |
puts "Solution to 15:" | |
arr = ['snow', 'winter', 'ice', 'slippery', 'salted roads', 'white trees'] | |
arr.delete_if { |word| word.start_with?('s') } | |
puts arr | |
puts | |
arr = ['snow', 'winter', 'ice', 'slippery', 'salted roads', 'white trees'] | |
arr.delete_if { |word| word.start_with?('s', 'w') } | |
puts arr | |
puts | |
puts "Solution to 16:" | |
a = ['white snow', 'winter wonderland', 'melting ice', | |
'slippery sidewalk', 'salted roads', 'white trees'] | |
puts a.map { |w| w.split }.flatten | |
puts | |
puts "Solution to 17:" | |
puts "These hashes are the same!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment