Skip to content

Commit 7a92f88

Browse files
committed
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.
1 parent e2289bb commit 7a92f88

File tree

6 files changed

+77
-0
lines changed

6 files changed

+77
-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/tests/grains.rs

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

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)