Browse Source

intial workup

master
Jim Infield 3 years ago
parent
commit
470467115b
  1. 23
      README.md
  2. 40
      src/main.rs

23
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

40
src/main.rs

@ -1,3 +1,41 @@
#![allow(unused)]
fn main() { 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("")
} }
Loading…
Cancel
Save