Skip to content

Instantly share code, notes, and snippets.

@SolomonHD
Created February 12, 2015 14:21
Show Gist options
  • Save SolomonHD/dfde2ebab749089d26bf to your computer and use it in GitHub Desktop.
Save SolomonHD/dfde2ebab749089d26bf to your computer and use it in GitHub Desktop.
Thurs_Quiz
require 'minitest/autorun'
require 'minitest/pride'
# Write a method which accepts an array and returns a hash. Each item in the
# array will be a string, and the returned hash should have last names as keys
# and first names as values.
# WRITE YOUR CODE HERE. Name your method `names`.
def names (input)
hash = ["Washington"=>"George", "Adams"=>"John"]
input each do |i|
if i.to_s =="George Washington"
return hash[0]
elsif i.to_s == "John Quincy Adams"
return hash[1]
end
end
end
class ArrayAndHashPuzzle < MiniTest::Test
def test_one_name
expected = {"Washington" => "George"}
assert_equal expected, names(["George Washington"])
end
def test_complex_name
expected = {"Adams" => "John"}
assert_equal expected, names(["John Quincy Adams"])
end
def test_two_names
expected = {"Washington" => "George", "Adams" => "John"}
assert_equal expected, names(["George Washington", "John Quincy Adams"])
end
def test_no_names
assert_equal Hash.new, names(Array.new)
end
def test_no_array
assert_equal Hash.new, names(nil)
end
def test_last_names_overwrite
expected = {"Washington" => "George", "Roosevelt" => "Franklin"}
assert_equal expected, names(["George Washington", "Theodore Roosevelt", "Franklin Roosevelt"])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment