Initial Commit
This commit is contained in:
38
go/leap/README.md
Normal file
38
go/leap/README.md
Normal file
@@ -0,0 +1,38 @@
|
||||
# Leap
|
||||
|
||||
Write a program that will take a year and report if it is a leap year.
|
||||
|
||||
The tricky thing here is that a leap year occurs:
|
||||
|
||||
```plain
|
||||
on every year that is evenly divisible by 4
|
||||
except every year that is evenly divisible by 100
|
||||
unless the year is also evenly divisible by 400
|
||||
```
|
||||
|
||||
For example, 1997 is not a leap year, but 1996 is. 1900 is not a leap
|
||||
year, but 2000 is.
|
||||
|
||||
If your language provides a method in the standard library that does
|
||||
this look-up, pretend it doesn't exist and implement it yourself.
|
||||
|
||||
## Notes
|
||||
|
||||
For a delightful, four minute explanation of the whole leap year
|
||||
phenomenon, go watch [this youtube video][video].
|
||||
|
||||
[video]: http://www.youtube.com/watch?v=xX96xng7sAE
|
||||
|
||||
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
|
||||
|
||||
JavaRanch Cattle Drive, exercise 3 [view source](http://www.javaranch.com/leap.jsp)
|
17
go/leap/cases_test.go
Normal file
17
go/leap/cases_test.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package leap
|
||||
|
||||
// Source: exercism/x-common
|
||||
// Commit: 945d08e Merge pull request #50 from soniakeys/master
|
||||
|
||||
var testCases = []struct {
|
||||
year int
|
||||
expected bool
|
||||
description string
|
||||
}{
|
||||
{1996, true, "leap year"},
|
||||
{1997, false, "non-leap year"},
|
||||
{1998, false, "non-leap even year"},
|
||||
{1900, false, "century"},
|
||||
{2400, true, "fourth century"},
|
||||
{2000, true, "Y2K"},
|
||||
}
|
15
go/leap/leap.go
Normal file
15
go/leap/leap.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package leap
|
||||
|
||||
// TestVersion is an exercism thing.
|
||||
const TestVersion = 1
|
||||
|
||||
// IsLeapYear returns whether the given year is a leap year.
|
||||
func IsLeapYear(year int) bool {
|
||||
if year%4 != 0 {
|
||||
return false
|
||||
}
|
||||
if year%400 == 0 {
|
||||
return true
|
||||
}
|
||||
return year%100 != 0
|
||||
}
|
34
go/leap/leap_test.go
Normal file
34
go/leap/leap_test.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package leap
|
||||
|
||||
import "testing"
|
||||
|
||||
// Define a function IsLeapYear(int) bool.
|
||||
//
|
||||
// Also define an exported TestVersion with a value that matches
|
||||
// the internal testVersion here.
|
||||
|
||||
const testVersion = 1
|
||||
|
||||
// Retired testVersions
|
||||
// (none) 4a9e144a3c5dc0d9773f4cf641ffe3efe48641d8
|
||||
|
||||
func TestLeapYears(t *testing.T) {
|
||||
if TestVersion != testVersion {
|
||||
t.Fatalf("Found TestVersion = %v, want %v", TestVersion, testVersion)
|
||||
}
|
||||
for _, test := range testCases {
|
||||
observed := IsLeapYear(test.year)
|
||||
if observed != test.expected {
|
||||
t.Fatalf("IsLeapYear(%d) = %t, want %t (%s)",
|
||||
test.year, observed, test.expected, test.description)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkLeapYears(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
for _, test := range testCases {
|
||||
IsLeapYear(test.year)
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user