arrabiata 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/arrabiata.gemspec +2 -2
- data/lib/arrabiata.rb +22 -2
- data/spec/arrabiata_to_arabic_spec.rb +20 -14
- data/spec/arrabiata_to_roman_spec.rb +17 -35
- metadata +4 -4
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.4.0
|
data/arrabiata.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{arrabiata}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.4.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["invadersmustdie"]
|
12
|
-
s.date = %q{2011-12-
|
12
|
+
s.date = %q{2011-12-27}
|
13
13
|
s.description = %q{dead simple conversion of roman <> arabic numerals}
|
14
14
|
s.email = %q{rugek@dirtyhack.net}
|
15
15
|
s.extra_rdoc_files = [
|
data/lib/arrabiata.rb
CHANGED
@@ -50,9 +50,29 @@ class Arrabiata
|
|
50
50
|
|
51
51
|
result = 0
|
52
52
|
|
53
|
-
str.scan(/\w/)
|
53
|
+
chars = str.scan(/\w/)
|
54
|
+
sum_up = 0
|
55
|
+
sub_down = 0
|
56
|
+
|
57
|
+
chars.each_with_index do |char, idx|
|
54
58
|
raise UnknownRomanNumberError, "'#{char}' is not defined in roman numbers" if !CONVERSION[char]
|
55
|
-
|
59
|
+
|
60
|
+
current_value = CONVERSION[char]
|
61
|
+
next_char = chars[idx+1]
|
62
|
+
next_value = CONVERSION[next_char]
|
63
|
+
|
64
|
+
if next_value && next_value >= current_value
|
65
|
+
if next_value > current_value
|
66
|
+
sub_down += current_value
|
67
|
+
elsif next_value == current_value
|
68
|
+
sum_up += current_value
|
69
|
+
end
|
70
|
+
else
|
71
|
+
result = result + current_value + sum_up - sub_down
|
72
|
+
|
73
|
+
sum_up = 0
|
74
|
+
sub_down = 0
|
75
|
+
end
|
56
76
|
end
|
57
77
|
|
58
78
|
result
|
@@ -13,23 +13,29 @@ describe "Arrabiata -> to_arabic" do
|
|
13
13
|
}.should raise_error(ArgumentError, "first argument is not a String")
|
14
14
|
end
|
15
15
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
16
|
+
roman_to_arabic_map = {
|
17
|
+
"III" => 3,
|
18
|
+
"VIII" => 8,
|
19
|
+
"IX" => 9,
|
20
|
+
"XIV" => 14,
|
21
|
+
"XIX" => 19,
|
22
|
+
"M" => 1000,
|
23
|
+
"MIII" => 1003,
|
24
|
+
"MMDCCCLV" => 2855
|
25
|
+
}
|
23
26
|
|
24
|
-
|
25
|
-
|
27
|
+
roman_to_arabic_map.each do |roman, arabic|
|
28
|
+
it "should convert #{roman} to #{arabic}" do
|
29
|
+
Arrabiata.to_arabic(roman).should == arabic
|
30
|
+
end
|
26
31
|
end
|
27
32
|
|
28
|
-
|
29
|
-
|
30
|
-
|
33
|
+
# smoke test
|
34
|
+
1.upto(1000) do |n|
|
35
|
+
roman_letters = Arrabiata.to_roman(n)
|
31
36
|
|
32
|
-
|
33
|
-
|
37
|
+
it "should convert #{n} / #{roman_letters} forward and backward" do
|
38
|
+
Arrabiata.to_arabic(roman_letters).should == n
|
39
|
+
end
|
34
40
|
end
|
35
41
|
end
|
@@ -3,7 +3,7 @@ require "spec_helper"
|
|
3
3
|
describe "Arrabiata -> to_roman" do
|
4
4
|
it "should fail on 0 because its not included in roman numerals" do
|
5
5
|
lambda {
|
6
|
-
Arrabiata.to_roman(0)
|
6
|
+
Arrabiata.to_roman(0)
|
7
7
|
}.should raise_error(Arrabiata::NoZeroInRomanNumbers, "roman numeral system doesn't include 0")
|
8
8
|
end
|
9
9
|
|
@@ -13,39 +13,21 @@ describe "Arrabiata -> to_roman" do
|
|
13
13
|
}.should raise_error(ArgumentError, "first argument is not a Fixnum")
|
14
14
|
end
|
15
15
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
it "should convert 2855 to MMDCCCLV" do
|
33
|
-
Arrabiata.to_roman(2855).should == "MMDCCCLV"
|
34
|
-
end
|
35
|
-
|
36
|
-
it "should convert 79 to LXXIX" do
|
37
|
-
Arrabiata.to_roman(79).should == "LXXIX"
|
38
|
-
end
|
39
|
-
|
40
|
-
it "should convert 4 to IV" do
|
41
|
-
Arrabiata.to_roman(4).should == "IV"
|
42
|
-
end
|
43
|
-
|
44
|
-
it "should convert 19 to XIX" do
|
45
|
-
Arrabiata.to_roman(19).should == "XIX"
|
46
|
-
end
|
47
|
-
|
48
|
-
it "should convert 8759 to MMMMMMMMDCCLIX" do
|
49
|
-
Arrabiata.to_roman(8759).should == "MMMMMMMMDCCLIX"
|
16
|
+
arabic_to_roman_map = {
|
17
|
+
3 => "III",
|
18
|
+
8 => "VIII",
|
19
|
+
1000 => "M",
|
20
|
+
1003 => "MIII",
|
21
|
+
2855 => "MMDCCCLV",
|
22
|
+
79 => "LXXIX",
|
23
|
+
4 => "IV",
|
24
|
+
19 => "XIX",
|
25
|
+
8759 => "MMMMMMMMDCCLIX"
|
26
|
+
}
|
27
|
+
|
28
|
+
arabic_to_roman_map.each do |arabic, roman|
|
29
|
+
it "should convert #{arabic} to #{roman}" do
|
30
|
+
Arrabiata.to_roman(arabic).should == roman
|
31
|
+
end
|
50
32
|
end
|
51
33
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: arrabiata
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 15
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
8
|
+
- 4
|
9
9
|
- 0
|
10
|
-
version: 0.
|
10
|
+
version: 0.4.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- invadersmustdie
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-12-
|
18
|
+
date: 2011-12-27 00:00:00 +01:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|