Last active
August 29, 2015 14:00
-
-
Save Heath101/11251530 to your computer and use it in GitHub Desktop.
Phone Directory codewars kata (working)
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
# Write a directory class which will manage a phone directory for a company. | |
def directory_lookup(directory, input) | |
r = /\A#{input}/ | |
d = directory.dup.keep_if {|name| name_to_num(name.split.last).match(r) } | |
if d.empty? | |
e = directory.dup.map { |name| name_to_num(name.split.last).chr} | |
e.keep_if {|n| n > input.chr }.first | |
return directory_lookup(directory, e) unless e.empty? | |
directory.last | |
else | |
d.sort! {|a, b| a.split.last <=> b.split.last } | |
d.first | |
end | |
end | |
def name_to_num(name) | |
num_map = Hash[('a'..'z').to_a.zip([2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,7,8,8,8,9,9,9,9])] | |
name.downcase.each_char.with_object('') do |char, mem| | |
mem << num_map[char].to_s | |
end | |
end | |
describe 'phone directory' do | |
# before do | |
# directory = ['Sarah Miller', 'John Albert', 'Michael Davis', 'Bill Yates'] | |
# end | |
it 'matches exact full input' do | |
directory = ['Sarah Miller', 'John Albert', 'Michael Davis', 'Bill Yates'] | |
Test.assert_equals(directory_lookup(directory,'252378'), 'John Albert') | |
Test.assert_equals(directory_lookup(directory,'32847'), 'Michael Davis') | |
end | |
it 'matches partial input of 4 digits' do | |
directory = ['Sarah Miller', 'John Albert', 'Michael Davis', 'Bill Yates'] | |
Test.assert_equals(directory_lookup(directory,'2523'),'John Albert') | |
Test.assert_equals(directory_lookup(directory,'3284'),'Michael Davis') | |
end | |
it 'matches partial input of 2 digits' do | |
directory = ['Sarah Miller', 'John Albert', 'Michael Davis', 'Bill Yates'] | |
Test.assert_equals(directory_lookup(directory,'25'),'John Albert') | |
Test.assert_equals(directory_lookup(directory,'32'),'Michael Davis') | |
end | |
it 'matches partial input of 1 digits' do | |
directory = ['Sarah Miller', 'John Albert', 'Michael Davis', 'Bill Yates'] | |
Test.assert_equals(directory_lookup(directory,'2'),'John Albert') | |
Test.assert_equals(directory_lookup(directory,'3'),'Michael Davis') | |
end | |
it 'matches first entry when 2 names have the same input' do | |
directory = ['Sarah Miller', 'John Albert', 'Michael Davis', 'Bill Yates', 'Mark Alfred'] | |
Test.assert_equals(directory_lookup(directory,'25'),'John Albert') | |
directory << 'Mark Alan' | |
Test.assert_equals(directory_lookup(directory,'25'),'Mark Alan') | |
end | |
it 'returns the closest name after the number entered' do | |
directory = ['Sarah Miller', 'John Albert', 'Michael Davis', 'Bill Yates'] | |
Test.assert_equals(directory_lookup(directory, '4'),'Sarah Miller') | |
Test.assert_equals(directory_lookup(directory, '1'),'John Albert') | |
end | |
it 'returns the last name if there are no names after the input' do | |
directory = ['Sarah Miller', 'John Albert', 'Michael Davis', 'Bill Yates','Chris Yellow'] | |
Test.assert_equals(directory_lookup(directory, '94'),'Chris Yellow') | |
end | |
########################################################################################## | |
require 'minitest/autorun' | |
# Write a directory class which will manage a phone directory for a company. | |
def directory_lookup(directory, input) | |
r = /\A#{input}/ | |
d = directory.dup.keep_if {|name| name_to_num(name.split.last).match(r) } | |
if d.empty? | |
e = directory.dup.map { |name| name_to_num(name.split.last).chr} | |
e.keep_if {|n| n > input.chr }.first | |
return directory_lookup(directory, e) unless e.empty? | |
directory.last | |
else | |
d.sort! {|a, b| a.split.last <=> b.split.last } | |
d.first | |
end | |
end | |
def name_to_num(name) | |
num_map = Hash[('a'..'z').to_a.zip([2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,7,8,8,8,9,9,9,9])] | |
name.downcase.each_char.with_object('') do |char, mem| | |
mem << num_map[char].to_s | |
end | |
end | |
describe 'phone directory' do | |
let(:directory) { ['Sarah Miller', 'John Albert', 'Michael Davis', 'Bill Yates'] } | |
it 'matches exact full input' do | |
assert_equal('John Albert', directory_lookup(directory,'252378')) | |
assert_equal('Michael Davis', directory_lookup(directory,'32847')) | |
end | |
it 'matches partial input of 4 digits' do | |
assert_equal('John Albert', directory_lookup(directory,'2523')) | |
assert_equal('Michael Davis', directory_lookup(directory,'3284')) | |
end | |
it 'matches partial input of 2 digits' do | |
assert_equal('John Albert', directory_lookup(directory,'25')) | |
assert_equal('Michael Davis', directory_lookup(directory,'32')) | |
end | |
it 'matches partial input of 1 digits' do | |
assert_equal('John Albert', directory_lookup(directory,'2')) | |
assert_equal('Michael Davis', directory_lookup(directory,'3')) | |
end | |
it 'matches first entry when 2 names have the same input' do | |
d = directory << 'Mark Alfred' | |
assert_equal('John Albert', directory_lookup(d,'25')) | |
d << 'Mark Alan' | |
assert_equal('Mark Alan', directory_lookup(d,'25')) | |
end | |
it 'returns the closest name after the number entered' do | |
assert_equal('Sarah Miller', directory_lookup(directory, '4')) | |
assert_equal('John Albert', directory_lookup(directory, '1')) | |
end | |
it 'returns the last name if there are no names after the input' do | |
d = directory << 'Chris Yellow' | |
assert_equal('Chris Yellow', directory_lookup(d, '94')) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment