From 67ef044ca63c72607f52839cfe3b24e6f92ce221 Mon Sep 17 00:00:00 2001 From: Augusto Date: Mon, 28 Dec 2020 17:14:17 +0000 Subject: [PATCH] Add subject and test for exercise `roman_numbers` --- rust/tests/roman_numbers_test/Cargo.lock | 12 +++++ rust/tests/roman_numbers_test/Cargo.toml | 10 ++++ rust/tests/roman_numbers_test/src/main.rs | 49 ++++++++++++++++++ subjects/roman_numbers/README.md | 60 +++++++++++++++++++++++ 4 files changed, 131 insertions(+) create mode 100644 rust/tests/roman_numbers_test/Cargo.lock create mode 100644 rust/tests/roman_numbers_test/Cargo.toml create mode 100644 rust/tests/roman_numbers_test/src/main.rs create mode 100644 subjects/roman_numbers/README.md diff --git a/rust/tests/roman_numbers_test/Cargo.lock b/rust/tests/roman_numbers_test/Cargo.lock new file mode 100644 index 00000000..a532508e --- /dev/null +++ b/rust/tests/roman_numbers_test/Cargo.lock @@ -0,0 +1,12 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "roman_numbers" +version = "0.1.0" + +[[package]] +name = "roman_numbers_test" +version = "0.1.0" +dependencies = [ + "roman_numbers", +] diff --git a/rust/tests/roman_numbers_test/Cargo.toml b/rust/tests/roman_numbers_test/Cargo.toml new file mode 100644 index 00000000..195c8f67 --- /dev/null +++ b/rust/tests/roman_numbers_test/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "roman_numbers_test" +version = "0.1.0" +authors = ["Augusto "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +roman_numbers = { path = "../../../../rust-piscine-solutions/roman_numbers"} diff --git a/rust/tests/roman_numbers_test/src/main.rs b/rust/tests/roman_numbers_test/src/main.rs new file mode 100644 index 00000000..bee51375 --- /dev/null +++ b/rust/tests/roman_numbers_test/src/main.rs @@ -0,0 +1,49 @@ +// # Instructions +// Implement the From Trait to create a roman number from a u32 +// the roman number should be in subtractive notation (the common way to write roman +// number I, II, II, IV, V, VI, VII, VIII, IX, X ...) + +// For this start by defining the digits as `RomanDigit` with the values +// I, V, X, L, C, D, M and Nulla for 0 + +// Next define RomanNumber as a wrapper to a vector of RomanDigit's +// And implement the Trait From + +// Examples: +// RomanNumber::from(32) = [X,X,X,I,I] +// RomanNumber::from(9) = [I,X] +// RomanNumber::from(45) = [X,L,V] +// RomanNumber:;from(0) = [Nulla] + +#[allow(unused_imports)] +use roman_numbers::RomanDigit::*; +#[allow(unused_imports)] +use roman_numbers::RomanNumber; + +#[allow(dead_code)] +fn main() { + println!("{:?}", RomanNumber::from(32)); + println!("{:?}", RomanNumber::from(9)); + println!("{:?}", RomanNumber::from(45)); + println!("{:?}", RomanNumber::from(0)); +} +#[test] +fn it_works() { + assert_eq!(RomanNumber::from(3).0, [I, I, I]); + assert_eq!(RomanNumber::from(6).0, [V, I]); + assert_eq!(RomanNumber::from(15).0, [X, V]); + assert_eq!(RomanNumber::from(30).0, [X, X, X]); + assert_eq!(RomanNumber::from(150).0, [C, L]); + assert_eq!(RomanNumber::from(200).0, [C, C]); + assert_eq!(RomanNumber::from(600).0, [D, C]); + assert_eq!(RomanNumber::from(1500).0, [M, D]); +} + +#[test] +fn substractive_notation() { + assert_eq!(RomanNumber::from(4).0, [I, V]); + assert_eq!(RomanNumber::from(44).0, [X, L, I, V]); + assert_eq!(RomanNumber::from(3446).0, [M, M, M, C, D, X, L, V, I]); + assert_eq!(RomanNumber::from(9).0, [I, X]); + assert_eq!(RomanNumber::from(94).0, [X, C, I, V]); +} diff --git a/subjects/roman_numbers/README.md b/subjects/roman_numbers/README.md new file mode 100644 index 00000000..c4c8ea5c --- /dev/null +++ b/subjects/roman_numbers/README.md @@ -0,0 +1,60 @@ +## roman_numbers + +### Instructions +Implement the From Trait to create a roman number from a u32 the roman number should be in subtractive notation (the common way to write roman number I, II, II, IV, V, VI, VII, VIII, IX, X ...) + +For this start by defining the digits as `RomanDigit` with the values I, V, X, L, C, D, M and Nulla for 0 + +Next define RomanNumber as a wrapper to a vector of RomanDigit's And implement the Trait From + +### Expected Functions and Data Structures + +```rust +use crate::RomanDigit::*; + +#[derive(Copy, Clone, Debug, PartialEq, Eq)] +pub enum RomanDigit { + Nulla, + I, + V, + X, + L, + C, + D, + M, +} + +#[derive(Debug, Clone, Debug, PartialEq, Eq)] +pub struct RomanNumber(pub Vec); + +impl From for RomanDigit { +} + +impl From for RomanNumber { +} +``` + +### Usage + +Here is a program to test your function. + +```rust +use roman_numbers::RomanNumber; +fn main() { + println!("{:?}", RomanNumber::from(32)); + println!("{:?}", RomanNumber::from(9)); + println!("{:?}", RomanNumber::from(45)); + println!("{:?}", RomanNumber::from(0)); +} +``` + +And its output + +```console +student@ubuntu:~/[[ROOT]]/test$ cargo run +RomanNumber([X, X, X, I, I]) +RomanNumber([I, X]) +RomanNumber([X, L, V]) +RomanNumber([Nulla]) +student@ubuntu:~/[[ROOT]]/test$ +```