-
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add grains exercise * Format stub * Add pending status * Update exercises/practice/grains/grains.coffee Co-authored-by: Erik Schierboom <erik_schierboom@hotmail.com> --------- Co-authored-by: Erik Schierboom <erik_schierboom@hotmail.com>
- Loading branch information
1 parent
848f16c
commit 1905ca9
Showing
7 changed files
with
159 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
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,15 @@ | ||
# Instructions | ||
|
||
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, with the number of grains doubling on each successive square. | ||
|
||
There are 64 squares on a chessboard (where square 1 has one grain, square 2 has two grains, and so on). | ||
|
||
Write code that shows: | ||
|
||
- how many grains were on a given square, and | ||
- the total number of grains on the chessboard |
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,19 @@ | ||
{ | ||
"authors": [ | ||
"BNAndras" | ||
], | ||
"files": { | ||
"solution": [ | ||
"grains.coffee" | ||
], | ||
"test": [ | ||
"grains.spec.coffee" | ||
], | ||
"example": [ | ||
".meta/example.coffee" | ||
] | ||
}, | ||
"blurb": "Calculate the number of grains of wheat on a chessboard given that the number on each square doubles.", | ||
"source": "The CodeRanch Cattle Drive, Assignment 6", | ||
"source_url": "https://coderanch.com/wiki/718824/Grains" | ||
} |
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,10 @@ | ||
class Grains | ||
square: (number) -> | ||
if number not in [1..64] | ||
throw 'square must be between 1 and 64' | ||
|
||
Math.pow(2, number - 1) | ||
|
||
total: () -> | ||
Math.pow(2, 64) - 1 | ||
module.exports = Grains |
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,43 @@ | ||
# This is an auto-generated file. | ||
# | ||
# Regenerating this file via `configlet sync` will: | ||
# - Recreate every `description` key/value pair | ||
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications | ||
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) | ||
# - Preserve any other key/value pair | ||
# | ||
# As user-added comments (using the # character) will be removed when this file | ||
# is regenerated, comments can be added via a `comment` key. | ||
|
||
[9fbde8de-36b2-49de-baf2-cd42d6f28405] | ||
description = "returns the number of grains on the square -> grains on square 1" | ||
|
||
[ee1f30c2-01d8-4298-b25d-c677331b5e6d] | ||
description = "returns the number of grains on the square -> grains on square 2" | ||
|
||
[10f45584-2fc3-4875-8ec6-666065d1163b] | ||
description = "returns the number of grains on the square -> grains on square 3" | ||
|
||
[a7cbe01b-36f4-4601-b053-c5f6ae055170] | ||
description = "returns the number of grains on the square -> grains on square 4" | ||
|
||
[c50acc89-8535-44e4-918f-b848ad2817d4] | ||
description = "returns the number of grains on the square -> grains on square 16" | ||
|
||
[acd81b46-c2ad-4951-b848-80d15ed5a04f] | ||
description = "returns the number of grains on the square -> grains on square 32" | ||
|
||
[c73b470a-5efb-4d53-9ac6-c5f6487f227b] | ||
description = "returns the number of grains on the square -> grains on square 64" | ||
|
||
[1d47d832-3e85-4974-9466-5bd35af484e3] | ||
description = "returns the number of grains on the square -> square 0 is invalid" | ||
|
||
[61974483-eeb2-465e-be54-ca5dde366453] | ||
description = "returns the number of grains on the square -> negative square is invalid" | ||
|
||
[a95e4374-f32c-45a7-a10d-ffec475c012f] | ||
description = "returns the number of grains on the square -> square greater than 64 is invalid" | ||
|
||
[6eb07385-3659-4b45-a6be-9dc474222750] | ||
description = "returns the total number of grains on the board" |
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,6 @@ | ||
class Grains | ||
@square: (number) -> | ||
|
||
@total: () -> | ||
|
||
module.exports = Grains |
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,58 @@ | ||
Grains = require './grains' | ||
|
||
describe 'Grains', -> | ||
grains = new Grains() | ||
it 'grains on square 1', -> | ||
results = grains.square(1) | ||
expected = 1 | ||
expect(results).toEqual expected | ||
|
||
xit 'grains on square 2', -> | ||
results = grains.square(2) | ||
expected = 2 | ||
expect(results).toEqual expected | ||
|
||
xit 'grains on square 3', -> | ||
results = grains.square(3) | ||
expected = 4 | ||
expect(results).toEqual expected | ||
|
||
xit 'grains on square 4', -> | ||
results = grains.square(4) | ||
expected = 8 | ||
expect(results).toEqual expected | ||
|
||
xit 'grains on square 16', -> | ||
results = grains.square(16) | ||
expected = 32768 | ||
expect(results).toEqual expected | ||
|
||
xit 'grains on square 32', -> | ||
results = grains.square(32) | ||
expected = 2147483648 | ||
expect(results).toEqual expected | ||
|
||
xit 'grains on square 64', -> | ||
results = grains.square(64) | ||
expected = 9223372036854775808 | ||
expect(results).toEqual expected | ||
|
||
xit 'square 0 is invalid', -> | ||
expect -> | ||
grains.square(0) | ||
.toThrow new Error 'square must be between 1 and 64' | ||
|
||
xit 'negative square is invalid', -> | ||
expect -> | ||
grains.square(-1) | ||
.toThrow new Error 'square must be between 1 and 64' | ||
|
||
xit 'square greater than 64 is invalid', -> | ||
expect -> | ||
grains.square(65) | ||
.toThrow new Error 'square must be between 1 and 64' | ||
|
||
xit 'returns the total number of grains on the board', -> | ||
results = grains.total(65) | ||
expected = 18446744073709551615 | ||
expect(results).toEqual expected |