math_expr 0.1.2

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 54c56b10d472df4af1ae3463e5ce82ecb90278ed08065e24a058110125b9dee4
4
+ data.tar.gz: 7586b44329ba1997c1117baa9f43c11e8b13b38db4b525fe60626f7da1e5df27
5
+ SHA512:
6
+ metadata.gz: 7c986bdbf4dfadba2458c1702c3bf3847930060059ce52361ee1691fbc1ae8f5960a9f57be573618381c9357afb14bdcee7244db0a5a2c7ae907879dd4d1850f
7
+ data.tar.gz: c91cc6bddfc6dab44da653c1f8ad2dec89a2e136d146fe3a05f9c2b27310881fbcb8624daa4f3337d324b183fae6a83673faf7f8afc09ad6e45ae9b647b04b1e
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ *.gem
@@ -0,0 +1 @@
1
+ 2.7.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in math_expr.gemspec
4
+ gemspec
@@ -0,0 +1,32 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ math_expr (0.1.2)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.3)
10
+ rspec (3.9.0)
11
+ rspec-core (~> 3.9.0)
12
+ rspec-expectations (~> 3.9.0)
13
+ rspec-mocks (~> 3.9.0)
14
+ rspec-core (3.9.1)
15
+ rspec-support (~> 3.9.1)
16
+ rspec-expectations (3.9.1)
17
+ diff-lcs (>= 1.2.0, < 2.0)
18
+ rspec-support (~> 3.9.0)
19
+ rspec-mocks (3.9.1)
20
+ diff-lcs (>= 1.2.0, < 2.0)
21
+ rspec-support (~> 3.9.0)
22
+ rspec-support (3.9.2)
23
+
24
+ PLATFORMS
25
+ ruby
26
+
27
+ DEPENDENCIES
28
+ math_expr!
29
+ rspec (~> 3.9.0)
30
+
31
+ BUNDLED WITH
32
+ 2.1.2
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Mateusz Kluge
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
13
+ all 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
21
+ THE SOFTWARE.
@@ -0,0 +1,48 @@
1
+ # MathExpr
2
+
3
+ Parses a mathematical expression from infix notation into reverse polish notation and produces a result.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'math_expr'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install math_expr
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ require 'math_expression/parser'
25
+
26
+ parser = MathExpression::Parser.new('4 + 5')
27
+
28
+ parser.to_tokens # => [{:type=>:number, :value=>4}, {:type=>:operator, :value=>"+"}, {:type=>:number, :value=>5}]
29
+
30
+ parser.to_postfix # => [4, 5, "+"]
31
+
32
+ parser.evaluate # => 9
33
+ ```
34
+
35
+ ## Development
36
+
37
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake respec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
38
+
39
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
40
+
41
+ ## Contributing
42
+
43
+ Bug reports and pull requests are welcome on GitHub at https://github.com/inclooder/math_expression.
44
+
45
+
46
+ ## License
47
+
48
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "math_expression"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,4 @@
1
+ require "math_expression/version"
2
+
3
+ module MathExpression
4
+ end
@@ -0,0 +1,125 @@
1
+ # frozen_string_literal: true
2
+
3
+ class MathExpression::Parser
4
+ def initialize(expression)
5
+ @expression = expression
6
+ end
7
+
8
+ def to_tokens
9
+ tokens = []
10
+ reading_number = []
11
+
12
+ form_number = lambda do
13
+ return if reading_number.empty?
14
+ num = reading_number.join.to_f
15
+ tokens << { type: :number, value: num }
16
+ reading_number = []
17
+ end
18
+
19
+ printable_chars = expression.chars.reject { |c| c == ' ' }
20
+
21
+ printable_chars.each do |character|
22
+ if operator?(character)
23
+ form_number.call
24
+ tokens << { type: :operator, value: character }
25
+ elsif parenthesis?(character)
26
+ form_number.call
27
+ tokens << { type: :parenthesis, value: character }
28
+ else
29
+ reading_number << character
30
+ end
31
+ end
32
+ form_number.call
33
+ tokens
34
+ end
35
+
36
+ def to_postfix
37
+ output_queue = []
38
+ operator_stack = []
39
+
40
+ to_tokens.each do |token|
41
+ type, value = token[:type], token[:value]
42
+
43
+ case type
44
+ when :number
45
+ output_queue << value
46
+ when :operator
47
+ loop do
48
+ break if operator_stack.empty?
49
+ break if left_parenthesis?(operator_stack.last)
50
+ operator_on_stack_precedence = operator_precedence(operator_stack.last)
51
+ current_operator_precedence = operator_precedence(value)
52
+ break if operator_on_stack_precedence <= current_operator_precedence
53
+
54
+ output_queue << operator_stack.pop
55
+ end
56
+ operator_stack.push(value)
57
+ when :parenthesis
58
+ if left_parenthesis?(value)
59
+ operator_stack.push(value)
60
+ else
61
+ while !left_parenthesis?(operator_stack.last)
62
+ output_queue << operator_stack.pop
63
+ end
64
+ if left_parenthesis?(operator_stack.last)
65
+ operator_stack.pop
66
+ end
67
+ end
68
+ end
69
+ end
70
+ (output_queue + operator_stack.reverse)
71
+ end
72
+
73
+ def evaluate
74
+ stack = []
75
+ to_postfix.each do |token|
76
+ result = case token
77
+ when '+'
78
+ stack.pop + stack.pop
79
+ when '-'
80
+ first, second = stack.pop, stack.pop
81
+ second - first
82
+ when '*'
83
+ stack.pop * stack.pop
84
+ when '/'
85
+ first, second = stack.pop, stack.pop
86
+ second / first
87
+ else
88
+ token.to_f
89
+ end
90
+ stack.push(result)
91
+ end
92
+ stack.pop
93
+ end
94
+
95
+ private
96
+
97
+ OPERATORS = {
98
+ '+' => 2,
99
+ '-' => 2,
100
+ '*' => 3,
101
+ '/' => 3,
102
+ }.freeze
103
+
104
+ def operator?(token)
105
+ OPERATORS.keys.include?(token)
106
+ end
107
+
108
+ def operator_precedence(operator)
109
+ OPERATORS.fetch(operator)
110
+ end
111
+
112
+ def parenthesis?(token)
113
+ left_parenthesis?(token) || right_parenthesis?(token)
114
+ end
115
+
116
+ def left_parenthesis?(token)
117
+ token == '('
118
+ end
119
+
120
+ def right_parenthesis?(token)
121
+ token == ')'
122
+ end
123
+
124
+ attr_reader :expression
125
+ end
@@ -0,0 +1,3 @@
1
+ module MathExpression
2
+ VERSION = "0.1.2"
3
+ end
@@ -0,0 +1,29 @@
1
+ require_relative 'lib/math_expression/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "math_expr"
5
+ spec.version = MathExpression::VERSION
6
+ spec.authors = ["Inclooder"]
7
+ spec.email = ["inclooder@gmail.com"]
8
+
9
+ spec.summary = %q{Mathematical expression parser and executor}
10
+ spec.description = %q{Mathematical expression parser and executor}
11
+ spec.homepage = "https://github.com/inclooder/math_expr"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ spec.metadata["homepage_uri"] = spec.homepage
16
+ spec.metadata["source_code_uri"] = "https://github.com/inclooder/math_expr"
17
+ spec.metadata["changelog_uri"] = "https://github.com/inclooder/math_expr"
18
+
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ Dir.glob(['**/*', '**/.??*']).
21
+ select { |e| File.file?(e) }.
22
+ reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ end
24
+ spec.bindir = "exe"
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ["lib"]
27
+
28
+ spec.add_development_dependency 'rspec', '~> 3.9.0'
29
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: math_expr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Inclooder
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-05-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 3.9.0
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 3.9.0
27
+ description: Mathematical expression parser and executor
28
+ email:
29
+ - inclooder@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - ".ruby-version"
36
+ - Gemfile
37
+ - Gemfile.lock
38
+ - LICENSE.txt
39
+ - README.md
40
+ - Rakefile
41
+ - bin/console
42
+ - bin/setup
43
+ - lib/math_expression.rb
44
+ - lib/math_expression/parser.rb
45
+ - lib/math_expression/version.rb
46
+ - math_expr-0.1.1.gem
47
+ - math_expr.gemspec
48
+ homepage: https://github.com/inclooder/math_expr
49
+ licenses:
50
+ - MIT
51
+ metadata:
52
+ homepage_uri: https://github.com/inclooder/math_expr
53
+ source_code_uri: https://github.com/inclooder/math_expr
54
+ changelog_uri: https://github.com/inclooder/math_expr
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: 2.3.0
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubygems_version: 3.1.2
71
+ signing_key:
72
+ specification_version: 4
73
+ summary: Mathematical expression parser and executor
74
+ test_files: []