r0man 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b3d625173dbf2878b775db385206b85b9f03f01b
4
+ data.tar.gz: 8bea85375e13253f610df0e60f31023f11cda892
5
+ SHA512:
6
+ metadata.gz: 79369ace5caff10c9c8b50cc7183b49570bfa8ed995f6b3c029b8fe806548ab3fdd239c40bbe46ed4f8e3fa22515ff57d119738d074a4a69ee535cfc445f1e82
7
+ data.tar.gz: 867762df91fc7aecc7662c260dd1b03b82ed90c4598e1f365f8143524174e9a1ea4dd590dc37bcd9a8460b432490abd8afe1471a9d2bfece91d6b951207926b8
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Chirantan Mitra
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/lib/r0man.rb ADDED
@@ -0,0 +1,3 @@
1
+ require "r0man/digit"
2
+ require "r0man/maximum_consecutive_count"
3
+ require "r0man/number"
@@ -0,0 +1,68 @@
1
+ module R0man
2
+ class Digit
3
+ attr_reader :value, :name, :max_consecutive_allowed, :allowed_next_digits
4
+
5
+ def initialize(options)
6
+ @value = options[:value]
7
+ @name = options[:name]
8
+ @max_consecutive_allowed = options[:max_consecutive_allowed]
9
+ @allowed_next_digits = options[:allowed_next_digits]
10
+ end
11
+
12
+ def valid?
13
+ true
14
+ end
15
+
16
+ def to_s
17
+ "#{name}(#{value})"
18
+ end
19
+
20
+ def inspect
21
+ to_s
22
+ end
23
+
24
+ def greater_than?(other_digit)
25
+ value > other_digit.value
26
+ end
27
+
28
+ def value_compared_to(other_digit)
29
+ other_digit.greater_than?(self) ? -value : value
30
+ end
31
+
32
+ def allows_next_digit?(other_digit)
33
+ allowed_next_digits.include?(other_digit)
34
+ end
35
+
36
+ M = new(name: "M", value: 1000, max_consecutive_allowed: 3, allowed_next_digits: [])
37
+ D = new(name: "D", value: 500, max_consecutive_allowed: 1, allowed_next_digits: [])
38
+ C = new(name: "C", value: 100, max_consecutive_allowed: 3, allowed_next_digits: [M, D])
39
+ L = new(name: "L", value: 50, max_consecutive_allowed: 1, allowed_next_digits: [])
40
+ X = new(name: "X", value: 10, max_consecutive_allowed: 3, allowed_next_digits: [C, L])
41
+ V = new(name: "V", value: 5, max_consecutive_allowed: 1, allowed_next_digits: [])
42
+ I = new(name: "I", value: 1, max_consecutive_allowed: 3, allowed_next_digits: [X, V])
43
+
44
+ LOOKUP = {
45
+ "I" => I,
46
+ "V" => V,
47
+ "X" => X,
48
+ "L" => L,
49
+ "C" => C,
50
+ "D" => D,
51
+ "M" => M,
52
+ }
53
+
54
+ Invalid = Struct.new(:name) do
55
+ def valid?
56
+ false
57
+ end
58
+ end
59
+
60
+ class << self
61
+ protected :new
62
+
63
+ def parse(letter)
64
+ LOOKUP[letter.upcase] || Invalid.new(letter)
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,25 @@
1
+ module R0man
2
+ class MaximumConsecutiveCount
3
+ class << self
4
+ def for(array)
5
+ MaximumConsecutiveCount.new.for(array)
6
+ end
7
+ end
8
+
9
+ def for(array)
10
+ counts = Hash.new(0)
11
+ current_count = 1
12
+
13
+ (array + [Float::NAN]).each_cons(2) do |item, next_item|
14
+ if item == next_item
15
+ current_count += 1
16
+ else
17
+ current_count = 1
18
+ end
19
+ counts[item] = current_count if current_count > counts[item]
20
+ end
21
+
22
+ counts
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,52 @@
1
+ require "simple_validation"
2
+
3
+ module R0man
4
+ class Number
5
+ include SimpleValidation
6
+
7
+ validate :digits_are_valid
8
+ validate :maximum_consecutive_counts_are_inside_limits
9
+ validate :previous_digit_are_allowed
10
+ validate :same_digit_is_not_added_to_and_subtracted_from_a_larger_digit
11
+
12
+ class << self
13
+ def parse(str)
14
+ new(str)
15
+ end
16
+ end
17
+
18
+ def initialize(str)
19
+ digits = str.each_char.map { |d| Digit.parse(d) }
20
+ @valid, @invalid = digits.partition(&:valid?)
21
+ end
22
+
23
+ def value
24
+ @value ||= @valid.each_cons(2).reduce(0) { |result, (d1, d2)| result += d1.value_compared_to(d2) } + @valid.last.value
25
+ end
26
+
27
+ private
28
+
29
+ def digits_are_valid
30
+ add_error("Number contains invalid characters: #{@invalid.map(&:name).join(", ")}") unless @invalid.empty?
31
+ end
32
+
33
+ def maximum_consecutive_counts_are_inside_limits
34
+ MaximumConsecutiveCount.for(@valid).each do |digit, count|
35
+ tolerance = digit.max_consecutive_allowed
36
+ add_error("#{digit} occurs #{count} times whereas it is allowed only #{tolerance} times") if count > tolerance
37
+ end
38
+ end
39
+
40
+ def previous_digit_are_allowed
41
+ @valid.each_cons(2) do |d1, d2|
42
+ add_error("#{d1} cannot be subtracted from #{d2}") if (!d1.allows_next_digit?(d2) && d2.greater_than?(d1))
43
+ end
44
+ end
45
+
46
+ def same_digit_is_not_added_to_and_subtracted_from_a_larger_digit
47
+ @valid.each_cons(3) do |d1, d2, d3|
48
+ add_error("#{d1} cannot be added to and subtracted from #{d2}") if (d1 == d3 && d2.greater_than?(d1))
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,3 @@
1
+ module R0man
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,85 @@
1
+ require_relative "../spec_helper"
2
+
3
+ require_relative "../../lib/r0man/digit"
4
+
5
+ describe "Roman digit" do
6
+ it "has a value" do
7
+ R0man::Digit::X.value.must_equal 10
8
+ end
9
+
10
+ it "has a name" do
11
+ R0man::Digit::X.name.must_equal 'X'
12
+ end
13
+
14
+ it "is valid" do
15
+ R0man::Digit::X.must_be :valid?
16
+ end
17
+
18
+ it "equals itself" do
19
+ R0man::Digit::X.must_equal R0man::Digit::X
20
+ end
21
+
22
+ it "has a string representation" do
23
+ R0man::Digit::X.to_s.must_equal "X(10)"
24
+ end
25
+
26
+ it "is inspectable" do
27
+ R0man::Digit::X.inspect.must_equal "X(10)"
28
+ end
29
+
30
+ describe "when compared to a smaller digit" do
31
+ it "has a positive value" do
32
+ R0man::Digit::X.value_compared_to(R0man::Digit::V).must_equal 10
33
+ end
34
+
35
+ it "knows that it is greater than the smaller digit" do
36
+ R0man::Digit::X.greater_than?(R0man::Digit::V).must_equal true
37
+ end
38
+ end
39
+
40
+ describe "when compared to itself" do
41
+ it "has a positive value" do
42
+ R0man::Digit::X.value_compared_to(R0man::Digit::X).must_equal 10
43
+ end
44
+
45
+ it "knows that it doesn't exceed itself" do
46
+ R0man::Digit::X.greater_than?(R0man::Digit::X).must_equal false
47
+ end
48
+ end
49
+
50
+ describe "when compared to a larger digit" do
51
+ it "has a positive value" do
52
+ R0man::Digit::X.value_compared_to(R0man::Digit::L).must_equal -10
53
+ end
54
+
55
+ it "knows that it not greater than the larger digit" do
56
+ R0man::Digit::X.greater_than?(R0man::Digit::L).must_equal false
57
+ end
58
+ end
59
+
60
+ describe ".parse" do
61
+ describe "for a known lower-case letter" do
62
+ it "converts it to the corresponding digit" do
63
+ R0man::Digit.parse("i").must_equal R0man::Digit::I
64
+ end
65
+ end
66
+
67
+ describe "for a known upper-case letter" do
68
+ it "converts it to the corresponding digit" do
69
+ R0man::Digit.parse("X").must_equal R0man::Digit::X
70
+ end
71
+ end
72
+
73
+ describe "for an unknown letter" do
74
+ let(:invalid) { R0man::Digit.parse("g") }
75
+
76
+ it "converts it to an unidentified digit with a name" do
77
+ invalid.name.must_equal "g"
78
+ end
79
+
80
+ it "is invalid" do
81
+ invalid.wont_be :valid?
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,35 @@
1
+ require_relative "../spec_helper"
2
+
3
+ require_relative "../../lib/r0man/maximum_consecutive_count"
4
+
5
+ describe "Maximum consecutive count" do
6
+ describe "for an empty array" do
7
+ it "is nothing" do
8
+ R0man::MaximumConsecutiveCount.for([]).must_equal({})
9
+ end
10
+ end
11
+
12
+ describe "for a single non-repeated element" do
13
+ it "is one" do
14
+ R0man::MaximumConsecutiveCount.for([10]).must_equal({10 => 1})
15
+ end
16
+ end
17
+
18
+ describe "for a single repeated element" do
19
+ it "is the number of repeations" do
20
+ R0man::MaximumConsecutiveCount.for([10, 10, 10]).must_equal({10 => 3})
21
+ end
22
+ end
23
+
24
+ describe "for an array with kinds of items" do
25
+ it "is the number of repeations for each item" do
26
+ R0man::MaximumConsecutiveCount.for([10, 10, 5]).must_equal({10 => 2, 5 => 1})
27
+ end
28
+ end
29
+
30
+ describe "for an array with one type followed by another type followed by first type" do
31
+ it "is the count of maximum number of elements in a single stretch" do
32
+ R0man::MaximumConsecutiveCount.for([10, 5, 10, 10]).must_equal({10 => 2, 5 => 1})
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,65 @@
1
+ require_relative "../spec_helper"
2
+
3
+ require_relative "../../lib/r0man/digit"
4
+ require_relative "../../lib/r0man/number"
5
+ require_relative "../../lib/r0man/maximum_consecutive_count"
6
+
7
+ describe "Roman number" do
8
+ describe ".parse" do
9
+ describe "when all values are same" do
10
+ let(:number) { R0man::Number.parse("III") }
11
+
12
+ it "is the sum of the values" do
13
+ number.value.must_equal 3
14
+ end
15
+ end
16
+
17
+ describe "when a smaller digit follows a larger digit" do
18
+ let(:number) { R0man::Number.parse("VI") }
19
+
20
+ it "is the sum of the values" do
21
+ number.value.must_equal 6
22
+ end
23
+ end
24
+
25
+ describe "when a smaller digit preceeds a larger digit" do
26
+ let(:number) { R0man::Number.parse("IV") }
27
+
28
+ it "the value of the smaller digit is subtracted" do
29
+ number.value.must_equal 4
30
+ end
31
+ end
32
+
33
+ describe "when a smaller digit is between larger digits" do
34
+ let(:number) { R0man::Number.parse("VIV") }
35
+
36
+ it "the value of the smaller digit is subtracted from the sum of the larger digits" do
37
+ number.value.must_equal 9
38
+ end
39
+ end
40
+
41
+ describe "when all letters are known" do
42
+ let(:number) { R0man::Number.parse("IV") }
43
+
44
+ it "creates a roman number out of them" do
45
+ number.value.must_equal 4
46
+ end
47
+
48
+ it "has no errors" do
49
+ number.errors.must_be_empty
50
+ end
51
+ end
52
+
53
+ describe "when some letters are unknown" do
54
+ let(:number) { R0man::Number.parse("ISVG") }
55
+
56
+ it "creates a roman number out of the identified letters" do
57
+ number.value.must_equal 4
58
+ end
59
+
60
+ it "has errors" do
61
+ number.errors.must_equal ["Number contains invalid characters: S, G"]
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,79 @@
1
+ require_relative "../spec_helper"
2
+
3
+ require_relative "../../lib/r0man/digit"
4
+ require_relative "../../lib/r0man/number"
5
+ require_relative "../../lib/r0man/maximum_consecutive_count"
6
+
7
+ describe "Roman number" do
8
+ describe "when a number has three consecutive I" do
9
+ let(:number) { R0man::Number.parse("III") }
10
+
11
+ it "has no errors" do
12
+ number.errors.must_be_empty
13
+ end
14
+ end
15
+
16
+ describe "when a number has one V" do
17
+ let(:number) { R0man::Number.parse("V") }
18
+
19
+ it "has no errors" do
20
+ number.errors.must_be_empty
21
+ end
22
+ end
23
+
24
+ describe "when in a number has four consecutive I" do
25
+ let(:number) { R0man::Number.parse("IIII") }
26
+
27
+ it "has errors" do
28
+ number.errors.must_equal ["I(1) occurs 4 times whereas it is allowed only 3 times"]
29
+ end
30
+ end
31
+
32
+ describe "when a number has two consecutive V" do
33
+ let(:number) { R0man::Number.parse("VV") }
34
+
35
+ it "has errors" do
36
+ number.errors.must_equal ["V(5) occurs 2 times whereas it is allowed only 1 times"]
37
+ end
38
+ end
39
+
40
+ describe "when I comes before V" do
41
+ let(:number) { R0man::Number.parse("IV") }
42
+
43
+ it "has no errors" do
44
+ number.errors.must_be_empty
45
+ end
46
+ end
47
+
48
+ describe "when I comes before X" do
49
+ let(:number) { R0man::Number.parse("IX") }
50
+
51
+ it "has no errors" do
52
+ number.errors.must_be_empty
53
+ end
54
+ end
55
+
56
+ describe "when L is between X and I" do
57
+ let(:number) { R0man::Number.parse("XLI") }
58
+
59
+ it "has no errors" do
60
+ number.errors.must_be_empty
61
+ end
62
+ end
63
+
64
+ describe "when I comes before L" do
65
+ let(:number) { R0man::Number.parse("IL") }
66
+
67
+ it "has errors" do
68
+ number.errors.must_equal ["I(1) cannot be subtracted from L(50)"]
69
+ end
70
+ end
71
+
72
+ describe "when same digit is added to and subtracted from a bigger digit" do
73
+ let(:number) { R0man::Number.parse("IXI") }
74
+
75
+ it "has errors" do
76
+ number.errors.must_equal ["I(1) cannot be added to and subtracted from X(10)"]
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,17 @@
1
+ begin
2
+ require 'simplecov'
3
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new [
4
+ SimpleCov::Formatter::HTMLFormatter
5
+ ]
6
+ SimpleCov.start do
7
+ add_filter '/spec|test|vendor/'
8
+ end
9
+ rescue LoadError
10
+ puts "\nPlease install simplecov to generate coverage report!\n\n"
11
+ end
12
+
13
+ require 'minitest/autorun'
14
+ require 'minitest/spec'
15
+ require 'minitest/reporters'
16
+
17
+ MiniTest::Reporters.use! MiniTest::Reporters::SpecReporter.new
metadata ADDED
@@ -0,0 +1,159 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: r0man
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Chirantan Mitra
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-04-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: simple_validation
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest-reporters
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: yard
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Convert Roman numbers to Indian
112
+ email:
113
+ - chirantan.mitra@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - LICENSE
119
+ - lib/r0man.rb
120
+ - lib/r0man/digit.rb
121
+ - lib/r0man/maximum_consecutive_count.rb
122
+ - lib/r0man/number.rb
123
+ - lib/r0man/version.rb
124
+ - spec/r0man/digit_spec.rb
125
+ - spec/r0man/maximum_consecutive_count_spec.rb
126
+ - spec/r0man/number_spec.rb
127
+ - spec/r0man/number_validity_spec.rb
128
+ - spec/spec_helper.rb
129
+ homepage:
130
+ licenses:
131
+ - MIT
132
+ metadata: {}
133
+ post_install_message:
134
+ rdoc_options: []
135
+ require_paths:
136
+ - lib
137
+ required_ruby_version: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ required_rubygems_version: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ requirements: []
148
+ rubyforge_project: roman
149
+ rubygems_version: 2.5.1
150
+ signing_key:
151
+ specification_version: 4
152
+ summary: Roman number conversions
153
+ test_files:
154
+ - spec/r0man/digit_spec.rb
155
+ - spec/r0man/number_validity_spec.rb
156
+ - spec/r0man/number_spec.rb
157
+ - spec/r0man/maximum_consecutive_count_spec.rb
158
+ - spec/spec_helper.rb
159
+ has_rdoc: