Browse Source

Rust exercisms

master
Julio Biason 4 years ago
parent
commit
28e9070df3
  1. 1
      rust/armstrong-numbers/.exercism/metadata.json
  2. 8
      rust/armstrong-numbers/.gitignore
  3. 4
      rust/armstrong-numbers/Cargo.toml
  4. 92
      rust/armstrong-numbers/README.md
  5. 9
      rust/armstrong-numbers/src/lib.rs
  6. 46
      rust/armstrong-numbers/tests/armstrong-numbers.rs
  7. 1
      rust/clock/.exercism/metadata.json
  8. 8
      rust/clock/.gitignore
  9. 6
      rust/clock/Cargo.toml
  10. 102
      rust/clock/README.md
  11. 39
      rust/clock/src/lib.rs
  12. 294
      rust/clock/tests/clock.rs

1
rust/armstrong-numbers/.exercism/metadata.json

@ -0,0 +1 @@
{"track":"rust","exercise":"armstrong-numbers","id":"8db62b77a67247978dc3a6a59e9eb4f3","url":"https://exercism.io/my/solutions/8db62b77a67247978dc3a6a59e9eb4f3","handle":"JBiason","is_requester":true,"auto_approve":false}

8
rust/armstrong-numbers/.gitignore vendored

@ -0,0 +1,8 @@
# Generated by Cargo
# will have compiled files and executables
/target/
**/*.rs.bk
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock
Cargo.lock

4
rust/armstrong-numbers/Cargo.toml

@ -0,0 +1,4 @@
[package]
edition = "2018"
name = "armstrong_numbers"
version = "1.1.0"

92
rust/armstrong-numbers/README.md

@ -0,0 +1,92 @@
# Armstrong Numbers
An [Armstrong number](https://en.wikipedia.org/wiki/Narcissistic_number) is a number that is the sum of its own digits each raised to the power of the number of digits.
For example:
- 9 is an Armstrong number, because `9 = 9^1 = 9`
- 10 is *not* an Armstrong number, because `10 != 1^2 + 0^2 = 1`
- 153 is an Armstrong number, because: `153 = 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153`
- 154 is *not* an Armstrong number, because: `154 != 1^3 + 5^3 + 4^3 = 1 + 125 + 64 = 190`
Write some code to determine whether a number is an Armstrong number.
## Rust Installation
Refer to the [exercism help page][help-page] for Rust installation and learning
resources.
## Writing the Code
Execute the tests with:
```bash
$ cargo test
```
All but the first test have been ignored. After you get the first test to
pass, open the tests source file which is located in the `tests` directory
and remove the `#[ignore]` flag from the next test and get the tests to pass
again. Each separate test is a function with `#[test]` flag above it.
Continue, until you pass every test.
If you wish to run all ignored tests without editing the tests source file, use:
```bash
$ cargo test -- --ignored
```
To run a specific test, for example `some_test`, you can use:
```bash
$ cargo test some_test
```
If the specific test is ignored use:
```bash
$ cargo test some_test -- --ignored
```
To learn more about Rust tests refer to the [online test documentation][rust-tests]
Make sure to read the [Modules][modules] chapter if you
haven't already, it will help you with organizing your files.
## Further improvements
After you have solved the exercise, please consider using the additional utilities, described in the [installation guide](https://exercism.io/tracks/rust/installation), to further refine your final solution.
To format your solution, inside the solution directory use
```bash
cargo fmt
```
To see, if your solution contains some common ineffective use cases, inside the solution directory use
```bash
cargo clippy --all-targets
```
## Submitting the solution
Generally you should submit all files in which you implemented your solution (`src/lib.rs` in most cases). If you are using any external crates, please consider submitting the `Cargo.toml` file. This will make the review process faster and clearer.
## Feedback, Issues, Pull Requests
The [exercism/rust](https://github.com/exercism/rust) repository on GitHub is the home for all of the Rust exercises. If you have feedback about an exercise, or want to help implement new exercises, head over there and create an issue. Members of the rust track team are happy to help!
If you want to know more about Exercism, take a look at the [contribution guide](https://github.com/exercism/docs/blob/master/contributing-to-language-tracks/README.md).
[help-page]: https://exercism.io/tracks/rust/learning
[modules]: https://doc.rust-lang.org/book/ch07-02-defining-modules-to-control-scope-and-privacy.html
[cargo]: https://doc.rust-lang.org/book/ch14-00-more-about-cargo.html
[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html
## Source
Wikipedia [https://en.wikipedia.org/wiki/Narcissistic_number](https://en.wikipedia.org/wiki/Narcissistic_number)
## Submitting Incomplete Solutions
It's possible to submit an incomplete solution so you can see how others have completed the exercise.

9
rust/armstrong-numbers/src/lib.rs

@ -0,0 +1,9 @@
pub fn is_armstrong_number(num: u32) -> bool {
let str_num = num.to_string();
let num_digits = str_num.len() as u32;
num == str_num
.chars()
.map(|x| x.to_digit(10).unwrap())
.map(|n| n.pow(num_digits))
.sum()
}

46
rust/armstrong-numbers/tests/armstrong-numbers.rs

@ -0,0 +1,46 @@
use armstrong_numbers::*;
#[test]
fn test_zero_is_an_armstrong_number() {
assert!(is_armstrong_number(0))
}
#[test]
fn test_single_digit_numbers_are_armstrong_numbers() {
assert!(is_armstrong_number(5))
}
#[test]
fn test_there_are_no_2_digit_armstrong_numbers() {
assert!(!is_armstrong_number(10))
}
#[test]
fn test_three_digit_armstrong_number() {
assert!(is_armstrong_number(153))
}
#[test]
fn test_three_digit_non_armstrong_number() {
assert!(!is_armstrong_number(100))
}
#[test]
fn test_four_digit_armstrong_number() {
assert!(is_armstrong_number(9474))
}
#[test]
fn test_four_digit_non_armstrong_number() {
assert!(!is_armstrong_number(9475))
}
#[test]
fn test_seven_digit_armstrong_number() {
assert!(is_armstrong_number(9_926_315))
}
#[test]
fn test_seven_digit_non_armstrong_number() {
assert!(!is_armstrong_number(9_926_316))
}

1
rust/clock/.exercism/metadata.json

@ -0,0 +1 @@
{"track":"rust","exercise":"clock","id":"bdcd40caa1ae4ebab5d3545c14b21bf7","url":"https://exercism.io/my/solutions/bdcd40caa1ae4ebab5d3545c14b21bf7","handle":"JBiason","is_requester":true,"auto_approve":false}

8
rust/clock/.gitignore vendored

@ -0,0 +1,8 @@
# Generated by Cargo
# will have compiled files and executables
/target/
**/*.rs.bk
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock
Cargo.lock

