Initial Commit

This commit is contained in:
2016-08-13 18:20:14 -05:00
commit 50f4a86fd8
408 changed files with 15301 additions and 0 deletions

23
go/clock/README.md Normal file
View File

@@ -0,0 +1,23 @@
# Clock
Implement a clock that handles times without dates.
Create a clock that is independent of date.
You should be able to add and subtract minutes to it.
Two clocks that represent the same time should be equal to each other.
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
Pairing session with Erin Drummond [view source](https://twitter.com/ebdrummond)

78
go/clock/cases_test.go Normal file
View File

@@ -0,0 +1,78 @@
package clock
// Source: exercism/x-common
// Commit: 269f498 Merge pull request #48 from soniakeys/custom-set-json
// Test creating a new clock with an initial time.
var timeTests = []struct {
h, m int
want string
}{
{8, 0, "08:00"}, // on the hour
{9, 0, "09:00"}, // on the hour
{11, 9, "11:09"}, // past the hour
{11, 19, "11:19"}, // past the hour
{24, 0, "00:00"}, // midnight is zero hours
{25, 0, "01:00"}, // hour rolls over
{1, 60, "02:00"}, // sixty minutes is next hour
{0, 160, "02:40"}, // minutes roll over
{25, 160, "03:40"}, // hour and minutes roll over
{-1, 15, "23:15"}, // negative hour
{-25, 0, "23:00"}, // negative hour rolls over
{1, -40, "00:20"}, // negative minutes
{1, -160, "22:20"}, // negative minutes roll over
{-25, -160, "20:20"}, // negative hour and minutes both roll over
}
// Test adding and subtracting minutes.
var addTests = []struct {
h, m, a int
want string
}{
{10, 0, 3, "10:03"}, // add minutes
{0, 45, 40, "01:25"}, // add to next hour
{10, 0, 61, "11:01"}, // add more than one hour
{23, 59, 2, "00:01"}, // add across midnight
{5, 32, 1500, "06:32"}, // add more than one day (1500 min = 25 hrs)
{0, 45, 160, "03:25"}, // add more than two hours with carry
{10, 3, -3, "10:00"}, // subtract minutes
{10, 3, -30, "09:33"}, // subtract to previous hour
{10, 3, -70, "08:53"}, // subtract more than an hour
{0, 3, -4, "23:59"}, // subtract across midnight
{0, 0, -160, "21:20"}, // subtract more than two hours
{5, 32, -1500, "04:32"}, // subtract more than one day (1500 min = 25 hrs)
{6, 15, -160, "03:35"}, // subtract more than two hours with borrow
}
// Construct two separate clocks, set times, test if they are equal.
type hm struct{ h, m int }
var eqTests = []struct {
c1, c2 hm
want bool
}{
// clocks with same time
{
hm{15, 37},
hm{15, 37},
true,
},
// clocks a minute apart
{
hm{15, 36},
hm{15, 37},
false,
},
// clocks an hour apart
{
hm{14, 37},
hm{15, 37},
false,
},
// clocks set 24 hours apart
{
hm{10, 37},
hm{34, 37},
true,
},
}

34
go/clock/clock.go Normal file
View File

@@ -0,0 +1,34 @@
package clock
import "fmt"
// TestVersion is for exercism
const TestVersion = 2
// Clock is an int representing the time of day
type Clock int
// Time returns a Clock representing the given hour & minute
func Time(hour, minute int) Clock {
return TimeFromMinutes(hour*60 + minute)
}
// String returns a string representation of Clock
func (c Clock) String() string {
return fmt.Sprintf("%02d:%02d", c/60, c%60)
}
// Add adds minutes to the clock
func (c Clock) Add(minutes int) Clock {
return TimeFromMinutes(int(c) + minutes)
}
// TimeFromMinutes takes a minutes integer value and returns a Clock
func TimeFromMinutes(minutes int) Clock {
for minutes < 0 {
// Keep adding 24 hours to the minutes value until it's positive
minutes = minutes + (24 * 60)
}
minutes = minutes % (24 * 60)
return Clock(minutes)
}

69
go/clock/clock_test.go Normal file
View File

@@ -0,0 +1,69 @@
package clock
import (
"reflect"
"testing"
)
// Clock type API:
//
// Time(hour, minute int) Clock // a "constructor"
// (Clock) String() string // a "stringer"
// (Clock) Add(minutes int) Clock
//
// Add should also handle subtraction by accepting negative values.
// To satisfy the readme requirement about clocks being equal, values of
// your Clock type need to work with the == operator.
//
// It might help to study the time.Time type in the standard library
// (https://golang.org/pkg/time/#Time) as a model. See how constructors there
// (Date and Now) return Time values rather than pointers. Note also how
// most time.Time methods have value receivers rather that pointer recievers.
// For more background on this read
// https://github.com/golang/go/wiki/CodeReviewComments#receiver-type.
const testVersion = 2
// Retired testVersions
// (none) 79937f6d58e25ebafe12d1cb4a9f88f4de70cfd6
// 1 8d0cb8b617be2e36b2ca5ad2034e5f80f2372924
func TestCreateClock(t *testing.T) {
if TestVersion != testVersion {
t.Fatalf("Found TestVersion = %v, want %v", TestVersion, testVersion)
}
for _, n := range timeTests {
if got := Time(n.h, n.m); got.String() != n.want {
t.Fatalf("Time(%d, %d) = %q, want %q", n.h, n.m, got, n.want)
}
}
t.Log(len(timeTests), "test cases")
}
func TestAddMinutes(t *testing.T) {
for _, a := range addTests {
if got := Time(a.h, a.m).Add(a.a); got.String() != a.want {
t.Fatalf("Time(%d, %d).Add(%d) = %q, want %q",
a.h, a.m, a.a, got, a.want)
}
}
t.Log(len(addTests), "test cases")
}
func TestCompareClocks(t *testing.T) {
for _, e := range eqTests {
clock1 := Time(e.c1.h, e.c1.m)
clock2 := Time(e.c2.h, e.c2.m)
got := clock1 == clock2
if got != e.want {
t.Log("Clock1:", clock1)
t.Log("Clock2:", clock2)
t.Logf("Clock1 == Clock2 is %t, want %t", got, e.want)
if reflect.DeepEqual(clock1, clock2) {
t.Log("(Hint: see comments in clock_test.go.)")
}
t.FailNow()
}
}
t.Log(len(eqTests), "test cases")
}