-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Luis Urraca
committed
May 22, 2019
1 parent
1868bab
commit 4cefd4b
Showing
5 changed files
with
176 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"track":"go","exercise":"grains","id":"d8647ac08b804c3694df91f5de19e23c","url":"https://exercism.io/my/solutions/d8647ac08b804c3694df91f5de19e23c","handle":"lurraca","is_requester":true,"auto_approve":false} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# Grains | ||
|
||
Calculate the number of grains of wheat on a chessboard given that the number | ||
on each square doubles. | ||
|
||
There once was a wise servant who saved the life of a prince. The king | ||
promised to pay whatever the servant could dream up. Knowing that the | ||
king loved chess, the servant told the king he would like to have grains | ||
of wheat. One grain on the first square of a chess board. Two grains on | ||
the next. Four on the third, and so on. | ||
|
||
There are 64 squares on a chessboard. | ||
|
||
Write code that shows: | ||
- how many grains were on each square, and | ||
- the total number of grains | ||
|
||
## For bonus points | ||
|
||
Did you get the tests passing and the code clean? If you want to, these | ||
are some additional things you could try: | ||
|
||
- Optimize for speed. | ||
- Optimize for readability. | ||
|
||
Then please share your thoughts in a comment on the submission. Did this | ||
experiment make the code better? Worse? Did you learn anything from it? | ||
|
||
## Running the tests | ||
|
||
To run the tests run the command `go test` from within the exercise directory. | ||
|
||
If the test suite contains benchmarks, you can run these with the `--bench` and `--benchmem` | ||
flags: | ||
|
||
go test -v --bench . --benchmem | ||
|
||
Keep in mind that each reviewer will run benchmarks on a different machine, with | ||
different specs, so the results from these benchmark tests may vary. | ||
|
||
## Further information | ||
|
||
For more detailed information about the Go track, including how to get help if | ||
you're having trouble, please visit the exercism.io [Go language page](http://exercism.io/languages/go/resources). | ||
|
||
## Source | ||
|
||
JavaRanch Cattle Drive, exercise 6 [http://www.javaranch.com/grains.jsp](http://www.javaranch.com/grains.jsp) | ||
|
||
## Submitting Incomplete Solutions | ||
It's possible to submit an incomplete solution so you can see how others have completed the exercise. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package grains | ||
|
||
// Source: exercism/problem-specifications | ||
// Commit: f079c2d grains: Move input (square) to input object (#1191) | ||
// Problem Specifications Version: 1.1.0 | ||
|
||
// returns the number of grains on the square | ||
var squareTests = []struct { | ||
description string | ||
input int | ||
expectedVal uint64 | ||
expectError bool | ||
}{ | ||
{ | ||
description: "1", | ||
input: 1, | ||
expectedVal: 1, | ||
}, | ||
{ | ||
description: "2", | ||
input: 2, | ||
expectedVal: 2, | ||
}, | ||
{ | ||
description: "3", | ||
input: 3, | ||
expectedVal: 4, | ||
}, | ||
{ | ||
description: "4", | ||
input: 4, | ||
expectedVal: 8, | ||
}, | ||
{ | ||
description: "16", | ||
input: 16, | ||
expectedVal: 32768, | ||
}, | ||
{ | ||
description: "32", | ||
input: 32, | ||
expectedVal: 2147483648, | ||
}, | ||
{ | ||
description: "64", | ||
input: 64, | ||
expectedVal: 9223372036854775808, | ||
}, | ||
{ | ||
description: "square 0 returns an error", | ||
input: 0, | ||
expectError: true, | ||
}, | ||
{ | ||
description: "negative square returns an error", | ||
input: -1, | ||
expectError: true, | ||
}, | ||
{ | ||
description: "square greater than 64 returns an error", | ||
input: 65, | ||
expectError: true, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package grains | ||
|
||
func Square(n int) (uint64, error) { | ||
return 0, nil | ||
} | ||
|
||
func Total() uint64 { | ||
return 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package grains | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestSquare(t *testing.T) { | ||
for _, test := range squareTests { | ||
actualVal, actualErr := Square(test.input) | ||
|
||
// check actualVal only if no error expected | ||
if !test.expectError && actualVal != test.expectedVal { | ||
t.Fatalf("FAIL: %s\nSquare(%d) expected %d, Actual %d", test.description, test.input, test.expectedVal, actualVal) | ||
} | ||
|
||
// if we expect an error and there isn't one | ||
if test.expectError && actualErr == nil { | ||
t.Fatalf("FAIL: %s\nSquare(%d) expected an error, but error is nil", test.description, test.input) | ||
} | ||
// if we don't expect an error and there is one | ||
if !test.expectError && actualErr != nil { | ||
var _ error = actualErr | ||
t.Fatalf("FAIL: %s\nSquare(%d) expected no error, but error is: %s", test.description, test.input, actualErr) | ||
} | ||
t.Logf("PASS: %s", test.description) | ||
} | ||
} | ||
|
||
func TestTotal(t *testing.T) { | ||
var expected uint64 = 18446744073709551615 | ||
if actual := Total(); actual != expected { | ||
t.Errorf("Total() expected %d, Actual %d", expected, actual) | ||
} | ||
} | ||
|
||
func BenchmarkSquare(b *testing.B) { | ||
|
||
for i := 0; i < b.N; i++ { | ||
|
||
for _, test := range squareTests { | ||
Square(test.input) | ||
} | ||
|
||
} | ||
} | ||
|
||
func BenchmarkTotal(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
Total() | ||
} | ||
} |