Skip to content

Commit 2096353

Browse files
authored
Implement grains (#219)
* Implement grains Follows the standard test suite, which is currently waiting for merge. exercism/problem-specifications#420 I did not include the test of square -1 since that would require the function signature to be `i32`. Placement is at the end of the "Introduction" section. It introduces a couple of concepts that may be new, - declaring number types - panic & should_panic I thought it would be nice to get some exposure to `panic` before introducing `result` in the Hamming exercise. Re: stub src/lib.rs file This is pretty early in the track, so signatures can be handy. And not getting a bunch of compilation errors is also nice. I don't think that this stub forces any particular implementation, though the setting of type of `s` to `u32` does help the students over a bit of type-casting hassle.
1 parent e2289bb commit 2096353

File tree

7 files changed

+93
-0
lines changed

7 files changed

+93
-0
lines changed

config.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"bob",
1212
"beer-song",
1313
"difference-of-squares",
14+
"grains",
1415
"hamming",
1516
"pascals-triangle",
1617
"scrabble-score",

exercises/grains/.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Generated by Cargo
2+
# will have compiled files and executables
3+
/target/
4+
5+
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
6+
# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock
7+
Cargo.lock

exercises/grains/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[package]
2+
name = "grains"
3+
version = "0.0.0"

exercises/grains/example.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
pub fn square(s: u32) -> u64 {
2+
if s == 0 || s > 64 {
3+
panic!("Square must be between 1 and 64");
4+
}
5+
6+
2u64.pow(s - 1)
7+
}
8+
9+
pub fn total() -> u64 {
10+
(1..65).fold(0, |acc, s| acc + square(s))
11+
}

exercises/grains/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
pub fn square(s: u32) -> u64 {
2+
unimplemented!();
3+
}
4+
5+
pub fn total() -> u64 {
6+
unimplemented!();
7+
}

exercises/grains/tests/grains.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
extern crate grains;
2+
3+
#[test]
4+
fn square_one() {
5+
assert_eq!(grains::square(1), 1);
6+
}
7+
8+
#[test]
9+
#[ignore]
10+
fn square_two() {
11+
assert_eq!(grains::square(2), 2);
12+
}
13+
14+
#[test]
15+
#[ignore]
16+
fn square_three() {
17+
assert_eq!(grains::square(3), 4);
18+
}
19+
20+
#[test]
21+
#[ignore]
22+
fn square_four() {
23+
assert_eq!(grains::square(4), 8);
24+
}
25+
26+
#[test]
27+
#[ignore]
28+
fn square_sixteen() {
29+
assert_eq!(grains::square(16), 32_768);
30+
}
31+
32+
#[test]
33+
#[ignore]
34+
fn square_thirty_two() {
35+
assert_eq!(grains::square(32), 2_147_483_648);
36+
}
37+
38+
#[test]
39+
#[ignore]
40+
fn square_sixty_four() {
41+
assert_eq!(grains::square(64), 9_223_372_036_854_775_808);
42+
}
43+
44+
#[test]
45+
#[ignore]
46+
#[should_panic(expected = "Square must be between 1 and 64")]
47+
fn square_zero_panics() {
48+
grains::square(0);
49+
}
50+
51+
#[test]
52+
#[ignore]
53+
#[should_panic(expected = "Square must be between 1 and 64")]
54+
fn square_sixty_five_panics() {
55+
grains::square(65);
56+
}
57+
58+
#[test]
59+
#[ignore]
60+
fn total_sums_all_squares() {
61+
assert_eq!(grains::total(), 18_446_744_073_709_551_615);
62+
grains::total();
63+
}

problems.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ raindrops | case (or `format`). Mutable string
2323
bob | chars, string functions
2424
beer-song | case, string concatenation, vector (optional), loop
2525
difference-of-squares | fold & map
26+
grains | math, panic
2627

2728
## Getting Rusty
2829

0 commit comments

Comments
 (0)