Skip to content

Commit

Permalink
Kata done with javascript and ruby
Browse files Browse the repository at this point in the history
  • Loading branch information
enriclluelles committed Oct 21, 2011
1 parent b2acc21 commit 0b009dd
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 0 deletions.
60 changes: 60 additions & 0 deletions enriclluelles/javascript/roman_numerals.js
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);
49 changes: 49 additions & 0 deletions enriclluelles/ruby/roman_numerals.rb
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

0 comments on commit 0b009dd

Please sign in to comment.