Last active
August 29, 2015 14:15
-
-
Save aellispierce/06160720d0e894c6245c to your computer and use it in GitHub Desktop.
Array/Hash Puzzle
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
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. | |
def names(array) | |
if array = Array.new || nil | |
Hash.new | |
else | |
h1 = Hash[*array] | |
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