-
Notifications
You must be signed in to change notification settings - Fork 0
/
phone_directory.rb
46 lines (42 loc) · 2.59 KB
/
phone_directory.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# Personal phone book
# On each line of the file it can find the phone number
require 'spec_helper'
def phone(string, num)
a = string.scan(/.*#{num}.*/)
return "Error => Too many people: #{num}" if a.size > 1
return "Error => Not found: #{num}" if a.empty?
a = a.first
"Phone => #{num}, Name => #{a[/<(.*)>/, 1]}, Address => #{a.gsub(/[^ ]*#{num}[^ ]*|<.*>|[,;\/]/,'')}".gsub(/[\s_]+/,' ').strip
end
describe "phone" do
$dr =
"/+1-541-754-3010 156 Alphand_St. <J Steeve>\n 133, Green, Rd. <E Kustur> NY-56423 ;+1-541-914-3010;\n"\
"+1-541-984-3012 <P Reed> /PO Box 530; Pollocksville, NC-28573\n :+1-321-512-2222 <Paul Dive> Sequoia Alley PQ-67209\n"\
"+1-741-984-3090 <Peter Reedgrave> _Chicago\n :+1-921-333-2222 <Anna Stevens> Haramburu_Street AA-67209\n"\
"+1-111-544-8973 <Peter Pan> LA\n +1-921-512-2222 <Wilfrid Stevens> Wild Street AA-67209\n"\
"<Peter Gone> LA ?+1-121-544-8974 \n <R Steell> Quora Street AB-47209 +1-481-512-2222!\n"\
"<Arthur Clarke> San Antonio $+1-121-504-8974 TT-45120\n <Ray Chandler> Teliman Pk. !+1-681-512-2222! AB-47209,\n"\
"<Sophia Loren> +1-421-674-8974 Bern TP-46017\n <Peter O'Brien> High Street +1-908-512-2222; CC-47209\n"\
"<Anastasia> +48-421-674-8974 Via Quirinal Roma\n <P Salinger> Main Street, +1-098-512-2222, Denver\n"\
"<C Powel> *+19-421-674-8974 Chateau des Fosses Strasbourg F-68000\n <Bernard Deltheil> +1-498-512-2222; Mount Av. Eldorado\n"\
"+1-099-500-8000 <Peter Crush> Labrador Bd.\n +1-931-512-4855 <William Saurin> Bison Street CQ-23071\n"\
"<P Salinge> Main Street, +1-098-512-2222, Denve\n"
it "Example cases" do
expect(phone($dr, "48-421-674-8974"))
.to eq("Phone => 48-421-674-8974, Name => Anastasia, Address => Via Quirinal Roma")
expect(phone($dr, "1-921-512-2222"))
.to eq("Phone => 1-921-512-2222, Name => Wilfrid Stevens, Address => Wild Street AA-67209")
expect(phone($dr, "1-908-512-2222"))
.to eq("Phone => 1-908-512-2222, Name => Peter O'Brien, Address => High Street CC-47209")
expect(phone($dr, "1-541-754-3010"))
.to eq("Phone => 1-541-754-3010, Name => J Steeve, Address => 156 Alphand St.")
expect(phone($dr, "1-121-504-8974"))
.to eq("Phone => 1-121-504-8974, Name => Arthur Clarke, Address => San Antonio TT-45120")
expect(phone($dr,"1-498-512-2222"))
.to eq("Phone => 1-498-512-2222, Name => Bernard Deltheil, Address => Mount Av. Eldorado")
expect(phone($dr, "1-098-512-2222"))
.to eq("Error => Too many people: 1-098-512-2222")
expect(phone($dr, "5-555-555-5555"))
.to eq("Error => Not found: 5-555-555-5555")
end
end