-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b2acc21
commit 0b009dd
Showing
2 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
assert = require("assert"); | ||
|
||
Number.prototype.toRoman = (function () { | ||
var nmap = { | ||
M: 1000, CM: 900, D: 500, CD: 400, C: 100, XC: 90, L: 50, XL: 40, X: 10, IX: 9, V: 5, IV: 4, I: 1 | ||
}; | ||
|
||
var toRomanR = function toRomanR(num) { | ||
for (var a in nmap) { | ||
if (nmap[a] <= num) { | ||
return a + toRomanR(num - nmap[a]); | ||
} | ||
} | ||
return ''; | ||
} | ||
|
||
return function toRoman() { | ||
return toRomanR(this); | ||
}; | ||
}()); | ||
|
||
String.prototype.toArabic = (function () { | ||
var nmap = { | ||
M: 1000, CM: 900, D: 500, CD: 400, C: 100, XC: 90, L: 50, XL: 40, X: 10, IX: 9, V: 5, IV: 4, I: 1 | ||
}; | ||
|
||
var toArabicR = function toArabicR(str) { | ||
for (var a in nmap){ | ||
if (str.substr(0, a.length) === a) { | ||
return nmap[a] + toArabicR(str.slice(a.length)); | ||
} | ||
} | ||
return 0; | ||
} | ||
|
||
return function toArabic() { | ||
return toArabicR(this); | ||
}; | ||
}()); | ||
|
||
|
||
assert.equal(Number(0).toRoman(),""); | ||
assert.equal(Number(1).toRoman(),"I"); | ||
assert.equal(Number(20).toRoman(),"XX"); | ||
assert.equal(Number(34).toRoman(),"XXXIV"); | ||
assert.equal(Number(99).toRoman(),"XCIX"); | ||
assert.equal(Number(999).toRoman(),"CMXCIX"); | ||
assert.equal(Number(2000).toRoman(),"MM"); | ||
assert.equal(Number(2999).toRoman(),"MMCMXCIX"); | ||
assert.equal(Number(12000).toRoman(),"MMMMMMMMMMMM"); | ||
|
||
assert.equal("".toArabic(),0); | ||
assert.equal("I".toArabic(),1); | ||
assert.equal("XX".toArabic(),20); | ||
assert.equal("XXXIV".toArabic(),34); | ||
assert.equal("XCIX".toArabic(),99); | ||
assert.equal("CMXCIX".toArabic(),999); | ||
assert.equal("MM".toArabic(),2000); | ||
assert.equal("MMCMXCIX".toArabic(),2999); | ||
assert.equal("MMMMMMMMMMMM".toArabic(),12000); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
class Fixnum | ||
def toRoman num = self | ||
@@nmap.each do |k, v| | ||
return k.to_s + toRoman(num - v) if (num >= v) | ||
end | ||
'' | ||
end | ||
|
||
private | ||
@@nmap = { M: 1000, CM: 900, D: 500, CD: 400, C: 100, XC: 90, L: 50, XL: 40, X: 10, IX: 9, V: 5, IV: 4, I: 1 } | ||
end | ||
|
||
class String | ||
def toArabic str = self | ||
@@nmap.each do |k, v| | ||
pat = %r{^#{k}} | ||
return v + toArabic(str.gsub(pat,'')) if str =~ pat | ||
end | ||
0 | ||
end | ||
|
||
private | ||
@@nmap = { M: 1000, CM: 900, D: 500, CD: 400, C: 100, XC: 90, L: 50, XL: 40, X: 10, IX: 9, V: 5, IV: 4, I: 1 } | ||
end | ||
|
||
require 'minitest/spec' | ||
require 'minitest/autorun' | ||
|
||
describe Fixnum do | ||
it "should convert arabics to romans" do | ||
2.toRoman.must_equal 'II' | ||
2000.toRoman.must_equal 'MM' | ||
36.toRoman.must_equal 'XXXVI' | ||
99.toRoman.must_equal 'XCIX' | ||
0.toRoman.must_equal '' | ||
12000.toRoman.must_equal 'M' * 12 | ||
end | ||
end | ||
|
||
describe String do | ||
it "should convert romans to arabics" do | ||
'II'.toArabic.must_equal 2 | ||
'MM'.toArabic.must_equal 2000 | ||
'XXXVI'.toArabic.must_equal 36 | ||
''.toArabic.must_equal 0 | ||
'LDLD'.toArabic.must_equal nil | ||
('M' * 12 + 'IV').toArabic.must_equal 12004 | ||
end | ||
end |