From 4fbacfaf2a8fdac29e06df4b09b92ae228a55b51 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Tue, 30 Aug 2016 02:19:29 +0200 Subject: Solve phone --- go/phone-number/README.md | 38 +++++++++++ go/phone-number/phone_number.go | 38 +++++++++++ go/phone-number/phone_number_test.go | 118 +++++++++++++++++++++++++++++++++++ 3 files changed, 194 insertions(+) create mode 100644 go/phone-number/README.md create mode 100644 go/phone-number/phone_number.go create mode 100644 go/phone-number/phone_number_test.go (limited to 'go') diff --git a/go/phone-number/README.md b/go/phone-number/README.md new file mode 100644 index 0000000..ce88c4e --- /dev/null +++ b/go/phone-number/README.md @@ -0,0 +1,38 @@ +# Phone Number + +Write a program that cleans up user-entered phone numbers so that they can be sent SMS messages. + +The rules are as follows: + +- If the phone number is less than 10 digits assume that it is bad + number +- If the phone number is 10 digits assume that it is good +- If the phone number is 11 digits and the first number is 1, trim the 1 + and use the last 10 digits +- If the phone number is 11 digits and the first number is not 1, then + it is a bad number +- If the phone number is more than 11 digits assume that it is a bad + number + +We've provided tests, now make them pass. + +Hint: Only make one test pass at a time. Disable the others, then flip +each on in turn after you get the current failing one to pass. + +To run the tests simply run the command `go test` in the exercise directory. + +If the test suite contains benchmarks, you can run these with the `-bench` +flag: + + go test -bench . + +For more detailed info about the Go track see the [help +page](http://exercism.io/languages/go). + +## Source + +Event Manager by JumpstartLab [http://tutorials.jumpstartlab.com/projects/eventmanager.html](http://tutorials.jumpstartlab.com/projects/eventmanager.html) + +## Submitting Incomplete Problems +It's possible to submit an incomplete solution so you can see how others have completed the exercise. + diff --git a/go/phone-number/phone_number.go b/go/phone-number/phone_number.go new file mode 100644 index 0000000..f88145a --- /dev/null +++ b/go/phone-number/phone_number.go @@ -0,0 +1,38 @@ +package phonenumber + +import ( + "fmt" + "unicode" +) + +func Number(s string) (string, error) { + r := []rune{} + for _, v := range s { + if unicode.IsNumber(v) { + r = append(r, v) + } + } + switch { + case len(r) == 11 && r[0] == '1': + return string(r)[1:], nil + case len(r) == 10: + return string(r), nil + } + return "", fmt.Errorf("bad number") +} + +func AreaCode(s string) (string, error) { + num, err := Number(s) + if err != nil { + return "", err + } + return num[:3], nil +} + +func Format(s string) (string, error) { + num, err := Number(s) + if err != nil { + return "", err + } + return fmt.Sprintf("(%v) %v-%v", num[:3], num[3:6], num[6:]), nil +} diff --git a/go/phone-number/phone_number_test.go b/go/phone-number/phone_number_test.go new file mode 100644 index 0000000..2effa00 --- /dev/null +++ b/go/phone-number/phone_number_test.go @@ -0,0 +1,118 @@ +package phonenumber + +import ( + "testing" +) + +type testCase struct { + input string + expected string + expectErr bool +} + +var numberTests = []testCase{ + {"(123) 456-7890", "1234567890", false}, + {"123.456.7890", "1234567890", false}, + {"1234567890", "1234567890", false}, + {"12345678901234567", "", true}, + {"21234567890", "", true}, + {"123456789", "", true}, +} + +func TestNumber(t *testing.T) { + for _, test := range numberTests { + actual, actualErr := Number(test.input) + if actual != test.expected { + t.Errorf("Number(%s): expected [%s], actual: [%s]", test.input, test.expected, actual) + } + // if we expect an error and there isn't one + if test.expectErr && actualErr == nil { + t.Errorf("Number(%s): expected an error, but error is nil", test.input) + } + // if we don't expect an error and there is one + if !test.expectErr && actualErr != nil { + t.Errorf("Number(%s): expected no error, but error is: %s", test.input, actualErr) + } + } +} + +func BenchmarkNumber(b *testing.B) { + b.StopTimer() + for _, test := range numberTests { + b.StartTimer() + for i := 0; i < b.N; i++ { + Number(test.input) + } + b.StopTimer() + } +} + +var areaCodeTests = []testCase{ + {"1234567890", "123", false}, + {"213.456.7890", "213", false}, + {"213.456.7890.2345", "", true}, + {"213.456", "", true}, +} + +func TestAreaCode(t *testing.T) { + for _, test := range areaCodeTests { + actual, actualErr := AreaCode(test.input) + if actual != test.expected { + t.Errorf("AreaCode(%s): expected [%s], actual: [%s]", test.input, test.expected, actual) + } + // if we expect an error and there isn't one + if test.expectErr && actualErr == nil { + t.Errorf("AreaCode(%s): expected an error, but error is nil", test.input) + } + // if we don't expect an error and there is one + if !test.expectErr && actualErr != nil { + t.Errorf("AreaCode(%s): expected no error, but error is: %s", test.input, actualErr) + } + } +} + +func BenchmarkAreaCode(b *testing.B) { + b.StopTimer() + for _, test := range areaCodeTests { + b.StartTimer() + for i := 0; i < b.N; i++ { + AreaCode(test.input) + } + b.StopTimer() + } +} + +var formatTests = []testCase{ + {"1234567890", "(123) 456-7890", false}, + {"11234567890", "(123) 456-7890", false}, + {"112345", "", true}, + {"11234590870986", "", true}, +} + +func TestFormat(t *testing.T) { + for _, test := range formatTests { + actual, actualErr := Format(test.input) + if actual != test.expected { + t.Errorf("Format(%s): expected [%s], actual: [%s]", test.input, test.expected, actual) + } + // if we expect an error and there isn't one + if test.expectErr && actualErr == nil { + t.Errorf("Format(%s): expected an error, but error is nil", test.input) + } + // if we don't expect an error and there is one + if !test.expectErr && actualErr != nil { + t.Errorf("Format(%s): expected no error, but error is: %s", test.input, actualErr) + } + } +} + +func BenchmarkFormat(b *testing.B) { + b.StopTimer() + for _, test := range areaCodeTests { + b.StartTimer() + for i := 0; i < b.N; i++ { + Format(test.input) + } + b.StopTimer() + } +} -- cgit v1.2.3