Skip to content

Commit 8282990

Browse files
committed
work on examples
1 parent 7b4528f commit 8282990

File tree

4 files changed

+106
-17
lines changed

4 files changed

+106
-17
lines changed

src/example0.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
use std::collections::HashMap;
22
use std::f32::INFINITY;
33

4-
struct ShoppingList {
5-
items: Vec<String>,
6-
}
7-
84
struct Store {
95
name: String,
106
prices: HashMap<String, f32>,
@@ -51,7 +47,8 @@ fn build_stores() -> Vec<Store> {
5147
stores
5248
}
5349

54-
fn find_best_store<'a>(stores: &'a [Store], shopping_list: &ShoppingList) -> &'a Store {
50+
fn find_best_store(stores: Vec<Store>, shopping_list: &Vec<String>) -> String {
51+
// EXERCISE 0
5552
assert!(stores.len() > 0);
5653
let mut best = None;
5754
let mut best_price = INFINITY;
@@ -60,22 +57,19 @@ fn find_best_store<'a>(stores: &'a [Store], shopping_list: &ShoppingList) -> &'a
6057
.map(|item_name| store.price(item_name))
6158
.fold(0.0, |v, u| v + u);
6259
if sum < best_price {
63-
best = Some(store);
60+
best = Some(store.name);
6461
best_price = sum;
6562
}
6663
}
6764
best.unwrap() // there will always be at least one store
6865
}
6966

70-
fn main() {
71-
let shopping_list = ShoppingList {
72-
items: vec![format!("chocolate"),
73-
format!("doll"),
74-
format!("bike")]
75-
};
76-
67+
pub fn main() {
68+
let shopping_list = vec![format!("chocolate"),
69+
format!("doll"),
70+
format!("bike")];
7771
let stores = build_stores();
78-
let store = find_best_store(&stores, &shopping_list);
79-
println!("Best store: {}", store.name);
72+
let best_store = find_best_store(stores, &shopping_list);
73+
println!("Best store: {}", best_store);
8074
}
8175

src/example1.rs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
use std::collections::HashMap;
2+
use std::f32::INFINITY;
3+
4+
struct ShoppingList {
5+
items: Vec<String>,
6+
}
7+
8+
struct Store {
9+
name: String,
10+
prices: HashMap<String, f32>,
11+
}
12+
13+
impl Store {
14+
fn new(name: String) -> Store {
15+
Store {
16+
name: name,
17+
prices: HashMap::new(),
18+
}
19+
}
20+
21+
fn add_item(&mut self, name: String, price: f32) {
22+
self.prices.insert(name, price);
23+
}
24+
25+
fn price(&self, item_name: &str) -> f32 {
26+
self.prices[item_name]
27+
}
28+
}
29+
30+
fn build_stores() -> Vec<Store> {
31+
let mut stores = vec![];
32+
33+
let mut store = Store::new(format!("R-mart"));
34+
store.add_item(format!("chocolate"), 5.0);
35+
store.add_item(format!("doll"), 22.0);
36+
store.add_item(format!("bike"), 150.0);
37+
stores.push(store);
38+
39+
let mut store = Store::new(format!("Bullseye"));
40+
store.add_item(format!("chocolate"), 2.0);
41+
store.add_item(format!("doll"), 23.0);
42+
store.add_item(format!("bike"), 145.0);
43+
stores.push(store);
44+
45+
let mut store = Store::new(format!("Woolmart"));
46+
store.add_item(format!("chocolate"), 2.0);
47+
store.add_item(format!("doll"), 23.0);
48+
store.add_item(format!("bike"), 146.0);
49+
stores.push(store);
50+
51+
stores
52+
}
53+
54+
fn find_best_store(stores: Vec<Store>, shopping_list: &ShoppingList) -> String {
55+
let threads = stores.into_iter()
56+
.map(|store| {
57+
let shopping_list = shopping_list.clone();
58+
thread::spawn(move || {
59+
shopping_list.items.iter()
60+
.map(|item_name| store.price(item_name))
61+
.fold(0.0, |v, u| v + u)
62+
})
63+
})
64+
.collect();
65+
66+
// EXERCISE 0
67+
assert!(stores.len() > 0);
68+
let mut best = None;
69+
let mut best_price = INFINITY;
70+
for store in stores {
71+
let sum =
72+
if sum < best_price {
73+
best = Some(store);
74+
best_price = sum;
75+
}
76+
}
77+
best.unwrap() // there will always be at least one store
78+
}
79+
80+
pub fn main() {
81+
let shopping_list = ShoppingList {
82+
items: vec![format!("chocolate"),
83+
format!("doll"),
84+
format!("bike")]
85+
};
86+
87+
let stores = build_stores();
88+
let store = find_best_store(&stores, &shopping_list);
89+
println!("Best store: {}", store.name);
90+
}
91+

src/lib.rs

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/main.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
mod example0;
2+
3+
fn main() {
4+
println!("----------------------------------------------------------------------");
5+
example0::main();
6+
}

0 commit comments

Comments
 (0)