multiplicative_roman_numerals 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/Tests/multiplicative_roman_numerals_tests.rb +72 -0
- data/lib/multiplicative_roman_numerals.rb +62 -0
- metadata +47 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: c9a72b9d68de5fa39658ed9f7824de99595f5392
|
|
4
|
+
data.tar.gz: e6fffccdb9bb9c83e6ea1e281d6b469fcb825b39
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 8f59f2c6c72df6cbe7c7b00b0a2184b29dcfd8abfe43ddc9a38d4fd97a4283643758d8b9246b69cee6dcc8ec5bd7df88dabe90fe9998a489db73dcbe2caff949
|
|
7
|
+
data.tar.gz: 53d4ac6c4d8a4a4a60d21891600407c483839c17d5d751d7edd27ab429ef5b9b5efc39dde83b7394bac705387befaddc85c9e329216f299a18ae977c106c07de
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
$LOAD_PATH << '.' << '../' << 'lib/'
|
|
2
|
+
require 'test/unit'
|
|
3
|
+
require 'multiplicative_roman_numerals'
|
|
4
|
+
|
|
5
|
+
class MultRomanNumTests < Test::Unit::TestCase
|
|
6
|
+
|
|
7
|
+
attr_accessor :converter
|
|
8
|
+
|
|
9
|
+
def setup
|
|
10
|
+
@converter = MultRomanNum.new
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def test_valid_token
|
|
14
|
+
random_nr = Random.rand(10000)
|
|
15
|
+
roman_nr = converter.int_to_roman(random_nr)
|
|
16
|
+
|
|
17
|
+
# Smells, but works
|
|
18
|
+
valid_token = !roman_nr.match(/[^IVXLCDM\*\s]+/i)
|
|
19
|
+
assert(valid_token)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def test_communtative
|
|
23
|
+
assert_equal(249, converter.roman_to_int("XLCCIX"))
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def test_sanity_roman_int_roman
|
|
27
|
+
assert_equal("MMXIII",
|
|
28
|
+
converter.int_to_roman(converter.roman_to_int("MMXIII")))
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def test_sanity_int_roman_int
|
|
32
|
+
nr = 2013
|
|
33
|
+
|
|
34
|
+
assert_equal(nr,
|
|
35
|
+
converter.roman_to_int(converter.int_to_roman(nr)))
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def test_tokens_int_roman
|
|
39
|
+
assert_equal("", converter.int_to_roman(0))
|
|
40
|
+
assert_equal( "I", converter.int_to_roman(1))
|
|
41
|
+
assert_equal("IV", converter.int_to_roman(4))
|
|
42
|
+
assert_equal( "V", converter.int_to_roman(5))
|
|
43
|
+
assert_equal("IX", converter.int_to_roman(9))
|
|
44
|
+
assert_equal( "X", converter.int_to_roman(10))
|
|
45
|
+
assert_equal("XL", converter.int_to_roman(40))
|
|
46
|
+
assert_equal( "L", converter.int_to_roman(50))
|
|
47
|
+
assert_equal("XC", converter.int_to_roman(90))
|
|
48
|
+
assert_equal( "C", converter.int_to_roman(100))
|
|
49
|
+
assert_equal("CD", converter.int_to_roman(400))
|
|
50
|
+
assert_equal( "D", converter.int_to_roman(500))
|
|
51
|
+
assert_equal("CM", converter.int_to_roman(900))
|
|
52
|
+
assert_equal( "M", converter.int_to_roman(1000))
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def test_tokens_roman_int
|
|
56
|
+
assert_equal(0, converter.roman_to_int(""))
|
|
57
|
+
assert_equal(1, converter.roman_to_int("I"))
|
|
58
|
+
assert_equal(4, converter.roman_to_int("IV"))
|
|
59
|
+
assert_equal(5, converter.roman_to_int("V"))
|
|
60
|
+
assert_equal(9, converter.roman_to_int("IX"))
|
|
61
|
+
assert_equal(10, converter.roman_to_int("X"))
|
|
62
|
+
assert_equal(40, converter.roman_to_int("XL"))
|
|
63
|
+
assert_equal(50, converter.roman_to_int("L"))
|
|
64
|
+
assert_equal(90, converter.roman_to_int("XC"))
|
|
65
|
+
assert_equal(100, converter.roman_to_int("C"))
|
|
66
|
+
assert_equal(400, converter.roman_to_int("CD"))
|
|
67
|
+
assert_equal(500, converter.roman_to_int("D"))
|
|
68
|
+
assert_equal(900, converter.roman_to_int("CM"))
|
|
69
|
+
assert_equal(1000, converter.roman_to_int("M"))
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
end
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
class MultRomanNum
|
|
2
|
+
attr_reader :terminals, :special
|
|
3
|
+
|
|
4
|
+
def initialize
|
|
5
|
+
@special = [["CM", 900], ["CD" , 400], ["XC" , 90], ["XL" , 40],
|
|
6
|
+
["IX" , 9], ["IV" , 4]]
|
|
7
|
+
@terminals = [["M" , 1000], ["D" , 500], ["C" , 100], ["L" , 50],
|
|
8
|
+
["X" , 10], ["V" , 5], ["I" , 1]]
|
|
9
|
+
@multiplier = [["* " , 0]]
|
|
10
|
+
@lang = @terminals.concat(@special).sort{ |x,y|
|
|
11
|
+
y[1] <=> x[1]
|
|
12
|
+
}
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def roman_to_int(input)
|
|
16
|
+
result = 0
|
|
17
|
+
inputArr = input.split("")
|
|
18
|
+
|
|
19
|
+
lookahead = []
|
|
20
|
+
inputArr.each_with_index do |val, i|
|
|
21
|
+
if lookahead != []
|
|
22
|
+
lookahead = []
|
|
23
|
+
next
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
if input[i+1] != nil
|
|
27
|
+
conc = val + input[i+1]
|
|
28
|
+
lookahead = @special.select{
|
|
29
|
+
|s| s.include? conc
|
|
30
|
+
}
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
if lookahead != []
|
|
34
|
+
result += lookahead[0][1]
|
|
35
|
+
else
|
|
36
|
+
result += @terminals.select{
|
|
37
|
+
|s| s.include? val
|
|
38
|
+
}[0][1]
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
result
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def int_to_roman(input)
|
|
46
|
+
result = ""
|
|
47
|
+
|
|
48
|
+
@lang.each_index{ |i|
|
|
49
|
+
val = @lang[i][1]
|
|
50
|
+
key = @lang[i][0]
|
|
51
|
+
x = input/val
|
|
52
|
+
if x > 0
|
|
53
|
+
x.times do
|
|
54
|
+
result += key.to_s
|
|
55
|
+
end
|
|
56
|
+
input -= x*val
|
|
57
|
+
end
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
result
|
|
61
|
+
end
|
|
62
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: multiplicative_roman_numerals
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- bentz, larmeh
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2013-02-13 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: |-
|
|
14
|
+
Roman numerals library capable of generating short numerals
|
|
15
|
+
for large numbers.
|
|
16
|
+
email: larmeh@tzi.de
|
|
17
|
+
executables: []
|
|
18
|
+
extensions: []
|
|
19
|
+
extra_rdoc_files: []
|
|
20
|
+
files:
|
|
21
|
+
- Tests/multiplicative_roman_numerals_tests.rb
|
|
22
|
+
- lib/multiplicative_roman_numerals.rb
|
|
23
|
+
homepage: http://rubygems.org/gems/multiplicative_roman_numerals
|
|
24
|
+
licenses:
|
|
25
|
+
- MIT
|
|
26
|
+
metadata: {}
|
|
27
|
+
post_install_message:
|
|
28
|
+
rdoc_options: []
|
|
29
|
+
require_paths:
|
|
30
|
+
- lib
|
|
31
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
32
|
+
requirements:
|
|
33
|
+
- - ">="
|
|
34
|
+
- !ruby/object:Gem::Version
|
|
35
|
+
version: '0'
|
|
36
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - ">="
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '0'
|
|
41
|
+
requirements: []
|
|
42
|
+
rubyforge_project:
|
|
43
|
+
rubygems_version: 2.2.2
|
|
44
|
+
signing_key:
|
|
45
|
+
specification_version: 4
|
|
46
|
+
summary: Yet another roman numerals lib.
|
|
47
|
+
test_files: []
|