Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
26
Task/Middle-three-digits/Go/middle-three-digits-1.go
Normal file
26
Task/Middle-three-digits/Go/middle-three-digits-1.go
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
package m3
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrorLT3 = errors.New("N of at least three digits required.")
|
||||
ErrorEven = errors.New("N with odd number of digits required.")
|
||||
)
|
||||
|
||||
func Digits(i int) (string, error) {
|
||||
if i < 0 {
|
||||
i = -i
|
||||
}
|
||||
if i < 100 {
|
||||
return "", ErrorLT3
|
||||
}
|
||||
s := strconv.Itoa(i)
|
||||
if len(s)%2 == 0 {
|
||||
return "", ErrorEven
|
||||
}
|
||||
m := len(s) / 2
|
||||
return s[m-1 : m+2], nil
|
||||
}
|
||||
59
Task/Middle-three-digits/Go/middle-three-digits-2.go
Normal file
59
Task/Middle-three-digits/Go/middle-three-digits-2.go
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
package m3_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"m3"
|
||||
)
|
||||
|
||||
func TestPassing(t *testing.T) {
|
||||
type s struct {
|
||||
i int
|
||||
m string
|
||||
}
|
||||
tcs := []s{
|
||||
{123, "123"},
|
||||
{12345, "234"},
|
||||
{1234567, "345"},
|
||||
{987654321, "654"},
|
||||
{10001, "000"},
|
||||
{-10001, "000"},
|
||||
}
|
||||
for _, tc := range tcs {
|
||||
m, err := m3.Digits(tc.i)
|
||||
if err != nil {
|
||||
t.Fatalf("d(%d) returned %q.", tc.i, err)
|
||||
}
|
||||
if m != tc.m {
|
||||
t.Fatalf("d(%d) expected %q, got %q.", tc.i, tc.m, m)
|
||||
}
|
||||
t.Logf("d(%d) = %q.", tc.i, m)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFailing(t *testing.T) {
|
||||
type s struct {
|
||||
i int
|
||||
err error
|
||||
}
|
||||
tcs := []s{
|
||||
{1, m3.ErrorLT3},
|
||||
{2, m3.ErrorLT3},
|
||||
{-1, m3.ErrorLT3},
|
||||
{-10, m3.ErrorLT3},
|
||||
{2002, m3.ErrorEven},
|
||||
{-2002, m3.ErrorEven},
|
||||
{0, m3.ErrorLT3},
|
||||
}
|
||||
for _, tc := range tcs {
|
||||
m, err := m3.Digits(tc.i)
|
||||
if err == nil {
|
||||
t.Fatal("d(%d) expected error %q, got non-error %q.",
|
||||
tc.i, tc.err, m)
|
||||
}
|
||||
if err != tc.err {
|
||||
t.Fatal("d(d) expected error %q, got %q", tc.i, tc.err, err)
|
||||
}
|
||||
t.Logf("d(%d) returns %q", tc.i, err)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue