Skip to content

Commit

Permalink
Various notes adjustments
Browse files Browse the repository at this point in the history
Reformat lists, fix blank lines, code blocks, alignment, wrapping, ...

Signed-off-by: Paul Mabileau <[email protected]>
  • Loading branch information
PaulDance committed Nov 12, 2020
1 parent 6b5982b commit a795b3c
Show file tree
Hide file tree
Showing 27 changed files with 164 additions and 191 deletions.
5 changes: 5 additions & 0 deletions notes/anagram.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,31 @@ I benchmarked a few solutions. The results for this solution were slower than
expected.

This solution:

```rust
test test_multiple_anagrams ... bench: 10,408 ns/iter (+/- 1,640)
```

A solution using sort and HashSet:

```rust
test test_multiple_anagrams ... bench: 3,025 ns/iter (+/- 283)
```

A solution using sort without HashMap:

```rust
test test_multiple_anagrams ... bench: 3,296 ns/iter (+/- 216)
```

A solution using unicase and lexical-sort with a HashSet:

```rust
test test_multiple_anagrams ... bench: 7,256 ns/iter (+/- 547)
```

A solution using no sort with a HashMap in a fold:

```rust
test test_multiple_anagrams ... bench: 5,680 ns/iter (+/- 290)
```
Expand Down
2 changes: 1 addition & 1 deletion notes/armstrong-numbers.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Congratulations on passing all the tests!

- I like this solution uses `div_euclid`.
* I like this solution uses `div_euclid`.

I don't benchmark this exercise, but this solution seems to be performant.

Expand Down
18 changes: 7 additions & 11 deletions notes/atbash-cypher.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
Congratulations on passing all the tests!

- I like this solution uses iterators.

- I like this solution uses a macro and includes a test for it.

- I like this approach uses iterators and match expressions.

- I like this approach uses an `reverse` function instead of doing comparisons
with collections such as a HashMap, Vector of tuples, or arrays.

- I like the use of constants.
* I like this solution uses iterators.
* I like this solution uses a macro and includes a test for it.
* I like this approach uses iterators and match expressions.
* I like this approach uses an `reverse` function instead of doing comparisons
with collections such as a HashMap, Vector of tuples, or arrays.
* I like the use of constants.

A quibble is that instead of always adding 1 here

```
```rust
(elem as u8 - A_ASCII) as i32 + 1)
```

Expand Down
20 changes: 11 additions & 9 deletions notes/beer-song.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
Congratulations on passing all the tests!

- I like this solution is readable.
* I like this solution is readable.
* I like the use of:

- I like the use of
```rust
(end..=start)
```
instead of
```rust
(end..start + 1)
```
```rust
(end..=start)
```

instead of:

```rust
(end..start + 1)
```

If you cared to, for a further challenge you might try to convert the if
expressions into a [match
Expand Down
34 changes: 17 additions & 17 deletions notes/benchmark.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,24 @@ extern crate test;
use test::Bencher;
```

- For the test you want to benchmark, replace #[test] with #[bench].
- If the test is marked #[ignore], delete #[ignore]
- Pass in the variable `b: &mut Bencher`.
- Modify the line calling `assert!` to call the function in a closure inside a
call to b.iter().
- I use rust analyzer in Vim, VSCode or CLion, which gives me a couple links to
run the benchmark or to run the code in Debug.
- Example below is from Sublist:
* For the test you want to benchmark, replace #[test] with #[bench].
* If the test is marked #[ignore], delete #[ignore]
* Pass in the variable `b: &mut Bencher`.
* Modify the line calling `assert!` to call the function in a closure inside a
call to b.iter().
* I use rust analyzer in Vim, VSCode or CLion, which gives me a couple links to
run the benchmark or to run the code in Debug.
* Example below is from Sublist:

```rust
#[bench]
fn huge_sublist_not_in_huge_list(b: &mut Bencher) {
let v1: Vec<u64> = (10..1_000_001).collect();
let v2: Vec<u64> = (1..1_000_000).collect();

b.iter(|| sublist(&v1, &v2));
}
```
```rust
#[bench]
fn huge_sublist_not_in_huge_list(b: &mut Bencher) {
let v1: Vec<u64> = (10..1_000_001).collect();
let v2: Vec<u64> = (1..1_000_000).collect();
b.iter(|| sublist(&v1, &v2));
}
```

Finally compile and run the benchmarks using `cargo +nightly bench`.

Expand Down
15 changes: 6 additions & 9 deletions notes/clock.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
Congratulations on passing all the tests!

- I like the use of constants.

- I like that `PartialEq` was derived instead of implementing it manually.

- I like the use of `{:02}:{:02}` instead of calculating the leading zero manually.

- I like the use of `rem_euclid`.

- I like that Clock only stores minutes.
* I like the use of constants.
* I like that `PartialEq` was derived instead of implementing it manually.
* I like the use of `{:02}:{:02}` instead of calculating the leading zero
manually.
* I like the use of `rem_euclid`.
* I like that Clock only stores minutes.

Something to consider is how the logic may possibly be simplified by storing
only minutes in the Clock struct.
Expand Down
11 changes: 4 additions & 7 deletions notes/forth.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
Congratulations on passing all the tests!

- I like the Operation enum.

- I like the use of match statements.

- I like the use of comments

- I like the code is broken up into functions instead of being one big blob.
* I like the Operation enum.
* I like the use of match statements.
* I like the use of comments
* I like the code is broken up into functions instead of being one big blob.

If you care to, you may try getting it to pass another test of my own which is a
bit more complicated.
Expand Down
11 changes: 5 additions & 6 deletions notes/gigasecond.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
Congratulations on passing all the tests!

- I like the large numeric is formatted with underscores to be more readable.

- I like this solution directly adds `Duration` to `start` with the `+` operator.

- I like the expression is directly returned instead of being set to a binding
and returning the binding or using `return` and a semicolon.
* I like the large numeric is formatted with underscores to be more readable.
* I like this solution directly adds `Duration` to `start` with the `+`
operator.
* I like the expression is directly returned instead of being set to a binding
and returning the binding or using `return` and a semicolon.

A minor style point is that usually `rustfmt` will usually put `use` elements
in alphabetical order. In larger programs with a lot of `use` statements it can
Expand Down
9 changes: 6 additions & 3 deletions notes/grains.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ Congratulations on passing all the tests!

I benchmarked this solution. It was among the fastest.

This solution
This solution:

```rust
test test_returns_the_total_number_of_grains_on_the_board ... bench: 0 ns/iter (+/- 0)
```

Here is another 0 nanosecond solution which uses less ceremony
Here is another 0 nanosecond solution which uses less ceremony:

```rust
pub fn square(s: u32) -> u64 {
if (s < 1) | (s > 64) {
Expand All @@ -21,7 +23,8 @@ pub fn total() -> u64 {
}
```

Other solution benchmarks
Other solution benchmarks:

