challenge 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3704798030ad77e1abeec1033448f74a161f542f9ddff0cff410e1296aefd775
4
+ data.tar.gz: 5c863502e3ea18a5d31891b94f8d1dd380a80e0efa4ba5d50aa7ae1d63e5faf7
5
+ SHA512:
6
+ metadata.gz: 760829baa1d4f079905abf84a5cad099f64fff27d53a366250c7da2617d25cd66a992a326aa596efced85396456a6084addb10c46e56a45f624cad0d7a6a72ff
7
+ data.tar.gz: fc3515950f5e5509e8e069ca2f9833f77d77d4a53c9164d5c4e8ebf4588f8a09a6c3b4d64ae847b2c920536a26e6ffdf6b533b000f8c089febcdaec730d6909e
@@ -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,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.5
7
+ before_install: gem install bundler -v 2.0.2
@@ -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 iashanmugavel@gmail.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 [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in challenge.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Shanmugavel Arunachalam
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,44 @@
1
+ # Challenge
2
+
3
+ A fun ruby gem to discover economical and distributed partners using input.csv, capacities.csv and partners.csv
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'challenge'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install challenge
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ require 'challenge'
25
+ Challenge::Solution.new('input.csv', 'capacities.csv', 'partners.csv')
26
+ ```
27
+
28
+ ## Development
29
+
30
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
31
+
32
+ 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).
33
+
34
+ ## Contributing
35
+
36
+ Bug reports and pull requests are welcome on GitHub at https://github.com/shanmugavela/challenge. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
41
+
42
+ ## Code of Conduct
43
+
44
+ Everyone interacting in the Challenge project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/shanmugavela/challenge/blob/master/CODE_OF_CONDUCT.md).
@@ -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 "challenge"
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,35 @@
1
+ lib = File.expand_path("lib", __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "challenge/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "challenge"
7
+ spec.version = Challenge::VERSION
8
+ spec.authors = ["Shanmugavel Arunachalam"]
9
+ spec.email = ["shanmugavel.arunachalam@gmail.com"]
10
+
11
+ spec.summary = %q{Ruby gem to discover economical and distributed distributer partners using input.csv, capacities.csv and partners.csv.}
12
+ spec.description = %q{A fun ruby gem to discover economical and distributed distributer partners.}
13
+ spec.homepage = "https://github.com/shanmugavela/challenge."
14
+ spec.license = "MIT"
15
+
16
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
17
+
18
+ spec.metadata["homepage_uri"] = spec.homepage
19
+ spec.metadata["source_code_uri"] = spec.homepage
20
+ spec.metadata["changelog_uri"] = spec.homepage
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
25
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ end
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ spec.add_development_dependency "bundler", "~> 2.0"
32
+ spec.add_development_dependency "rake", "~> 10.0"
33
+ spec.add_development_dependency "rspec", "~> 3.0"
34
+ spec.add_development_dependency "csv", "~> 3.1.2"
35
+ end
@@ -0,0 +1,50 @@
1
+ require "challenge/version"
2
+ require 'csv'
3
+ require_relative 'helper'
4
+ module Challenge
5
+ class Solution
6
+ include Helper
7
+ def initialize(theatres_file, capacities_file, partners_file)
8
+ @theatres = import_theatres_data(theatres_file)
9
+ @capacities = import_capacities_data(capacities_file)
10
+ @partners = import_partners_data(partners_file)
11
+ economical_deliveries
12
+ distributed_deliveries
13
+ end
14
+
15
+ private
16
+ def economical_deliveries
17
+ output1 = @theatres.each { |theatre| theatre.set_deliverable_options(@partners) }.map { |deliverable| deliverable.filter_by_cost }
18
+ export_data('output1.csv', output1)
19
+ end
20
+
21
+ def distributed_deliveries
22
+ delivery_options = @theatres.map { |theatre| theatre.deliverable_options }.select{ |theatre| theatre.size > 0 }
23
+ possible_delivery_options = delivery_options&.first.product(*delivery_options.drop(1))
24
+ deliverable = {}
25
+ deliverable_cost = nil
26
+ possible_delivery_options.each do |possible_delivery_option|
27
+ definded_capacities = @capacities.dup
28
+ feasible_option = {}
29
+ possible_cost = 0
30
+ possible_delivery_option.each do |delivery_option|
31
+ if definded_capacities[delivery_option.partner_id] >= delivery_option.delivery_size
32
+ feasible_option[delivery_option.delivery_id] = delivery_option
33
+ definded_capacities[delivery_option.partner_id] -= delivery_option.delivery_size
34
+ possible_cost += delivery_option.delivery_cost
35
+ else
36
+ feasible_option = deliverable.dup
37
+ possible_cost = deliverable_cost
38
+ break
39
+ end
40
+ end
41
+ if deliverable_cost.nil? || deliverable_cost > possible_cost
42
+ deliverable_cost = possible_cost
43
+ deliverable = feasible_option
44
+ end
45
+ end
46
+ output2 = @theatres.map { |theatre| deliverable[theatre.delivery_id].nil? ? Deliverable.new(theatre.delivery_id, false) : deliverable[theatre.delivery_id] }
47
+ export_data('output2.csv', output2)
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,3 @@
1
+ module Challenge
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,11 @@
1
+ class Deliverable
2
+ attr_accessor :delivery_id, :is_deliverable, :partner_id, :delivery_cost, :delivery_size
3
+
4
+ def initialize(delivery_id, is_deliverable, partner_id="", delivery_cost="", delivery_size="")
5
+ self.delivery_id = delivery_id
6
+ self.is_deliverable = is_deliverable
7
+ self.partner_id = partner_id
8
+ self.delivery_cost = delivery_cost
9
+ self.delivery_size = delivery_size
10
+ end
11
+ end
@@ -0,0 +1,48 @@
1
+ require_relative 'partner'
2
+ require_relative 'theatre'
3
+ require_relative 'deliverable'
4
+ require 'pry'
5
+ module Helper
6
+ def read_data(file_name, have_headers=true)
7
+ csv_text = File.read(file_name)
8
+ csv_data = CSV.parse(csv_text, headers: have_headers)
9
+ csv_data
10
+ end
11
+
12
+ def export_data(file_name, deliverables)
13
+ CSV.open(file_name, "wb", headers: false) do |row|
14
+ deliverables.each do |deliverable|
15
+ row << [deliverable.delivery_id, deliverable.is_deliverable, deliverable.partner_id, deliverable.delivery_cost]
16
+ end
17
+ end
18
+ end
19
+
20
+ def import_capacities_data(file_name)
21
+ capacities_data = read_data(file_name)
22
+ capacities = {}
23
+ capacities_data.each do |capacity_data|
24
+ capacities[capacity_data[0].strip] = capacity_data[1].strip.to_i
25
+ end
26
+ capacities
27
+ end
28
+
29
+ def import_partners_data(file_name)
30
+ partners_data = read_data(file_name)
31
+ partners = []
32
+ partners_data.each do |partner_data|
33
+ slabs = partner_data[1].strip.split('-')
34
+ partners << Partner.new(partner_data[0].strip, slabs.first.to_i, slabs.last.to_i, partner_data[2].strip.to_i, partner_data[3].strip.to_i, partner_data[4].strip)
35
+ end
36
+ partners
37
+ end
38
+
39
+ def import_theatres_data(file_name)
40
+ theatres_data = read_data(file_name, false)
41
+ theatres = []
42
+ theatres_data.each do |theatre_data|
43
+ theatres << Theatre.new(theatre_data[0].strip, theatre_data[1].strip.to_i, theatre_data[2].strip )
44
+ end
45
+ theatres
46
+ end
47
+
48
+ end
@@ -0,0 +1,21 @@
1
+ class Partner
2
+ attr_accessor :theater_id, :minimum_size, :maximum_size, :minimum_cost, :cost_per_gb, :partner_id
3
+
4
+ def initialize(theater_id, minimum_size, maximum_size, minimum_cost, cost_per_gb, partner_id)
5
+ self.theater_id = theater_id
6
+ self.minimum_size = minimum_size
7
+ self.maximum_size = maximum_size
8
+ self.minimum_cost = minimum_cost
9
+ self.cost_per_gb = cost_per_gb
10
+ self.partner_id = partner_id
11
+ end
12
+
13
+ def deliverable_size?(delivery_size)
14
+ delivery_size.between?(minimum_size, maximum_size)
15
+ end
16
+
17
+ def calculate_delivery_cost(theatre)
18
+ calculated_cost = theatre.delivery_size * self.cost_per_gb
19
+ calculated_cost < self.minimum_cost ? self.minimum_cost : calculated_cost
20
+ end
21
+ end
@@ -0,0 +1,27 @@
1
+ class Theatre
2
+ attr_accessor :delivery_id, :delivery_size, :theater_id, :deliverable_options
3
+
4
+ def initialize(delivery_id, delivery_size, theater_id)
5
+ self.delivery_id = delivery_id
6
+ self.delivery_size = delivery_size
7
+ self.theater_id = theater_id
8
+ self.deliverable_options = []
9
+ end
10
+
11
+ def filter_by_cost
12
+ lowest = self.deliverable_options.min_by{ |opt| opt.delivery_cost }
13
+ lowest.nil? ? Deliverable.new(self.delivery_id, false) : lowest
14
+ end
15
+
16
+ def set_deliverable_options(partners)
17
+ available_partners = filtered_partners(partners)
18
+ available_partners.each do |partner|
19
+ self.deliverable_options << Deliverable.new(self.delivery_id, true, partner.partner_id, partner.calculate_delivery_cost(self), self.delivery_size)
20
+ end
21
+ end
22
+
23
+ private
24
+ def filtered_partners(partners)
25
+ partners.select { |partner| partner.theater_id == self.theater_id && partner.deliverable_size?(self.delivery_size)}
26
+ end
27
+ end
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: challenge
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Shanmugavel Arunachalam
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-12-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.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: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: csv
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 3.1.2
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 3.1.2
69
+ description: A fun ruby gem to discover economical and distributed distributer partners.
70
+ email:
71
+ - shanmugavel.arunachalam@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - CODE_OF_CONDUCT.md
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - challenge.gemspec
87
+ - lib/challenge.rb
88
+ - lib/challenge/version.rb
89
+ - lib/deliverable.rb
90
+ - lib/helper.rb
91
+ - lib/partner.rb
92
+ - lib/theatre.rb
93
+ homepage: https://github.com/shanmugavela/challenge.
94
+ licenses:
95
+ - MIT
96
+ metadata:
97
+ allowed_push_host: https://rubygems.org
98
+ homepage_uri: https://github.com/shanmugavela/challenge.
99
+ source_code_uri: https://github.com/shanmugavela/challenge.
100
+ changelog_uri: https://github.com/shanmugavela/challenge.
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubygems_version: 3.0.6
117
+ signing_key:
118
+ specification_version: 4
119
+ summary: Ruby gem to discover economical and distributed distributer partners using
120
+ input.csv, capacities.csv and partners.csv.
121
+ test_files: []