fuelator 0.1.0

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
+ SHA1:
3
+ metadata.gz: 84c07b0359c805681e31d8e45cfbdb44bc608984
4
+ data.tar.gz: 1ded9d2012fd3de28786d3e7456023c18b8564cd
5
+ SHA512:
6
+ metadata.gz: a70f869c85fdc639b9930370944dfd2a31543478afaa1b8adb6eeab24e3ee524847cb9eacc8325d914941f7000d5be24fb4927035b1c6e2e2bbef832affa3223
7
+ data.tar.gz: 8d4b262fe7082c29d2280ede76c41665423ca85eee660fd0ef25a9ab45284c8290e2e0f2ebabc59e95c73e34917d969ed7ba1650ec7fc3ac1d9ef78691e6764f
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.1
6
+ before_install: gem install bundler -v 2.1.4
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at sbrytiuk@lgscout.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in fuelator.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
8
+ gem 'pry'
@@ -0,0 +1,40 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ fuelator.rb (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ coderay (1.1.3)
10
+ diff-lcs (1.3)
11
+ method_source (1.0.0)
12
+ pry (0.13.1)
13
+ coderay (~> 1.1)
14
+ method_source (~> 1.0)
15
+ rake (12.3.3)
16
+ rspec (3.8.0)
17
+ rspec-core (~> 3.8.0)
18
+ rspec-expectations (~> 3.8.0)
19
+ rspec-mocks (~> 3.8.0)
20
+ rspec-core (3.8.2)
21
+ rspec-support (~> 3.8.0)
22
+ rspec-expectations (3.8.6)
23
+ diff-lcs (>= 1.2.0, < 2.0)
24
+ rspec-support (~> 3.8.0)
25
+ rspec-mocks (3.8.2)
26
+ diff-lcs (>= 1.2.0, < 2.0)
27
+ rspec-support (~> 3.8.0)
28
+ rspec-support (3.8.3)
29
+
30
+ PLATFORMS
31
+ ruby
32
+
33
+ DEPENDENCIES
34
+ fuelator.rb!
35
+ pry
36
+ rake (~> 12.0)
37
+ rspec (~> 3.0)
38
+
39
+ BUNDLED WITH
40
+ 2.1.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Sergii Brytiuk
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,31 @@
1
+ # Fuelator
2
+
3
+ The goal of this application is to calculate fuel to launch from one planet of the Solar system, and to land on another
4
+ planet of the Solar system, depending on the flight route.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'fuelator.rb'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle install
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install fuelator
21
+
22
+ ## Usage
23
+ If you plan a trip to Moon, once you are at fuel station, run:
24
+
25
+ $ fuelator 28801, [[:launch, 9.807], [:land, 1.62], [:launch, 1.62], [:land, 9.807]]
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ 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).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "fuelator"
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,37 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "fuelator"
4
+ require 'optionparser'
5
+
6
+ def log(object)
7
+ STDERR.puts object
8
+ end
9
+
10
+ verbose = false
11
+
12
+ optparse = OptionParser.new do |opts|
13
+ opts.banner = "Usage: fuelator [args]"
14
+
15
+ opts.separator "Args sample: 28801 launch 9.807 land 1.62 launch 1.62 land 9.807"
16
+ opts.separator "Where 28801 - flight ship mass"
17
+ opts.separator "directive/gravity pairs:"
18
+ opts.separator " launch - flight directive"
19
+ opts.separator " 1.62 - target planet gravity"
20
+ opts.separator "Add as many directive/gravity pairs as you wish. Note, you can not pass 2 same directives in a sequence."
21
+
22
+ opts.on("-v", "--verbose", "Verbose mode") do |v|
23
+ verbose = v
24
+ end
25
+ end
26
+ optparse.parse!
27
+
28
+ def run(params, verbose)
29
+ result = Fuelator.calculate(params)
30
+ log "#{result.to_i} kg"
31
+ rescue Fuelator::Error => e
32
+ log e
33
+ log e.backtrace if verbose
34
+ false
35
+ end
36
+
37
+ exit 1 unless run ARGV, verbose
@@ -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,29 @@
1
+ require_relative 'lib/fuelator/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "fuelator"
5
+ spec.version = Fuelator::VERSION
6
+ spec.authors = ["Sergii Brytiuk"]
7
+ spec.email = ["brytiuk076@gmail.com"]
8
+
9
+ spec.summary = %q{They do not trust Elon Musk any more}
10
+ spec.description = %q{The goal of this application is to calculate fuel
11
+ to launch from one planet of the Solar system, and to land on another
12
+ planet of the Solar system, depending on the flight route}
13
+ spec.homepage = "https://github.com/645383/fuelator"
14
+ spec.license = "MIT"
15
+
16
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
17
+
18
+ spec.metadata["homepage_uri"] = spec.homepage
19
+ spec.metadata["source_code_uri"] = "https://github.com/645383/fuelator"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ end
26
+ spec.bindir = "bin"
27
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+ end
@@ -0,0 +1,56 @@
1
+ require "fuelator/version"
2
+ require "fuelator/parameters"
3
+ # Calculation of fuel required for flight based on input params
4
+
5
+ module Fuelator
6
+ # Calculate fuel for a flight
7
+ #
8
+ # Example:
9
+ # >> Fuelator.calculate(28801, [[:launch, 9.807], [:land, 1.62], [:launch, 1.62], [:land, 9.807]])
10
+ # => 51898
11
+
12
+ class Error < StandardError; end
13
+
14
+ FLIGHT_CONSTANTS = {
15
+ launch: {
16
+ theta: 0.042,
17
+ bias: 33,
18
+ },
19
+ land: {
20
+ theta: 0.033,
21
+ bias: 42,
22
+ },
23
+ }
24
+
25
+ # @param [Array] params
26
+ # [123, [[:launch, 123]]] or flatten [123, :launch, 123]
27
+ # params are validated and validation error may be raised
28
+ # @return Float - fuel for a flight
29
+ def self.calculate(*params)
30
+ parameters = Parameters.new(params)
31
+
32
+ full_mass = parameters.mass
33
+
34
+ parameters.dir_grav_pairs.reverse.map do |val|
35
+ full_mass += recursive_calculate(full_mass, val)
36
+ end.last - parameters.mass
37
+ end
38
+
39
+ private
40
+
41
+ def self.recursive_calculate(mass, params)
42
+ directive = params[0]
43
+ gravity = params[1]
44
+ constants = FLIGHT_CONSTANTS[directive]
45
+
46
+ begin
47
+ fuel_mass = (mass * gravity * constants[:theta] - constants[:bias]).to_i # rounded down
48
+ rescue StandardError => e
49
+ raise Error.new(e.message)
50
+ end
51
+
52
+ return 0 if fuel_mass <= 0
53
+ result = recursive_calculate(fuel_mass, params)
54
+ fuel_mass + result
55
+ end
56
+ end
@@ -0,0 +1,41 @@
1
+ require 'fuelator/parameters/error'
2
+ # For flight parameters validation purposes
3
+
4
+ module Fuelator
5
+ class Parameters
6
+ attr_reader :mass, :dir_grav_pairs
7
+
8
+ def initialize(params)
9
+ params = normalize(params)
10
+ @mass = params[0]
11
+ @dir_grav_pairs = params[1..-1].each_slice(2).to_a
12
+
13
+ validate!
14
+ end
15
+
16
+ private
17
+
18
+ def normalize(params)
19
+ params.flatten.map { |v| (!!Float(v) rescue false) ? v.to_f : v.to_sym }
20
+ end
21
+
22
+ def validate!
23
+ error(:mass, mass) unless mass.is_a?(Float)
24
+ validate_pairs!
25
+ end
26
+
27
+ def validate_pairs!
28
+ dir = nil
29
+ dir_grav_pairs.each do |pair|
30
+ error(:pairs, pair.count) if (pair.count % 2) != 0
31
+ error(:format, pair[0], pair[1]) unless pair[0].is_a?(Symbol) && pair[1].is_a?(Float)
32
+ error(:sequence, dir, pair[0]) if dir == pair[0] # no same directives allowed in a sequence
33
+ dir = pair[0]
34
+ end
35
+ end
36
+
37
+ def error(type, *args)
38
+ raise Error.new Error::MESSAGES[type] % args
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,15 @@
1
+ # Flight parameters validation error,
2
+ # Add error descriptive message to map of messages when new validation case added
3
+
4
+ module Fuelator
5
+ class Parameters
6
+ class Error < StandardError
7
+ MESSAGES = {
8
+ format: "directive/gravity pairs should be in format string/float: directive: %s, gravity: %s",
9
+ sequence: "you can not pass 2 same directives in a sequence: %s => %s",
10
+ mass: "mass should be float: %s",
11
+ pairs: "provide pairs for directive/gravity: %s elements"
12
+ }
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ module Fuelator
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fuelator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sergii Brytiuk
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-07-24 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: |-
14
+ The goal of this application is to calculate fuel
15
+ to launch from one planet of the Solar system, and to land on another
16
+ planet of the Solar system, depending on the flight route
17
+ email:
18
+ - brytiuk076@gmail.com
19
+ executables:
20
+ - console
21
+ - fuelator.rb
22
+ - setup
23
+ extensions: []
24
+ extra_rdoc_files: []
25
+ files:
26
+ - ".gitignore"
27
+ - ".rspec"
28
+ - ".travis.yml"
29
+ - CODE_OF_CONDUCT.md
30
+ - Gemfile
31
+ - Gemfile.lock
32
+ - LICENSE.txt
33
+ - README.md
34
+ - Rakefile
35
+ - bin/console
36
+ - bin/fuelator.rb
37
+ - bin/setup
38
+ - fuelator.gemspec
39
+ - lib/fuelator.rb
40
+ - lib/fuelator/parameters.rb
41
+ - lib/fuelator/parameters/error.rb
42
+ - lib/fuelator/version.rb
43
+ homepage: https://github.com/645383/fuelator
44
+ licenses:
45
+ - MIT
46
+ metadata:
47
+ homepage_uri: https://github.com/645383/fuelator
48
+ source_code_uri: https://github.com/645383/fuelator
49
+ post_install_message:
50
+ rdoc_options: []
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: 2.3.0
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ requirements: []
64
+ rubyforge_project:
65
+ rubygems_version: 2.5.2
66
+ signing_key:
67
+ specification_version: 4
68
+ summary: They do not trust Elon Musk any more
69
+ test_files: []
70
+ has_rdoc: