Skip to content

Commit a45894e

Browse files
authored
Merge pull request #451 from coriolinus/book-store-no-floats
book-store: use integer number of cents, not floating-point numbers
2 parents 80cc2a5 + 627d009 commit a45894e

File tree

5 files changed

+28
-33
lines changed

5 files changed

+28
-33
lines changed
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[package]
22
name = "book_store"
3-
version = "1.0.1"
3+
version = "1.3.0"
44
authors = ["Peter Goodspeed-Niklaus <[email protected]>"]
55

66
[dependencies]
7-
noisy_float = "0.1.2"

exercises/book-store/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "book_store"
3-
version = "1.1.0"
3+
version = "1.3.0"
44
authors = ["Peter Goodspeed-Niklaus <[email protected]>"]
55

66
[dependencies]

exercises/book-store/example.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,10 @@ use std::hash::{Hash, Hasher};
55
use std::mem;
66
use std::cell::RefCell;
77

8-
extern crate noisy_float;
9-
use noisy_float::prelude::*;
10-
118
type Book = usize;
129
type GroupedBasket = Vec<Group>;
13-
type Price = f64;
14-
const BOOK_PRICE: Price = 8.0;
10+
type Price = usize;
11+
const BOOK_PRICE: Price = 800;
1512

1613
#[derive(Debug, Clone, PartialEq, Eq)]
1714
struct Group(RefCell<BTreeSet<Book>>);
@@ -30,12 +27,12 @@ impl Group {
3027
fn price(&self) -> Price {
3128
(self.0.borrow().len() as Price) * BOOK_PRICE *
3229
match self.0.borrow().len() {
33-
2 => 0.95,
34-
3 => 0.90,
35-
4 => 0.80,
36-
5 => 0.75,
37-
_ => 1.0,
38-
}
30+
2 => 95,
31+
3 => 90,
32+
4 => 80,
33+
5 => 75,
34+
_ => 100,
35+
} / 100
3936
}
4037
}
4138

