Skip to content

Commit bdae5c1

Browse files
committed
exercises: avoid confusing names in std types exercise
the exercise used to call the map from values to their counts `values, but it maps *from* values to their counts. it is more aptly called `counts`. additionally, mapping from u32 to u64 in the initial monomorphic implementation make it less than obvious which integer type is the type of the values being counted and which integer type is the count itself. switch the type initially being counted to char to remove this ambiguity. finally, do not use `value` as the variable name for the individual values of the type being counted. though this is technically accurate, it is an unfortunate collision with the fact that hash maps are a key-value data structure, and in this case the keys of the hash map are the values being counted, while its values are the per-value counts. instead, we count "item"s.
1 parent 16aee3e commit bdae5c1

2 files changed

Lines changed: 33 additions & 23 deletions

File tree

‎src/std-types/exercise.md‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ It uses a
1515
to keep track of what values have been seen and how many times each one has
1616
appeared.
1717

18-
The initial version of `Counter` is hardcoded to only work for `u32` values.
18+
The initial version of `Counter` is hardcoded to only work for `char` values.
1919
Make the struct and its methods generic over the type of value being tracked,
2020
that way `Counter` can track any type of value.
2121

@@ -32,29 +32,29 @@ use std::collections::HashMap;
3232
3333
/// Counter counts the number of times each value of type T has been seen.
3434
struct Counter {
35-
values: HashMap<u32, u64>,
35+
counts: HashMap<char, u64>,
3636
}
3737
3838
impl Counter {
3939
/// Create a new Counter.
4040
fn new() -> Self {
4141
Counter {
42-
values: HashMap::new(),
42+
counts: HashMap::new(),
4343
}
4444
}
4545
4646
/// Count an occurrence of the given value.
47-
fn count(&mut self, value: u32) {
48-
if self.values.contains_key(&value) {
49-
*self.values.get_mut(&value).unwrap() += 1;
47+
fn count(&mut self, value: char) {
48+
if self.counts.contains_key(&value) {
49+
*self.counts.get_mut(&value).unwrap() += 1;
5050
} else {
51-
self.values.insert(value, 1);
51+
self.counts.insert(value, 1);
5252
}
5353
}
5454
5555
/// Return the number of times the given value has been seen.
56-
fn times_seen(&self, value: u32) -> u64 {
57-
self.values.get(&value).copied().unwrap_or_default()
56+
fn times_seen(&self, value: char) -> u64 {
57+
self.counts.get(&value).copied().unwrap_or_default()
5858
}
5959
}
6060

‎src/std-types/exercise.rs‎

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,38 +20,48 @@ use std::hash::Hash;
2020

2121
/// Counter counts the number of times each value of type T has been seen.
2222
struct Counter<T> {
23-
values: HashMap<T, u64>,
23+
counts: HashMap<T, u64>,
2424
}
2525

2626
impl<T: Eq + Hash> Counter<T> {
2727
/// Create a new Counter.
2828
fn new() -> Self {
29-
Counter { values: HashMap::new() }
29+
Counter { counts: HashMap::new() }
3030
}
3131

3232
/// Count an occurrence of the given value.
33-
fn count(&mut self, value: T) {
34-
*self.values.entry(value).or_default() += 1;
33+
fn count(&mut self, item: T) {
34+
*self.counts.entry(item).or_default() += 1;
3535
}
3636

3737
/// Return the number of times the given value has been seen.
38-
fn times_seen(&self, value: T) -> u64 {
39-
self.values.get(&value).copied().unwrap_or_default()
38+
fn times_seen(&self, item: T) -> u64 {
39+
self.counts.get(&item).copied().unwrap_or_default()
4040
}
4141
}
4242

4343
// ANCHOR: main
4444
fn main() {
45-
let mut ctr = Counter::new();
46-
ctr.count(13);
47-
ctr.count(14);
48-
ctr.count(16);
49-
ctr.count(14);
50-
ctr.count(14);
51-
ctr.count(11);
45+
let mut charctr = Counter::new();
46+
charctr.count(' ');
47+
charctr.count('a');
48+
charctr.count('¥');
49+
charctr.count('a');
50+
println!(
51+
"most common character: {:?}",
52+
charctr.counts.keys().max_by_key(|&c| charctr.times_seen(*c))
53+
);
54+
55+
let mut intctr = Counter::new();
56+
intctr.count(13);
57+
intctr.count(14);
58+
intctr.count(16);
59+
intctr.count(14);
60+
intctr.count(14);
61+
intctr.count(11);
5262

5363
for i in 10..20 {
54-
println!("saw {} values equal to {}", ctr.times_seen(i), i);
64+
println!("saw {} values equal to {}", intctr.times_seen(i), i);
5565
}
5666

5767
let mut strctr = Counter::new();

0 commit comments

Comments
 (0)