Initial Commit
This commit is contained in:
31
go/series/README.md
Normal file
31
go/series/README.md
Normal file
@@ -0,0 +1,31 @@
|
||||
# Series
|
||||
|
||||
Write a program that will take a string of digits and give you all the possible consecutive number series of length `n` in that string.
|
||||
|
||||
For example, the string "01234" has the following 3-digit series:
|
||||
|
||||
- 012
|
||||
- 123
|
||||
- 234
|
||||
|
||||
And the following 4-digit series:
|
||||
|
||||
- 0123
|
||||
- 1234
|
||||
|
||||
And if you ask for a 6-digit series from a 5-digit string, you deserve
|
||||
whatever you get.
|
||||
|
||||
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://help.exercism.io/getting-started-with-go.html).
|
||||
|
||||
## Source
|
||||
|
||||
A subset of the Problem 8 at Project Euler [view source](http://projecteuler.net/problem=8)
|
29
go/series/asktoomuch_test.go
Normal file
29
go/series/asktoomuch_test.go
Normal file
@@ -0,0 +1,29 @@
|
||||
// +build asktoomuch
|
||||
|
||||
package slice
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestAskTooMuch(t *testing.T) {
|
||||
test := allTests[0]
|
||||
defer func() {
|
||||
if recover() != nil {
|
||||
t.Fatalf("Yikes, Frist(%d, %s) panicked!", test.n, test.s)
|
||||
}
|
||||
}()
|
||||
for _, test = range allTests {
|
||||
switch res := Frist(test.n, test.s); {
|
||||
case len(test.out) > 0: // well, this should work
|
||||
if res != test.out[0] {
|
||||
t.Fatalf("Yikes, Frist(%d, %s) = %q, want %q.",
|
||||
test.n, test.s, res, test.out[0])
|
||||
}
|
||||
case len(res) != test.n:
|
||||
t.Fatalf("Yikes, Frist(%d, %s) = %q, but %q doesn't have %d characters.",
|
||||
test.n, test.s, res, res, test.n)
|
||||
default:
|
||||
t.Fatalf("Yikes, Frist(%d, %s) = %q, but %q isn't in %q",
|
||||
test.n, test.s, res, res, test.s)
|
||||
}
|
||||
}
|
||||
}
|
23
go/series/first_test.go
Normal file
23
go/series/first_test.go
Normal file
@@ -0,0 +1,23 @@
|
||||
// +build first
|
||||
|
||||
package slice
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestFirst(t *testing.T) {
|
||||
for _, test := range allTests {
|
||||
switch res, ok := First(test.n, test.s); {
|
||||
case !ok:
|
||||
if len(test.out) > 0 {
|
||||
t.Fatalf("First(%d, %s) returned !ok, want ok.",
|
||||
test.n, test.s)
|
||||
}
|
||||
case len(test.out) == 0:
|
||||
t.Fatalf("First(%d, %s) = %s, %t. Expected ok == false",
|
||||
test.n, test.s, res, ok)
|
||||
case res != test.out[0]:
|
||||
t.Fatalf("First(%d, %s) = %s. Want %s.",
|
||||
test.n, test.s, res, test.out[0])
|
||||
}
|
||||
}
|
||||
}
|
BIN
go/series/main/main
Executable file
BIN
go/series/main/main
Executable file
Binary file not shown.
52
go/series/main/main.go
Normal file
52
go/series/main/main.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// All ...
|
||||
func All(l int, str string) []string {
|
||||
var ret []string
|
||||
for len(str) >= l {
|
||||
ret = append(ret, str[:l])
|
||||
str = str[1:]
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
func main() {
|
||||
var allTests = []struct {
|
||||
n int
|
||||
s string
|
||||
out []string
|
||||
}{
|
||||
{1, "01234",
|
||||
[]string{"0", "1", "2", "3", "4"}},
|
||||
{1, "92834",
|
||||
[]string{"9", "2", "8", "3", "4"}},
|
||||
{2, "01234",
|
||||
[]string{"01", "12", "23", "34"}},
|
||||
{2, "98273463",
|
||||
[]string{"98", "82", "27", "73", "34", "46", "63"}},
|
||||
{2, "37103",
|
||||
[]string{"37", "71", "10", "03"}},
|
||||
{3, "01234",
|
||||
[]string{"012", "123", "234"}},
|
||||
{3, "31001",
|
||||
[]string{"310", "100", "001"}},
|
||||
{3, "982347",
|
||||
[]string{"982", "823", "234", "347"}},
|
||||
{4, "01234",
|
||||
[]string{"0123", "1234"}},
|
||||
{4, "91274",
|
||||
[]string{"9127", "1274"}},
|
||||
{5, "01234",
|
||||
[]string{"01234"}},
|
||||
{5, "81228",
|
||||
[]string{"81228"}},
|
||||
{6, "01234", nil},
|
||||
}
|
||||
for _, test := range allTests {
|
||||
fmt.Println(All(test.n, test.s))
|
||||
}
|
||||
}
|
86
go/series/series_test.go
Normal file
86
go/series/series_test.go
Normal file
@@ -0,0 +1,86 @@
|
||||
// Define two functions: (Two? Yes, sometimes we ask more out of Go.)
|
||||
//
|
||||
// // All returns a list of all substrings of s with length n.
|
||||
// All(n int, s string) []string
|
||||
//
|
||||
// // Frist returns the first substring of s with length n.
|
||||
// Frist(n int, s string) string
|
||||
//
|
||||
// Wait, is that a typo? It'll make sense if you do the bonus!
|
||||
//
|
||||
// Bonus exercise:
|
||||
//
|
||||
// Maybe we typed too fast. Once you get `go test` passing, try
|
||||
// `go test -tags asktoomuch`. (Hint, you can't make it happy.)
|
||||
//
|
||||
// Now slow down and do things right(tm). Define
|
||||
//
|
||||
// First(int, string) (first string, ok bool)
|
||||
//
|
||||
// spelling first correctly this time, and test with `go test -tags first`.
|
||||
|
||||
package slice
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var allTests = []struct {
|
||||
n int
|
||||
s string
|
||||
out []string
|
||||
}{
|
||||
{1, "01234",
|
||||
[]string{"0", "1", "2", "3", "4"}},
|
||||
{1, "92834",
|
||||
[]string{"9", "2", "8", "3", "4"}},
|
||||
{2, "01234",
|
||||
[]string{"01", "12", "23", "34"}},
|
||||
{2, "98273463",
|
||||
[]string{"98", "82", "27", "73", "34", "46", "63"}},
|
||||
{2, "37103",
|
||||
[]string{"37", "71", "10", "03"}},
|
||||
{3, "01234",
|
||||
[]string{"012", "123", "234"}},
|
||||
{3, "31001",
|
||||
[]string{"310", "100", "001"}},
|
||||
{3, "982347",
|
||||
[]string{"982", "823", "234", "347"}},
|
||||
{4, "01234",
|
||||
[]string{"0123", "1234"}},
|
||||
{4, "91274",
|
||||
[]string{"9127", "1274"}},
|
||||
{5, "01234",
|
||||
[]string{"01234"}},
|
||||
{5, "81228",
|
||||
[]string{"81228"}},
|
||||
{6, "01234", nil},
|
||||
{len(cx) + 1, cx, nil},
|
||||
}
|
||||
|
||||
var cx = "01032987583"
|
||||
|
||||
func TestAll(t *testing.T) {
|
||||
for _, test := range allTests {
|
||||
switch res := All(test.n, test.s); {
|
||||
case len(res) == 0 && len(test.out) == 0:
|
||||
case reflect.DeepEqual(res, test.out):
|
||||
default:
|
||||
t.Fatalf("All(%d, %s) = %v, want %v.",
|
||||
test.n, test.s, res, test.out)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFrist(t *testing.T) {
|
||||
for _, test := range allTests {
|
||||
if len(test.out) == 0 {
|
||||
continue
|
||||
}
|
||||
if res := Frist(test.n, test.s); res != test.out[0] {
|
||||
t.Fatalf("Frist(%d, %s) = %s, want %s.",
|
||||
test.n, test.s, res, test.out[0])
|
||||
}
|
||||
}
|
||||
}
|
21
go/series/slice.go
Normal file
21
go/series/slice.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package slice
|
||||
|
||||
// All takes a string 'str' and a length 'l' and returns
|
||||
// all substrings of length l
|
||||
func All(l int, str string) []string {
|
||||
var ret []string
|
||||
for len(str) >= l {
|
||||
ret = append(ret, str[:l])
|
||||
str = str[1:]
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
// Frist (Frist?!) takes a string 'str' and a length 'l' and returns
|
||||
// the first substring of length l
|
||||
func Frist(l int, str string) string {
|
||||
if len(str) >= l {
|
||||
return str[:l]
|
||||
}
|
||||
return ""
|
||||
}
|
Reference in New Issue
Block a user