6
rust/clock/Cargo.toml

@ -0,0 +1,6 @@
[package]
edition = "2018"
name = "clock"
version = "2.4.0"
[dependencies]

102
rust/clock/README.md

@ -0,0 +1,102 @@
# Clock
Implement a clock that handles times without dates.
You should be able to add and subtract minutes to it.
Two clocks that represent the same time should be equal to each other.
## Rust Traits for `.to_string()`
Did you implement `.to_string()` for the `Clock` struct?
If so, try implementing the
[Display trait](https://doc.rust-lang.org/std/fmt/trait.Display.html) for `Clock` instead.
Traits allow for a common way to implement functionality for various types.
For additional learning, consider how you might implement `String::from` for the `Clock` type.
You don't have to actually implement this—it's redundant with `Display`, which is generally the
better choice when the destination type is `String`—but it's useful to have a few type-conversion
traits in your toolkit.
## Rust Installation
Refer to the [exercism help page][help-page] for Rust installation and learning
resources.
## Writing the Code
Execute the tests with:
```bash
$ cargo test
```
All but the first test have been ignored. After you get the first test to
pass, open the tests source file which is located in the `tests` directory
and remove the `#[ignore]` flag from the next test and get the tests to pass
again. Each separate test is a function with `#[test]` flag above it.
Continue, until you pass every test.
If you wish to run all ignored tests without editing the tests source file, use:
```bash
$ cargo test -- --ignored
```
To run a specific test, for example `some_test`, you can use:
```bash
$ cargo test some_test
```
If the specific test is ignored use:
```bash
$ cargo test some_test -- --ignored
```
To learn more about Rust tests refer to the [online test documentation][rust-tests]
Make sure to read the [Modules][modules] chapter if you
haven't already, it will help you with organizing your files.
## Further improvements
After you have solved the exercise, please consider using the additional utilities, described in the [installation guide](https://exercism.io/tracks/rust/installation), to further refine your final solution.
To format your solution, inside the solution directory use
```bash
cargo fmt
```
To see, if your solution contains some common ineffective use cases, inside the solution directory use
```bash
cargo clippy --all-targets
```
## Submitting the solution
Generally you should submit all files in which you implemented your solution (`src/lib.rs` in most cases). If you are using any external crates, please consider submitting the `Cargo.toml` file. This will make the review process faster and clearer.
## Feedback, Issues, Pull Requests
The [exercism/rust](https://github.com/exercism/rust) repository on GitHub is the home for all of the Rust exercises. If you have feedback about an exercise, or want to help implement new exercises, head over there and create an issue. Members of the rust track team are happy to help!
If you want to know more about Exercism, take a look at the [contribution guide](https://github.com/exercism/docs/blob/master/contributing-to-language-tracks/README.md).
[help-page]: https://exercism.io/tracks/rust/learning
[modules]: https://doc.rust-lang.org/book/ch07-02-defining-modules-to-control-scope-and-privacy.html
[cargo]: https://doc.rust-lang.org/book/ch14-00-more-about-cargo.html
[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html
## Source
Pairing session with Erin Drummond [https://twitter.com/ebdrummond](https://twitter.com/ebdrummond)
## Submitting Incomplete Solutions
It's possible to submit an incomplete solution so you can see how others have completed the exercise.

39
rust/clock/src/lib.rs

@ -0,0 +1,39 @@
#[derive(Eq, PartialEq, Debug)]
pub struct Clock {
minutes: i32,
}
const MINUTES_IN_AN_HOUR: i32 = 60;
const HOURS_IN_A_DAY: i32 = 24;
const MINUTES_IN_A_DAY: i32 = HOURS_IN_A_DAY * MINUTES_IN_AN_HOUR;
impl Clock {
pub fn new(hours: i32, minutes: i32) -> Self {
Self {
minutes: Self::as_minutes(hours, minutes),
}
}
pub fn add_minutes(&self, minutes: i32) -> Self {
Self::new(0, self.minutes + minutes)
}
fn as_minutes(hours: i32, minutes: i32) -> i32 {
((hours * MINUTES_IN_AN_HOUR + minutes) % MINUTES_IN_A_DAY + MINUTES_IN_A_DAY)
% MINUTES_IN_A_DAY
}
fn hours(&self) -> i32 {
self.minutes / MINUTES_IN_AN_HOUR
}
fn minutes(&self) -> i32 {
self.minutes % MINUTES_IN_AN_HOUR
}
}
impl std::fmt::Display for Clock {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:02}:{:02}", self.hours(), self.minutes())
}
}

294
rust/clock/tests/clock.rs

@ -0,0 +1,294 @@
use clock::Clock;
//
// Clock Creation
//
#[test]
fn test_on_the_hour() {
assert_eq!(Clock::new(8, 0).to_string(), "08:00");
}
#[test]
fn test_past_the_hour() {
assert_eq!(Clock::new(11, 9).to_string(), "11:09");
}
#[test]
fn test_midnight_is_zero_hours() {
assert_eq!(Clock::new(24, 0).to_string(), "00:00");
}
#[test]
fn test_hour_rolls_over() {
assert_eq!(Clock::new(25, 0).to_string(), "01:00");
}
#[test]
fn test_hour_rolls_over_continuously() {
assert_eq!(Clock::new(100, 0).to_string(), "04:00");
}
#[test]
fn test_sixty_minutes_is_next_hour() {
assert_eq!(Clock::new(1, 60).to_string(), "02:00");
}
#[test]
fn test_minutes_roll_over() {
assert_eq!(Clock::new(0, 160).to_string(), "02:40");
}
#[test]
fn test_minutes_roll_over_continuously() {
assert_eq!(Clock::new(0, 1723).to_string(), "04:43");
}
#[test]
fn test_hours_and_minutes_roll_over() {
assert_eq!(Clock::new(25, 160).to_string(), "03:40");
}
#[test]
fn test_hours_and_minutes_roll_over_continuously() {
assert_eq!(Clock::new(201, 3001).to_string(), "11:01");
}
#[test]
fn test_hours_and_minutes_roll_over_to_exactly_midnight() {
assert_eq!(Clock::new(72, 8640).to_string(), "00:00");
}
#[test]
fn test_negative_hour() {
assert_eq!(Clock::new(-1, 15).to_string(), "23:15");
}
#[test]
fn test_negative_hour_roll_over() {
assert_eq!(Clock::new(-25, 00).to_string(), "23:00");
}
#[test]
fn test_negative_hour_roll_over_continuously() {
assert_eq!(Clock::new(-91, 00).to_string(), "05:00");
}
#[test]
fn test_negative_minutes() {
assert_eq!(Clock::new(1, -40).to_string(), "00:20");
}
#[test]
fn test_negative_minutes_roll_over() {
assert_eq!(Clock::new(1, -160).to_string(), "22:20");
}
#[test]
fn test_negative_minutes_roll_over_continuously() {
assert_eq!(Clock::new(1, -4820).to_string(), "16:40");
}
#[test]
fn test_negative_sixty_minutes_is_prev_hour() {
assert_eq!(Clock::new(2, -60).to_string(), "01:00");
}
#[test]
fn test_negative_hour_and_minutes_both_roll_over() {
assert_eq!(Clock::new(-25, -160).to_string(), "20:20");
}
#[test]
fn test_negative_hour_and_minutes_both_roll_over_continuously() {
assert_eq!(Clock::new(-121, -5810).to_string(), "22:10");
}
#[test]
fn test_zero_hour_and_negative_minutes() {
assert_eq!(Clock::new(0, -22).to_string(), "23:38");
}
//
// Clock Math
//
#[test]
fn test_add_minutes() {
let clock = Clock::new(10, 0).add_minutes(3);
assert_eq!(clock.to_string(), "10:03");
}
#[test]
fn test_add_no_minutes() {
let clock = Clock::new(6, 41).add_minutes(0);
assert_eq!(clock.to_string(), "06:41");
}
#[test]
fn test_add_to_next_hour() {
let clock = Clock::new(0, 45).add_minutes(40);
assert_eq!(clock.to_string(), "01:25");
}
#[test]
fn test_add_more_than_one_hour() {
let clock = Clock::new(10, 0).add_minutes(61);
assert_eq!(clock.to_string(), "11:01");
}
#[test]
fn test_add_more_than_two_hours_with_carry() {
let clock = Clock::new(0, 45).add_minutes(160);
assert_eq!(clock.to_string(), "03:25");
}
#[test]
fn test_add_across_midnight() {
let clock = Clock::new(23, 59).add_minutes(2);
assert_eq!(clock.to_string(), "00:01");
}
#[test]
fn test_add_more_than_one_day() {
let clock = Clock::new(5, 32).add_minutes(1500);
assert_eq!(clock.to_string(), "06:32");
}
#[test]
fn test_add_more_than_two_days() {
let clock = Clock::new(1, 1).add_minutes(3500);
assert_eq!(clock.to_string(), "11:21");
}
#[test]
fn test_subtract_minutes() {
let clock = Clock::new(10, 3).add_minutes(-3);
assert_eq!(clock.to_string(), "10:00");
}
#[test]
fn test_subtract_to_previous_hour() {
let clock = Clock::new(10, 3).add_minutes(-30);
assert_eq!(clock.to_string(), "09:33");
}
#[test]
fn test_subtract_more_than_an_hour() {
let clock = Clock::new(10, 3).add_minutes(-70);
assert_eq!(clock.to_string(), "08:53");
}
#[test]
fn test_subtract_across_midnight() {
let clock = Clock::new(0, 3).add_minutes(-4);
assert_eq!(clock.to_string(), "23:59");
}
#[test]
fn test_subtract_more_than_two_hours() {
let clock = Clock::new(0, 0).add_minutes(-160);
assert_eq!(clock.to_string(), "21:20");
}
#[test]
fn test_subtract_more_than_two_hours_with_borrow() {
let clock = Clock::new(6, 15).add_minutes(-160);
assert_eq!(clock.to_string(), "03:35");
}
#[test]
fn test_subtract_more_than_one_day() {
let clock = Clock::new(5, 32).add_minutes(-1500);
assert_eq!(clock.to_string(), "04:32");
}
#[test]
fn test_subtract_mores_than_two_days() {
let clock = Clock::new(2, 20).add_minutes(-3000);
assert_eq!(clock.to_string(), "00:20");
}
//
// Test Equality
//
#[test]
fn test_compare_clocks_for_equality() {
assert_eq!(Clock::new(15, 37), Clock::new(15, 37));
}
#[test]
fn test_compare_clocks_a_minute_apart() {
assert_ne!(Clock::new(15, 36), Clock::new(15, 37));
}
#[test]
fn test_compare_clocks_an_hour_apart() {
assert_ne!(Clock::new(14, 37), Clock::new(15, 37));
}
#[test]
fn test_compare_clocks_with_hour_overflow() {
assert_eq!(Clock::new(10, 37), Clock::new(34, 37));
}
#[test]
fn test_compare_clocks_with_hour_overflow_by_several_days() {
assert_eq!(Clock::new(3, 11), Clock::new(99, 11));
}
#[test]
fn test_compare_clocks_with_negative_hour() {
assert_eq!(Clock::new(22, 40), Clock::new(-2, 40));
}
#[test]
fn test_compare_clocks_with_negative_hour_that_wraps() {
assert_eq!(Clock::new(17, 3), Clock::new(-31, 3));
}
#[test]
fn test_compare_clocks_with_negative_hour_that_wraps_multiple_times() {
assert_eq!(Clock::new(13, 49), Clock::new(-83, 49));
}
#[test]
fn test_compare_clocks_with_minutes_overflow() {
assert_eq!(Clock::new(0, 1), Clock::new(0, 1441));
}
#[test]
fn test_compare_clocks_with_minutes_overflow_by_several_days() {
assert_eq!(Clock::new(2, 2), Clock::new(2, 4322));
}
#[test]
fn test_compare_clocks_with_negative_minute() {
assert_eq!(Clock::new(2, 40), Clock::new(3, -20))
}
#[test]
fn test_compare_clocks_with_negative_minute_that_wraps() {
assert_eq!(Clock::new(4, 10), Clock::new(5, -1490))
}
#[test]
fn test_compare_clocks_with_negative_minute_that_wraps_multiple() {
assert_eq!(Clock::new(6, 15), Clock::new(6, -4305))
}
#[test]
fn test_compare_clocks_with_negative_hours_and_minutes() {
assert_eq!(Clock::new(7, 32), Clock::new(-12, -268))
}
#[test]
fn test_compare_clocks_with_negative_hours_and_minutes_that_wrap() {
assert_eq!(Clock::new(18, 7), Clock::new(-54, -11_513))
}
#[test]
fn test_compare_full_clock_and_zeroed_clock() {
assert_eq!(Clock::new(24, 0), Clock::new(0, 0))
}
Loading…
Cancel
Save