Skip to content

Commit ca792b7

Browse files
author
Katrina Owen
authored
Merge pull request exercism#24 from mhinz/exercise/roman-numerals
Add exercise: roman-numerals
2 parents 9b71537 + 1cfa088 commit ca792b7

File tree

4 files changed

+101
-0
lines changed

4 files changed

+101
-0
lines changed

config.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@
5454
"slug": "atbash-cipher",
5555
"difficulty": 1,
5656
"topics": []
57+
},
58+
{
59+
"slug": "roman-numerals",
60+
"difficulty": 1,
61+
"topics": []
5762
}
5863
],
5964
"deprecated": [],

exercises/roman-numerals/example.vim

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
function! ToRoman(number) abort
2+
let romandict = [
3+
\ [1000, 'M' ],
4+
\ [ 900, 'CM'],
5+
\ [ 500, 'D' ],
6+
\ [ 400, 'CD'],
7+
\ [ 100, 'C' ],
8+
\ [ 90, 'XC'],
9+
\ [ 50, 'L' ],
10+
\ [ 40, 'XL'],
11+
\ [ 10, 'X' ],
12+
\ [ 9, 'IX'],
13+
\ [ 5, 'V' ],
14+
\ [ 4, 'IV'],
15+
\ [ 1, 'I' ],
16+
\ ]
17+
18+
let num = a:number
19+
let ret = ''
20+
21+
for [arabic, roman] in romandict
22+
while num >= arabic
23+
let ret .= roman
24+
let num -= arabic
25+
endwhile
26+
endfor
27+
28+
return ret
29+
endfunction
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
Execute (1 is a single I):
2+
AssertEqual 'I', ToRoman(1)
3+
4+
Execute (2 is two I's):
5+
AssertEqual 'II', ToRoman(2)
6+
7+
Execute (2 is three I's):
8+
AssertEqual 'III', ToRoman(3)
9+
10+
Execute (4, being 5 - 1, is IV):
11+
AssertEqual 'IV', ToRoman(4)
12+
13+
Execute (5 is a single V):
14+
AssertEqual 'V', ToRoman(5)
15+
16+
Execute (6, being 5 + 1, is VI):
17+
AssertEqual 'VI', ToRoman(6)
18+
19+
Execute (9, being 10 - 1, is IX):
20+
AssertEqual 'IX', ToRoman(9)
21+
22+
Execute (20 is two X's):
23+
AssertEqual 'XXVII', ToRoman(27)
24+
25+
Execute (48 is not 50 - 2, but rather 40 + 8):
26+
AssertEqual 'XLVIII', ToRoman(48)
27+
28+
Execute (50 is a single L):
29+
AssertEqual 'LIX', ToRoman(59)
30+
31+
Execute (90, being 100 - 10, is XC):
32+
AssertEqual 'XCIII', ToRoman(93)
33+
34+
Execute (100 is a single C):
35+
AssertEqual 'CXLI', ToRoman(141)
36+
37+
Execute (60, being 50 + 10, is LX):
38+
AssertEqual 'CLXIII', ToRoman(163)
39+
40+
Execute (400, being 500 - 100, is CD):
41+
AssertEqual 'CDII', ToRoman(402)
42+
43+
Execute (500 is a single D):
44+
AssertEqual 'DLXXV', ToRoman(575)
45+
46+
Execute (900, being 1000 - 100, is CM):
47+
AssertEqual 'CMXI', ToRoman(911)
48+
49+
Execute (1000 is a single M):
50+
AssertEqual 'MXXIV', ToRoman(1024)
51+
52+
Execute (3000 is three M's):
53+
AssertEqual 'MMM', ToRoman(3000)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"
2+
" Write a function to convert Arabic numbers to Roman numerals.
3+
"
4+
" Examples:
5+
"
6+
" :echo ToRoman(1990)
7+
" MCMXC
8+
"
9+
" :echo ToRoman(2008)
10+
" MMVIII
11+
"
12+
function! ToRoman(number) abort
13+
" your code goes here
14+
endfunction

0 commit comments

Comments
 (0)