From 470467115b8e9b6c5783ab57e4e958ecdc4bf4ca Mon Sep 17 00:00:00 2001 From: Jim Infield Date: Fri, 20 Aug 2021 09:01:26 -0500 Subject: [PATCH] intial workup --- README.md | 23 +++++++++++++++++++++++ src/main.rs | 40 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..92c8c64 --- /dev/null +++ b/README.md @@ -0,0 +1,23 @@ +Create a function taking a positive integer as its parameter and returning a string containing the Roman Numeral representation of that integer. + +Modern Roman numerals are written by expressing each digit separately starting with the left most digit and skipping any digit with a value of zero. In Roman numerals 1990 is rendered: 1000=M, 900=CM, 90=XC; resulting in MCMXC. 2008 is written as 2000=MM, 8=VIII; or MMVIII. 1666 uses each Roman symbol in descending order: MDCLXVI. + +**Example:** +```rust +solution(1000); // should return 'M' +``` +**Help:** + +``` +Symbol Value +I 1 +V 5 +X 10 +L 50 +C 100 +D 500 +M 1,000 +``` +Remember that there can't be more than 3 identical symbols in a row. + +More about roman numerals - http://en.wikipedia.org/wiki/Roman_numerals \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index e7a11a9..ff60b2c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,41 @@ +#![allow(unused)] fn main() { - println!("Hello, world!"); + + let args = vec![ + 182, + 1990, + 1875, + ]; + + println!(); + + for arg in args { + println!("{:4} | {:?}", arg, func(arg)); + }; + + println!(); +} + +fn func(num: i32) -> String { + let mut out = vec![]; + let mut stack = vec![ + (1, "I"), (4, "IV"), (5, "V"), (9, "IX"), + (10, "X"), (40, "XL"), (50, "L"), + (90, "XC"), (100, "C"), (400, "CD"), + (500, "D"), (900, "CM"), (1000, "M"), + ]; + + let mut num = num; + + while let Some(div) = stack.pop() { + while num/div.0 > 0 { + out.push(div.1); + num -= div.0; + } + if num == 0 { + break + } + } + + out.join("") }