Skip to content

Commit 0ba9109

Browse files
Merge pull request #80 from IanWhitney/implement_roman_numerals
Implement Roman Numerals exercise
2 parents 6192da4 + 74e42a0 commit 0ba9109

File tree

5 files changed

+161
-0
lines changed

5 files changed

+161
-0
lines changed

config.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"rna-transcription",
1515
"nucleotide-count",
1616
"nucleotide-codons",
17+
"roman-numerals",
1718
"robot-name",
1819
"etl",
1920
"raindrops",

exercises/roman-numerals/Cargo.lock

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[package]
2+
name = "roman-numerals"
3+
version = "0.0.0"
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
static ROMAN_MAP: [(usize, &'static str); 13] = [
2+
(1, "I"),
3+
(4, "IV"),
4+
(5, "V"),
5+
(9, "IX"),
6+
(10, "X"),
7+
(40, "XL"),
8+
(50, "L"),
9+
(90, "XC"),
10+
(100, "C"),
11+
(400, "CD"),
12+
(500, "D"),
13+
(900, "CM"),
14+
(1000, "M"),
15+
];
16+
17+
pub struct Roman {
18+
num: usize
19+
}
20+
21+
impl Roman {
22+
pub fn from(source: usize) -> String {
23+
Roman::new(source).convert()
24+
}
25+
26+
fn convert(&self) -> String {
27+
let mut start = self.num.clone();
28+
let mut result = String::new();
29+
for &(numeric, roman_string) in ROMAN_MAP.into_iter().rev() {
30+
while start >= numeric {
31+
result.push_str(roman_string);
32+
start = start - numeric;
33+
}
34+
}
35+
result
36+
}
37+
38+
fn new(num: usize) -> Roman {
39+
Roman {
40+
num: num,
41+
}
42+
}
43+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
extern crate roman_numerals;
2+
3+
use roman_numerals::*;
4+
5+
#[test]
6+
fn test_one() {
7+
assert_eq!("I", Roman::from(1));
8+
}
9+
10+
#[test]
11+
#[ignore]
12+
fn test_two() {
13+
assert_eq!("II", Roman::from(2));
14+
}
15+
16+
#[test]
17+
#[ignore]
18+
fn test_three() {
19+
assert_eq!("III", Roman::from(3));
20+
}
21+
22+
#[test]
23+
#[ignore]
24+
fn test_four() {
25+
assert_eq!("IV", Roman::from(4));
26+
}
27+
28+
#[test]
29+
#[ignore]
30+
fn test_five() {
31+
assert_eq!("V", Roman::from(5));
32+
}
33+
34+
#[test]
35+
#[ignore]
36+
fn test_six() {
37+
assert_eq!("VI", Roman::from(6));
38+
}
39+
40+
#[test]
41+
#[ignore]
42+
fn test_nine() {
43+
assert_eq!("IX", Roman::from(9));
44+
}
45+
46+
#[test]
47+
#[ignore]
48+
fn test_twenty_seven() {
49+
assert_eq!("XXVII", Roman::from(27));
50+
}
51+
52+
#[test]
53+
#[ignore]
54+
fn test_forty_eight() {
55+
assert_eq!("XLVIII", Roman::from(48));
56+
}
57+
58+
#[test]
59+
#[ignore]
60+
fn test_fifty_nine() {
61+
assert_eq!("LIX", Roman::from(59));
62+
}
63+
64+
#[test]
65+
#[ignore]
66+
fn test_ninety_three() {
67+
assert_eq!("XCIII", Roman::from(93));
68+
}
69+
70+
#[test]
71+
#[ignore]
72+
fn test_141() {
73+
assert_eq!("CXLI", Roman::from(141));
74+
}
75+
76+
#[test]
77+
#[ignore]
78+
fn test_163() {
79+
assert_eq!("CLXIII", Roman::from(163));
80+
}
81+
82+
#[test]
83+
#[ignore]
84+
fn test_402() {
85+
assert_eq!("CDII", Roman::from(402));
86+
}
87+
88+
#[test]
89+
#[ignore]
90+
fn test_575() {
91+
assert_eq!("DLXXV", Roman::from(575));
92+
}
93+
94+
#[test]
95+
#[ignore]
96+
fn test_911() {
97+
assert_eq!("CMXI", Roman::from(911));
98+
}
99+
100+
#[test]
101+
#[ignore]
102+
fn test_1024() {
103+
assert_eq!("MXXIV", Roman::from(1024));
104+
}
105+
106+
#[test]
107+
#[ignore]
108+
fn test_3000() {
109+
assert_eq!("MMM", Roman::from(3000));
110+
}

0 commit comments

Comments
 (0)