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 CHANGED
@@ -1 +1 @@
1
- 0.3.0
1
+ 0.4.0
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{arrabiata}
8
- s.version = "0.3.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-26}
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 = [
@@ -50,9 +50,29 @@ class Arrabiata
50
50
 
51
51
  result = 0
52
52
 
53
- str.scan(/\w/).each do |char|
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
- result = result + CONVERSION[char]
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
- it "should convert III to 3" do
17
- Arrabiata.to_arabic("III").should == 3
18
- end
19
-
20
- it "should convert VIII to 8" do
21
- Arrabiata.to_arabic("VIII").should == 8
22
- end
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
- it "should convert M to 1000" do
25
- Arrabiata.to_arabic("M").should == 1000
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
- it "should convert MIII to 1003" do
29
- Arrabiata.to_arabic("MIII").should == 1003
30
- end
33
+ # smoke test
34
+ 1.upto(1000) do |n|
35
+ roman_letters = Arrabiata.to_roman(n)
31
36
 
32
- it "should convert MMDCCCLV to 2855 " do
33
- Arrabiata.to_arabic("MMDCCCLV").should == 2855
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
- it "should convert 3 to III" do
17
- Arrabiata.to_roman(3).should == "III"
18
- end
19
-
20
- it "should convert 8 to VIII" do
21
- Arrabiata.to_roman(8).should == "VIII"
22
- end
23
-
24
- it "should convert 1000 to M" do
25
- Arrabiata.to_roman(1000).should == "M"
26
- end
27
-
28
- it "should convert 1003 to M" do
29
- Arrabiata.to_roman(1003).should == "MIII"
30
- end
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: 19
4
+ hash: 15
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 3
8
+ - 4
9
9
  - 0
10
- version: 0.3.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-26 00:00:00 +01:00
18
+ date: 2011-12-27 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies: []
21
21