microwave_key_press_efficiency 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Uģis Ozols
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,23 @@
1
+ # Microwave Key Press Efficiency Calcualtor
2
+
3
+ Based on [RubyQuiz #118 - Micrrowave Numbers](http://www.rubyquiz.com/quiz118.html)
4
+
5
+ ## Examples
6
+
7
+ ```ruby
8
+ calc = MicrowaveKeyPressEfficiency::Calculator.new
9
+ ```
10
+
11
+ 99 seconds are the same as 1:39 but is it more effecient? Yes, because 99 is less
12
+ key presses than 139.
13
+
14
+ ```ruby
15
+ calc.keys_to_press(99) # => 99
16
+ ```
17
+
18
+ 71 seconds are equal to 1:11 so here it's more effecient to just press 1 three
19
+ times instead of 7 and 1.
20
+
21
+ ```ruby
22
+ calc.keys_to_press(71) # => 111
23
+ ```
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require 'rspec/core/rake_task'
2
+
3
+ RSpec::Core::RakeTask.new(:spec)
4
+
5
+ task :default => :spec
@@ -0,0 +1,5 @@
1
+ require "microwave_key_press_efficiency/version"
2
+ require "microwave_key_press_efficiency/calculator"
3
+
4
+ module MicrowaveKeyPressEfficiency
5
+ end
@@ -0,0 +1,55 @@
1
+ module MicrowaveKeyPressEfficiency
2
+ class Calculator
3
+ KEYPAD = {
4
+ 1 => [1, 2, 3],
5
+ 2 => [4, 5, 6],
6
+ 3 => [7, 8, 9],
7
+ 4 => [nil, 0, :cook]
8
+ }
9
+
10
+ def keys_to_press(time)
11
+ time_as_entered = time.to_s.split("").map(&:to_i)
12
+
13
+ time_converted = []
14
+ divided_time = time.divmod(60)
15
+ if divided_time[1] == 0
16
+ time_converted << divided_time[0] << divided_time[1] << 0
17
+ elsif divided_time[1] < 10
18
+ time_converted << divided_time[0] << 0 << divided_time[1]
19
+ else
20
+ time_converted << divided_time[0] << divided_time[1]
21
+ end
22
+
23
+ score1 = efficiency(time_as_entered)
24
+ score2 = efficiency(time_converted)
25
+
26
+ score1 > score2 ? time_converted.join.to_i : time
27
+ end
28
+
29
+ def efficiency(*keys)
30
+ keys = keys.dup.flatten!
31
+ score = 0
32
+ prev_floor = nil
33
+ prev_value_position = nil
34
+
35
+ # this key needs to be pressed at the very end
36
+ keys << :cook
37
+
38
+ keys.each_with_index do |key, idx|
39
+ KEYPAD.each_pair do |floor, values|
40
+ if values.include?(key)
41
+ # we want to start adding score only when this is not first iteration
42
+ if idx > 0
43
+ score += (prev_floor - floor).abs
44
+ score += (prev_value_position - values.index(key)).abs
45
+ end
46
+ prev_floor = floor
47
+ prev_value_position = values.index(key)
48
+ end
49
+ end
50
+ end
51
+
52
+ score
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,3 @@
1
+ module MicrowaveKeyPressEfficiency
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+ require File.expand_path("../lib/microwave_key_press_efficiency/version", __FILE__)
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = "microwave_key_press_efficiency"
6
+ spec.version = MicrowaveKeyPressEfficiency::VERSION
7
+ spec.authors = ["Uģis Ozols"]
8
+ spec.email = ["ugis.ozolss@gmail.com"]
9
+ spec.description = %q{Calculate the most effecient way of how to press keys
10
+ on your microwave when you setup a time.}
11
+ spec.summary = %q{Based on http://www.rubyquiz.com/quiz118.html}
12
+ spec.homepage = "http://ugisozols.com"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files`.split($/)
16
+ spec.test_files = spec.files.grep(%r{^spec/})
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.add_development_dependency "bundler", "~> 1.3"
20
+ spec.add_development_dependency "rake"
21
+ spec.add_development_dependency "rspec", "~> 2.13"
22
+ end
@@ -0,0 +1,25 @@
1
+ require File.expand_path("../../../../lib/microwave_key_press_efficiency/calculator", __FILE__)
2
+
3
+ module MicrowaveKeyPressEfficiency
4
+ describe Calculator do
5
+ let(:calculator) { Calculator.new }
6
+
7
+ describe "#keys_to_press" do
8
+ it "returns most effective keys to press" do
9
+ expect(calculator.keys_to_press(99)).to eq(99)
10
+ expect(calculator.keys_to_press(71)).to eq(111)
11
+ expect(calculator.keys_to_press(120)).to eq(200)
12
+ expect(calculator.keys_to_press(123)).to eq(123)
13
+ end
14
+ end
15
+
16
+ describe "#efficiency" do
17
+ it "returns score based on keys that needs to be pressed" do
18
+ expect(calculator.efficiency([9, 9])).to eq(1)
19
+ expect(calculator.efficiency([1, 3, 9])).to eq(5)
20
+ expect(calculator.efficiency([1, 2, 0])).to eq(5)
21
+ expect(calculator.efficiency([2, 0, 0])).to eq(4)
22
+ end
23
+ end
24
+ end
25
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: microwave_key_press_efficiency
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Uģis Ozols
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-07 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.3'
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.3'
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: '2.13'
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: '2.13'
62
+ description: ! "Calculate the most effecient way of how to press keys\n on
63
+ your microwave when you setup a time."
64
+ email:
65
+ - ugis.ozolss@gmail.com
66
+ executables: []
67
+ extensions: []
68
+ extra_rdoc_files: []
69
+ files:
70
+ - .gitignore
71
+ - .rspec
72
+ - Gemfile
73
+ - LICENSE.txt
74
+ - README.md
75
+ - Rakefile
76
+ - lib/microwave_key_press_efficiency.rb
77
+ - lib/microwave_key_press_efficiency/calculator.rb
78
+ - lib/microwave_key_press_efficiency/version.rb
79
+ - microwave_key_press_efficiency.gemspec
80
+ - spec/lib/microwave_key_press_efficiency/calculator_spec.rb
81
+ homepage: http://ugisozols.com
82
+ licenses:
83
+ - MIT
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ! '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubyforge_project:
102
+ rubygems_version: 1.8.23
103
+ signing_key:
104
+ specification_version: 3
105
+ summary: Based on http://www.rubyquiz.com/quiz118.html
106
+ test_files:
107
+ - spec/lib/microwave_key_press_efficiency/calculator_spec.rb
108
+ has_rdoc: