utc_rpn_calc 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +6 -0
- data/lib/utc_rpn_calc.rb +5 -0
- data/lib/utc_rpn_calc/calculator.rb +66 -0
- data/lib/utc_rpn_calc/core_ext/fixnum.rb +12 -0
- data/lib/utc_rpn_calc/core_ext/string.rb +11 -0
- data/lib/utc_rpn_calc/version.rb +3 -0
- data/spec/calculator_spec.rb +83 -0
- data/spec/core_ext/fixnum_spec.rb +21 -0
- data/spec/core_ext/string_spec.rb +38 -0
- data/spec/spec_helper.rb +18 -0
- data/utc_rpn_calc.gemspec +24 -0
- metadata +120 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Colin Rymer
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# UtcRpnCalc
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'utc_rpn_calc'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install utc_rpn_calc
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it ( http://github.com/<my-github-username>/utc_rpn_calc/fork )
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/utc_rpn_calc.rb
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'utc_rpn_calc/core_ext/string'
|
2
|
+
require 'utc_rpn_calc/core_ext/fixnum'
|
3
|
+
|
4
|
+
module UtcRpnCalc
|
5
|
+
class Calculator
|
6
|
+
|
7
|
+
ERROR = "BLARGH!"
|
8
|
+
FFFF = "FFFF".hex
|
9
|
+
|
10
|
+
def initialize(input)
|
11
|
+
@inputs = input.gsub('X', '^').split
|
12
|
+
@stack = []
|
13
|
+
end
|
14
|
+
|
15
|
+
def calculate
|
16
|
+
@inputs.each do |input|
|
17
|
+
return ERROR unless acceptable_input?(input)
|
18
|
+
process_input(input)
|
19
|
+
end
|
20
|
+
|
21
|
+
@stack.pop.to_formatted_hex
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def acceptable_input?(input)
|
27
|
+
input.valid_number? || has_necessary_operands?(input)
|
28
|
+
end
|
29
|
+
|
30
|
+
def process_input(input)
|
31
|
+
@stack.push(result(input))
|
32
|
+
end
|
33
|
+
|
34
|
+
def result(input)
|
35
|
+
input.valid_number? ? input.hex : compute(input)
|
36
|
+
end
|
37
|
+
|
38
|
+
def has_necessary_operands?(operation)
|
39
|
+
@stack.length > (operation.negation_operation? ? 0 : 1)
|
40
|
+
end
|
41
|
+
|
42
|
+
def compute(operation)
|
43
|
+
operation.negation_operation? ? compute_negation : compute_with_operands(operation, @stack.pop(2))
|
44
|
+
end
|
45
|
+
|
46
|
+
def compute_negation
|
47
|
+
@stack.pop.negate
|
48
|
+
end
|
49
|
+
|
50
|
+
def compute_with_operands(operation, operands)
|
51
|
+
raw_result = operands.first.send(operation, operands.last)
|
52
|
+
valid_result(raw_result)
|
53
|
+
end
|
54
|
+
|
55
|
+
def valid_result(result)
|
56
|
+
if result > FFFF
|
57
|
+
FFFF
|
58
|
+
elsif result < 0
|
59
|
+
0
|
60
|
+
else
|
61
|
+
result
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
require 'utc_rpn_calc'
|
4
|
+
|
5
|
+
describe UtcRpnCalc::Calculator do
|
6
|
+
|
7
|
+
subject do
|
8
|
+
UtcRpnCalc::Calculator.new(input).calculate
|
9
|
+
end
|
10
|
+
|
11
|
+
describe 'operations' do
|
12
|
+
|
13
|
+
context 'addition' do
|
14
|
+
let(:input) { "1 2 +" }
|
15
|
+
it { should eq("0003") }
|
16
|
+
end
|
17
|
+
|
18
|
+
context 'subtraction' do
|
19
|
+
let(:input) { "F 1 -" }
|
20
|
+
it { should eq("000E") }
|
21
|
+
end
|
22
|
+
|
23
|
+
context 'multiplication' do
|
24
|
+
let(:input) { "3 2 *" }
|
25
|
+
it { should eq("0006") }
|
26
|
+
end
|
27
|
+
|
28
|
+
context 'division' do
|
29
|
+
let(:input) { "6 2 /" }
|
30
|
+
it { should eq("0003") }
|
31
|
+
end
|
32
|
+
|
33
|
+
context 'logical AND' do
|
34
|
+
let(:input) { "FF00 0FF0 &" }
|
35
|
+
it { should eq("0F00") }
|
36
|
+
end
|
37
|
+
|
38
|
+
context 'logical OR' do
|
39
|
+
let(:input) { "FF00 0FF0 |" }
|
40
|
+
it { should eq("FFF0") }
|
41
|
+
end
|
42
|
+
|
43
|
+
context 'logical XOR' do
|
44
|
+
let(:input) { "FF00 0FF0 X" }
|
45
|
+
it { should eq("F0F0") }
|
46
|
+
end
|
47
|
+
|
48
|
+
context 'logical NOT' do
|
49
|
+
let(:input) { "00FF ~" }
|
50
|
+
it { should eq("FF00") }
|
51
|
+
end
|
52
|
+
|
53
|
+
context 'multiple operations' do
|
54
|
+
let(:input) { "2 3 * 4 +" }
|
55
|
+
it { should eq("000A") }
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
describe 'return value' do
|
61
|
+
|
62
|
+
context 'when input is valid' do
|
63
|
+
|
64
|
+
context 'result greater than FFFF' do
|
65
|
+
let(:input) { "ABCD ABCD +" }
|
66
|
+
it { should eq("FFFF") }
|
67
|
+
end
|
68
|
+
|
69
|
+
context 'result less than 0000' do
|
70
|
+
let(:input) { "5 A -" }
|
71
|
+
it { should eq("0000") }
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
75
|
+
|
76
|
+
context 'when input is invalid' do
|
77
|
+
let(:input) { "2 * 3" }
|
78
|
+
it { should eq("BLARGH!") }
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Fixnum do
|
4
|
+
|
5
|
+
let(:value) { "FF00".hex }
|
6
|
+
|
7
|
+
describe '#to_formatted_hex' do
|
8
|
+
|
9
|
+
subject { value.to_formatted_hex }
|
10
|
+
it { should eq("FF00") }
|
11
|
+
|
12
|
+
end
|
13
|
+
|
14
|
+
describe '#negate' do
|
15
|
+
|
16
|
+
subject { value.negate }
|
17
|
+
it { should eq("00FF".hex) }
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe String do
|
4
|
+
|
5
|
+
describe '#valid_number?' do
|
6
|
+
|
7
|
+
subject { value.valid_number? }
|
8
|
+
|
9
|
+
context "valid number" do
|
10
|
+
let(:value) { "0FF0" }
|
11
|
+
it { should be_true }
|
12
|
+
end
|
13
|
+
|
14
|
+
context "invalid number" do
|
15
|
+
let(:value) { "03X0" }
|
16
|
+
it { should be_false }
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
describe '#negation_operation?' do
|
22
|
+
|
23
|
+
subject { value.negation_operation? }
|
24
|
+
|
25
|
+
context "negation" do
|
26
|
+
let(:value) { "~" }
|
27
|
+
it { should be_true }
|
28
|
+
end
|
29
|
+
|
30
|
+
context "not negation" do
|
31
|
+
let(:value) { "*" }
|
32
|
+
it { should be_false }
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
|
8
|
+
RSpec.configure do |config|
|
9
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
10
|
+
config.run_all_when_everything_filtered = true
|
11
|
+
config.filter_run :focus
|
12
|
+
|
13
|
+
# Run specs in random order to surface order dependencies. If you find an
|
14
|
+
# order dependency and want to debug it, you can fix the order by providing
|
15
|
+
# the seed, which is printed after each run.
|
16
|
+
# --seed 1234
|
17
|
+
config.order = 'random'
|
18
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'utc_rpn_calc/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "utc_rpn_calc"
|
8
|
+
spec.version = UtcRpnCalc::VERSION
|
9
|
+
spec.authors = ["Colin Rymer"]
|
10
|
+
spec.email = ["colin.rymer@gmail.com"]
|
11
|
+
spec.summary = %q{A simple RPN style calculator that operates on unsigned hex numbers}
|
12
|
+
spec.description = spec.summary
|
13
|
+
spec.homepage = "https://github.com/colinrymer/utc_rpn_calc"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,120 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: utc_rpn_calc
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Colin Rymer
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-02-05 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.5'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.5'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
description: A simple RPN style calculator that operates on unsigned hex numbers
|
63
|
+
email:
|
64
|
+
- colin.rymer@gmail.com
|
65
|
+
executables: []
|
66
|
+
extensions: []
|
67
|
+
extra_rdoc_files: []
|
68
|
+
files:
|
69
|
+
- .gitignore
|
70
|
+
- .rspec
|
71
|
+
- Gemfile
|
72
|
+
- LICENSE.txt
|
73
|
+
- README.md
|
74
|
+
- Rakefile
|
75
|
+
- lib/utc_rpn_calc.rb
|
76
|
+
- lib/utc_rpn_calc/calculator.rb
|
77
|
+
- lib/utc_rpn_calc/core_ext/fixnum.rb
|
78
|
+
- lib/utc_rpn_calc/core_ext/string.rb
|
79
|
+
- lib/utc_rpn_calc/version.rb
|
80
|
+
- spec/calculator_spec.rb
|
81
|
+
- spec/core_ext/fixnum_spec.rb
|
82
|
+
- spec/core_ext/string_spec.rb
|
83
|
+
- spec/spec_helper.rb
|
84
|
+
- utc_rpn_calc.gemspec
|
85
|
+
homepage: https://github.com/colinrymer/utc_rpn_calc
|
86
|
+
licenses:
|
87
|
+
- MIT
|
88
|
+
post_install_message:
|
89
|
+
rdoc_options: []
|
90
|
+
require_paths:
|
91
|
+
- lib
|
92
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
93
|
+
none: false
|
94
|
+
requirements:
|
95
|
+
- - ! '>='
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
segments:
|
99
|
+
- 0
|
100
|
+
hash: -4398044844294905452
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
none: false
|
103
|
+
requirements:
|
104
|
+
- - ! '>='
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
segments:
|
108
|
+
- 0
|
109
|
+
hash: -4398044844294905452
|
110
|
+
requirements: []
|
111
|
+
rubyforge_project:
|
112
|
+
rubygems_version: 1.8.23
|
113
|
+
signing_key:
|
114
|
+
specification_version: 3
|
115
|
+
summary: A simple RPN style calculator that operates on unsigned hex numbers
|
116
|
+
test_files:
|
117
|
+
- spec/calculator_spec.rb
|
118
|
+
- spec/core_ext/fixnum_spec.rb
|
119
|
+
- spec/core_ext/string_spec.rb
|
120
|
+
- spec/spec_helper.rb
|