Merge Go Work

This commit is contained in:
2016-08-23 15:47:19 -05:00
parent b8814259b5
commit 9e58d17c5c
25 changed files with 941 additions and 50 deletions

38
go/phone-number/README.md Normal file
View File

@@ -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.

View File

@@ -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()
}
}

View File

@@ -0,0 +1,44 @@
package phonenumber
import (
"errors"
"strings"
)
// Number takes a number and validates it
func Number(num string) (string, error) {
num = strings.Map(func(r rune) rune {
if r >= '0' && r <= '9' {
return r
}
return -1
}, num)
// A number can be 11 digits long, if it starts with a 1
// but ignore that one.
if len(num) == 11 && num[0] == '1' {
num = num[1:]
}
if len(num) != 10 {
return "", errors.New("Invalid Number")
}
return num, nil
}
// AreaCode takes a number, validates it, and returns the area code
func AreaCode(num string) (string, error) {
num, err := Number(num)
if err != nil {
return "", err
}
return num[:3], nil
}
// Format takes a number, validates it and returns it in the format
// (nnn) nnn-nnnn
func Format(num string) (string, error) {
num, err := Number(num)
if err != nil {
return "", err
}
return "(" + num[:3] + ") " + num[3:6] + "-" + num[6:], nil
}