@@ -96,10 +93,9 @@ fn hash_of(basket: &GroupedBasket) -> u64 {
9693

9794
pub fn lowest_price(books: &[Book]) -> Price {
9895
DecomposeGroups::new(books)
99-
.map(|gb| r64(basket_price(&gb)))
96+
.map(|gb| basket_price(&gb))
10097
.min()
101-
.map(|r| r.raw())
102-
.unwrap_or(0.0)
98+
.unwrap_or(0)
10399
}
104100

105101
struct DecomposeGroups {

exercises/book-store/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
pub fn lowest_price(_: &[usize]) -> f64 {
1+
pub fn lowest_price(_: &[usize]) -> usize {
22
unimplemented!()
33
}

exercises/book-store/tests/book-store.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use book_store::*;
1414
/// in terms of this function.
1515
///
1616
/// Expected input format: ('basket', 'targetgrouping')
17-
fn process_total_case(input: (Vec<usize>, Vec<Vec<usize>>), expected: f64) {
17+
fn process_total_case(input: (Vec<usize>, Vec<Vec<usize>>), expected: usize) {
1818
assert_eq!(
1919
lowest_price(&input.0),
2020
expected
@@ -30,108 +30,108 @@ fn process_total_case(input: (Vec<usize>, Vec<Vec<usize>>), expected: f64) {
3030
#[test]
3131
/// Only a single book
3232
fn test_only_a_single_book() {
33-
process_total_case((vec![1], vec![vec![1]]), 8.0);
33+
process_total_case((vec![1], vec![vec![1]]), 800);
3434
}
3535

3636

3737
#[test]
3838
#[ignore]
3939
/// Two of the same book
4040
fn test_two_of_the_same_book() {
41-
process_total_case((vec![2, 2], vec![vec![2], vec![2]]), 16.0);
41+
process_total_case((vec![2, 2], vec![vec![2], vec![2]]), 1_600);
4242
}
4343

4444

4545
#[test]
4646
#[ignore]
4747
/// Empty basket
4848
fn test_empty_basket() {
49-
process_total_case((vec![], vec![]), 0.0);
49+
process_total_case((vec![], vec![]), 0);
5050
}
5151

5252

5353
#[test]
5454
#[ignore]
5555
/// Two different books
5656
fn test_two_different_books() {
57-
process_total_case((vec![1, 2], vec![vec![1, 2]]), 15.2);
57+
process_total_case((vec![1, 2], vec![vec![1, 2]]), 1_520);
5858
}
5959

6060

6161
#[test]
6262
#[ignore]
6363
/// Three different books
6464
fn test_three_different_books() {
65-
process_total_case((vec![1, 2, 3], vec![vec![1, 2, 3]]), 21.6);
65+
process_total_case((vec![1, 2, 3], vec![vec![1, 2, 3]]), 2_160);
6666
}
6767

6868

6969
#[test]
7070
#[ignore]
7171
/// Four different books
7272
fn test_four_different_books() {
73-
process_total_case((vec![1, 2, 3, 4], vec![vec![1, 2, 3, 4]]), 25.6);
73+
process_total_case((vec![1, 2, 3, 4], vec![vec![1, 2, 3, 4]]), 2_560);
7474
}
7575

7676

7777
#[test]
7878
#[ignore]
7979
/// Five different books
8080
fn test_five_different_books() {
81-
process_total_case((vec![1, 2, 3, 4, 5], vec![vec![1, 2, 3, 4, 5]]), 30.0);
81+
process_total_case((vec![1, 2, 3, 4, 5], vec![vec![1, 2, 3, 4, 5]]), 3_000);
8282
}
8383

8484

8585
#[test]
8686
#[ignore]
8787
/// Two groups of four is cheaper than group of five plus group of three
8888
fn test_two_groups_of_four_is_cheaper_than_group_of_five_plus_group_of_three() {
89-
process_total_case((vec![1, 1, 2, 2, 3, 3, 4, 5], vec![vec![1, 2, 3, 4], vec![1, 2, 3, 5]]), 51.2);
89+
process_total_case((vec![1, 1, 2, 2, 3, 3, 4, 5], vec![vec![1, 2, 3, 4], vec![1, 2, 3, 5]]), 5_120);
9090
}
9191

9292

9393
#[test]
9494
#[ignore]
9595
/// Group of four plus group of two is cheaper than two groups of three
9696
fn test_group_of_four_plus_group_of_two_is_cheaper_than_two_groups_of_three() {
97-
process_total_case((vec![1, 1, 2, 2, 3, 4], vec![vec![1, 2, 3, 4], vec![1, 2]]), 40.8);
97+
process_total_case((vec![1, 1, 2, 2, 3, 4], vec![vec![1, 2, 3, 4], vec![1, 2]]), 4_080);
9898
}
9999

100100

101101
#[test]
102102
#[ignore]
103103
/// Two each of first 4 books and 1 copy each of rest
104104
fn test_two_each_of_first_4_books_and_1_copy_each_of_rest() {
105-
process_total_case((vec![1, 1, 2, 2, 3, 3, 4, 4, 5], vec![vec![1, 2, 3, 4, 5], vec![1, 2, 3, 4]]), 55.6);
105+
process_total_case((vec![1, 1, 2, 2, 3, 3, 4, 4, 5], vec![vec![1, 2, 3, 4, 5], vec![1, 2, 3, 4]]), 5_560);
106106
}
107107

108108

109109
#[test]
110110
#[ignore]
111111
/// Two copies of each book
112112
fn test_two_copies_of_each_book() {
113-
process_total_case((vec![1, 1, 2, 2, 3, 3, 4, 4, 5, 5], vec![vec![1, 2, 3, 4, 5], vec![1, 2, 3, 4, 5]]), 60.0);
113+
process_total_case((vec![1, 1, 2, 2, 3, 3, 4, 4, 5, 5], vec![vec![1, 2, 3, 4, 5], vec![1, 2, 3, 4, 5]]), 6_000);
114114
}
115115

116116

117117
#[test]
118118
#[ignore]
119119
/// Three copies of first book and 2 each of remaining
120120
fn test_three_copies_of_first_book_and_2_each_of_remaining() {
121-
process_total_case((vec![1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 1], vec![vec![1, 2, 3, 4, 5], vec![1, 2, 3, 4, 5], vec![1]]), 68.0);
121+
process_total_case((vec![1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 1], vec![vec![1, 2, 3, 4, 5], vec![1, 2, 3, 4, 5], vec![1]]), 6_800);
122122
}
123123

124124

125125
#[test]
126126
#[ignore]
127127
/// Three each of first 2 books and 2 each of remaining books
128128
fn test_three_each_of_first_2_books_and_2_each_of_remaining_books() {
129-
process_total_case((vec![1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 1, 2], vec![vec![1, 2, 3, 4, 5], vec![1, 2, 3, 4, 5], vec![1, 2]]), 75.2);
129+
process_total_case((vec![1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 1, 2], vec![vec![1, 2, 3, 4, 5], vec![1, 2, 3, 4, 5], vec![1, 2]]), 7_520);
130130
}
131131

132132
#[test]
133133
#[ignore]
134134
/// Four groups of four are cheaper than two groups each of five and three
135135
fn test_four_groups_of_four_are_cheaper_than_two_groups_each_of_five_and_three() {
136-
process_total_case((vec![1,1,2,2,3,3,4,5,1,1,2,2,3,3,4,5], vec![vec![1,2,3,4],vec![1,2,3,5],vec![1,2,3,4],vec![1,2,3,5]]), 102.4);
136+
process_total_case((vec![1,1,2,2,3,3,4,5,1,1,2,2,3,3,4,5], vec![vec![1,2,3,4],vec![1,2,3,5],vec![1,2,3,4],vec![1,2,3,5]]), 10_240);
137137
}

0 commit comments

Comments
 (0)