```rust
test test_returns_the_total_number_of_grains_on_the_board ... bench: 0 ns/iter (+/- 0)
test test_returns_the_total_number_of_grains_on_the_board ... bench: 224 ns/iter (+/- 32)
Expand Down
2 changes: 1 addition & 1 deletion notes/hamming.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Congratulations on passing all the tests!

- I like the match on a tuple
* I like the match on a tuple.

As formattted, it may be a bit less readable than a maintainer would like to
come back to in a few months to modify.
Expand Down
46 changes: 23 additions & 23 deletions notes/high-score.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
Congratulations on passing all the tests!

- I like the solution is succinct and readable.
* I like the solution is succinct and readable.
* I like that `personal_best` reuses `personal_top_three`.
* I like that you used a `match` expression.

- I like that `personal_best` reuses `personal_top_three`.

- I like that you used a `match` expression.

`personal_top_three` could be handled by the [min](https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.min) function.
`personal_top_three` could be handled by the
[min](https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.min)
function.

```rust
pub fn personal_top_three(&self) -> Vec<u32> {
let mut tmp = self.scores.to_vec();
tmp.sort();
tmp.reverse();
tmp[0..min(3, tmp.len())].to_vec()
}
pub fn personal_top_three(&self) -> Vec<u32> {
let mut tmp = self.scores.to_vec();
tmp.sort();
tmp.reverse();
tmp[0..min(3, tmp.len())].to_vec()
}
```

`latest` could be a match expression on `scores.is_empty()`, returning `None`
Expand All @@ -30,25 +30,25 @@ so it also could be `self.scores.last().cloned()`.
function.

```rust
pub fn personal_best(&self) -> Option<u32> {
match self.nums.iter().max() {
None => None,
Some(val) => Some(*(val)),
}
pub fn personal_best(&self) -> Option<u32> {
match self.nums.iter().max() {
None => None,
Some(val) => Some(*(val)),
}
}
```

`personal_top_three` could be simplified by the
[min](https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.min)
function.

```rust
pub fn personal_top_three(&self) -> Vec<u32> {
let mut tmp = self.scores.to_vec();
tmp.sort();
tmp.reverse();
tmp[0..min(3, tmp.len())].to_vec()
}
pub fn personal_top_three(&self) -> Vec<u32> {
let mut tmp = self.scores.to_vec();
tmp.sort();
tmp.reverse();
tmp[0..min(3, tmp.len())].to_vec()
}
```

Viewing the community solutions may offer other approaches to this exercise
Expand Down
8 changes: 3 additions & 5 deletions notes/isbn-verifier.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
Congratulations on passing all the tests!

- I like the use of match expressions and iterators.

- I like that the work is broken up into private helper functions.

- I like the use of Result.
* I like the use of match expressions and iterators.
* I like that the work is broken up into private helper functions.
* I like the use of `Result`.

An idiomatic Rust solution.
14 changes: 7 additions & 7 deletions notes/macros.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
Congratulations on passing all the tests!

I like this solution is well-commented.

I like that the logic cascades down. This is probably more readable than a more
succinct but perhaps less readable solution, such as
* I like this solution is well-commented.
* I like that the logic cascades down. This is probably more readable than a
more succinct but perhaps less readable solution, such as

This is more succinct, but perhaps less readable...

```rust
#[macro_export]
macro_rules! hashmap {
Expand All @@ -17,6 +17,6 @@ macro_rules! hashmap {
}
```

The inside pattern is saying to match one or more of key=>val with zero or one
comma after it. The outside pattern is saying there could be zero or more of the
inside pattern, which takes care of `test_empty`.
The inside pattern is saying to match one or more of `key => val` with zero or
one comma after it. The outside pattern is saying there could be zero or more
of the inside pattern, which takes care of `test_empty`.
32 changes: 14 additions & 18 deletions notes/matching-brackets.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
Congratulations on passing all the tests!

- I like this solution is succinct and readable.

- I like the use of constants.

- I like the use of `match`.

- I like that Some and None are used instead of
[unwrap](https://doc.rust-lang.org/stable/rust-by-example/error/option_unwrap.html).
Not that using `unwrap` is not always wrong, but I like that the code shows a
comfort with using Options instead of immediately dereferencing them. In this
solution it goes to graceful handling of the braces and when there is nothing
left to pop.
* I like this solution is succinct and readable.
* I like the use of constants.
* I like the use of `match`.
* I like that Some and None are used instead of
[unwrap](https://doc.rust-lang.org/stable/rust-by-example/error/option_unwrap.html).
Not that using `unwrap` is not always wrong, but I like that the code shows
a comfort with using Options instead of immediately dereferencing them. In
this solution it goes to graceful handling of the braces and when there is
nothing left to pop.

Did you consider using a match in `is_correct_closing_brace`?

Expand All @@ -32,17 +29,16 @@ Also, since it is a helper function, `is_correct_closing_brace` can be private
This:

```rust
if open_brace_stack.len() == 0 {
return true;
}
false

if open_brace_stack.len() == 0 {
return true;
}
false
```

could be just

```rust
open_brace_stack.len() == 0
open_brace_stack.len() == 0
```

There are many idiomatic ways to solve this exercise. For
Expand Down
2 changes: 1 addition & 1 deletion notes/nth-prime.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Congratulations on passing all the tests!

- I like that this solution is succinct and readable.
* I like that this solution is succinct and readable.

FYI, I benchmarked `test_big_prime` and got a quite respectable result. Most
I've seen are usually ~100 milliseconds. Some have been ~7 milliseconds and
Expand Down
Loading

0 comments on commit a795b3c

Please sign in to comment.