factorial_flight 0.0.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 69c44903b7cbddd6aab1fb9d9ad46c07a49ff0d2
4
+ data.tar.gz: 72eda36073cd9c5b8029591fe572b0ca73d2c3bf
5
+ SHA512:
6
+ metadata.gz: c871fae155beec10a76259f79ee28f9dd7a81523670a52cb5e6e315b7332744f51c4ce63f4892eba826f97d047f5ba9996913d06ec185734f656fcdeab412e60
7
+ data.tar.gz: 8a79ff8cb159692994a77e651f6bbb9a0840cf336a9343a203ddadd9654895f0f22fe0ccb332fbdcadfed75b7b36704f8f29a8b8ec210814b8de5d79209d0934
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/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in factorial_flight.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Kerri Miller
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,53 @@
1
+ factorial_flight
2
+ ================
3
+
4
+ Patches 4 different solutions for factorials onto Fixnum.
5
+
6
+ I get asked to calculate factorials in interviews as an example of recursion all the time, so here you go.
7
+
8
+ Can we talk about how I solve the real problems facing your product or business now?
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ gem 'factorial_flight'
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install factorial_flight
23
+
24
+ ## Usage
25
+
26
+ 2.0.0p247 :001 > require 'factorial_flight'
27
+ => true
28
+ 2.0.0p247 :002 > 5.factorial
29
+ => 120
30
+ 2.0.0p247 :003 > 6.factorial(:recursive)
31
+ => 720
32
+ 2.0.0p247 :004 > -1.factorial
33
+ => "Can not calculate factorial of a negative number"
34
+ 2.0.0p247 :005 >
35
+
36
+ Included in the available options for ```#factorial```:
37
+
38
+ :gamma
39
+ : Uses Math::gamma(n+1)
40
+ :inject
41
+ : **Default** - Uses Enumerable's ```#inject``` method
42
+ :loop
43
+ : Uses a loop to iterate over the range of integers included in the factorial
44
+ :recursive
45
+ : Perhaps what most interviewers are specifically asking for when requiring a candidate to write an algorithm to find factorials
46
+
47
+ ## Contributing
48
+
49
+ 1. Fork it ( http://github.com/<my-github-username>/factorial_flight/fork )
50
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
51
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
52
+ 4. Push to the branch (`git push origin my-new-feature`)
53
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ task :default => [:test]
5
+
6
+ Rake::TestTask.new do |t|
7
+ t.libs << 'test'
8
+ t.pattern = "test/*_test.rb"
9
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'factorial_flight/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "factorial_flight"
8
+ spec.version = FactorialFlight::VERSION
9
+ spec.authors = ["Kerri Miller"]
10
+ spec.email = ["kerrizor@kerrizor.com"]
11
+ spec.summary = %q{Factorials patched onto Fixnum, 4 ways.}
12
+ spec.description = %q{I get asked to calculate factorials in interviews as an example of recursion all the time, so here you go. Can we talk about how I solve the real problems facing your product or business now?}
13
+ spec.homepage = "http://github.com/kerrizor/factorial_flight"
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
+ end
@@ -0,0 +1,50 @@
1
+ require 'factorial_flight/version'
2
+
3
+ module FactorialFlight
4
+ def factorial(method = :inject)
5
+ return 'Can not calculate factorial of a negative number' if self < 0
6
+
7
+ case method
8
+ when :gamma
9
+ gamma_method
10
+ when :loop
11
+ loop_method
12
+ when :recursive
13
+ recursive_method
14
+ else
15
+ inject_method
16
+ end
17
+ end
18
+
19
+ private
20
+ def gamma_method
21
+ Math::gamma(self + 1).to_i
22
+ end
23
+
24
+ def inject_method
25
+ return 1 if self < 1
26
+ (1..self).inject(:*)
27
+ end
28
+
29
+ def loop_method
30
+ current_count = 1
31
+
32
+ (1..self).each do |num|
33
+ current_count *= num
34
+ end
35
+
36
+ current_count
37
+ end
38
+
39
+ def recursive_method
40
+ if self <= 1
41
+ 1
42
+ else
43
+ self * (self - 1).factorial
44
+ end
45
+ end
46
+ end
47
+
48
+ class Fixnum
49
+ include FactorialFlight
50
+ end
@@ -0,0 +1,3 @@
1
+ module FactorialFlight
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,47 @@
1
+ require 'test_helper'
2
+
3
+ class TestFactorialFlight < MiniTest::Unit::TestCase
4
+ def test_its_inclusion_on_fixnum
5
+ assert_respond_to 5, :factorial
6
+ end
7
+
8
+ def test_it_returns_a_factorial
9
+ assert_equal 120, 5.factorial
10
+ end
11
+
12
+ def test_it_returns_an_error_on_negative_numbers
13
+ assert_kind_of String, -5.factorial
14
+ end
15
+
16
+ def test_it_returns_an_error_when_providing_an_unknown_method
17
+ assert_kind_of String, -5.factorial(:foobar)
18
+ end
19
+
20
+ def test_it_returns_a_factorial_for_inject
21
+ assert_equal 120, 5.factorial(:inject)
22
+ end
23
+
24
+ def test_it_returns_a_factorial_for_loop
25
+ assert_equal 120, 5.factorial(:loop)
26
+ end
27
+
28
+ def test_it_returns_a_factorial_for_loop
29
+ assert_equal 120, 5.factorial(:gamma)
30
+ end
31
+
32
+ def test_inject_works_for_0
33
+ assert_equal 1, 0.factorial(:inject)
34
+ end
35
+
36
+ def test_loop_works_for_0
37
+ assert_equal 1, 0.factorial(:loop)
38
+ end
39
+
40
+ def test_recursive_works_for_0
41
+ assert_equal 1, 0.factorial(:recursive)
42
+ end
43
+
44
+ def test_recursive_works_for_0
45
+ assert_equal 1, 0.factorial(:gamma)
46
+ end
47
+ end
@@ -0,0 +1,4 @@
1
+ require './lib/factorial_flight'
2
+ require 'minitest/unit'
3
+ require 'minitest/autorun'
4
+ require 'minitest/pride'
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: factorial_flight
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Kerri Miller
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-30 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: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: I get asked to calculate factorials in interviews as an example of recursion
42
+ all the time, so here you go. Can we talk about how I solve the real problems facing
43
+ your product or business now?
44
+ email:
45
+ - kerrizor@kerrizor.com
46
+ executables: []
47
+ extensions: []
48
+ extra_rdoc_files: []
49
+ files:
50
+ - .gitignore
51
+ - Gemfile
52
+ - LICENSE.txt
53
+ - README.md
54
+ - Rakefile
55
+ - factorial_flight.gemspec
56
+ - lib/factorial_flight.rb
57
+ - lib/factorial_flight/version.rb
58
+ - test/factorial_3_ways_test.rb
59
+ - test/test_helper.rb
60
+ homepage: http://github.com/kerrizor/factorial_flight
61
+ licenses:
62
+ - MIT
63
+ metadata: {}
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 2.0.6
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: Factorials patched onto Fixnum, 4 ways.
84
+ test_files:
85
+ - test/factorial_3_ways_test.rb
86
+ - test/test_helper.rb