job_interview 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.
- data/lib/job_interview/answer.rb +10 -0
- data/lib/job_interview/fibonacci.rb +36 -0
- data/lib/job_interview/fizz_buzz.rb +21 -0
- data/lib/job_interview/knapsack.rb +7 -0
- data/lib/job_interview.rb +6 -0
- data/spec/fibonacci_spec.rb +19 -0
- data/spec/fizz_buzz_spec.rb +27 -0
- metadata +69 -0
@@ -0,0 +1,36 @@
|
|
1
|
+
module JobInterview
|
2
|
+
module Fibonacci
|
3
|
+
|
4
|
+
##
|
5
|
+
# args should be the strategy to be used (one of :iterative, :recursive)
|
6
|
+
# Defaults to recursive
|
7
|
+
#
|
8
|
+
def fib(n, *args)
|
9
|
+
if args && args.include?(:iterative)
|
10
|
+
iterative_fib(n)
|
11
|
+
else
|
12
|
+
## TODO: make this efficient
|
13
|
+
return Array.new(n) {|i| recursive_fib(i + 1) }
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
def recursive_fib(n)
|
21
|
+
return n if (0..1).include? n
|
22
|
+
recursive_fib(n-1) + recursive_fib(n-2)
|
23
|
+
end
|
24
|
+
|
25
|
+
def iterative_fib(n)
|
26
|
+
result = [1, 1]
|
27
|
+
|
28
|
+
(n - 2) .times do |i|
|
29
|
+
result << result[-1] + result[-2]
|
30
|
+
end
|
31
|
+
|
32
|
+
return result
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module JobInterview
|
2
|
+
module FizzBuzz
|
3
|
+
|
4
|
+
def fizz_buzz(max)
|
5
|
+
acc = []
|
6
|
+
(1..max).each do |n|
|
7
|
+
if ((n % 3 == 0) && (n % 5 == 0))
|
8
|
+
acc << "FizzBuzz"
|
9
|
+
elsif (n % 3 == 0)
|
10
|
+
acc << "Fizz"
|
11
|
+
elsif (n % 5 == 0)
|
12
|
+
acc << "Buzz"
|
13
|
+
else
|
14
|
+
acc << n
|
15
|
+
end
|
16
|
+
end
|
17
|
+
return acc
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,6 @@
|
|
1
|
+
require File.expand_path('../job_interview/fizz_buzz', __FILE__)
|
2
|
+
require File.expand_path('../job_interview/fibonacci', __FILE__)
|
3
|
+
require File.expand_path('../job_interview/knapsack' , __FILE__)
|
4
|
+
require File.expand_path('../job_interview/answer' , __FILE__)
|
5
|
+
module JobInterview
|
6
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require "spec_helper.rb"
|
2
|
+
module FibonacciSpec
|
3
|
+
|
4
|
+
describe "When called" do
|
5
|
+
before(:each) do
|
6
|
+
@answer = JobInterview::Answer.new
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should return the fibinacci sequence in iterative mode" do
|
10
|
+
@answer.fib(10, :iterative).should == [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should return the fibinacci sequence in recursive mode" do
|
14
|
+
@answer.fib(10).should == [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
|
19
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module FizzBuzzSpec
|
4
|
+
|
5
|
+
describe "When called" do
|
6
|
+
before(:each) do
|
7
|
+
@answer = JobInterview::Answer.new
|
8
|
+
end
|
9
|
+
|
10
|
+
it "returns a list of numbers up to n" do
|
11
|
+
@answer.fizz_buzz(5).size.should eq(5)
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should replace 3 with 'Fizz'" do
|
15
|
+
@answer.fizz_buzz(3).last.should eq("Fizz")
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should replace 5 with 'Buzz'" do
|
19
|
+
@answer.fizz_buzz(5).last.should eq("Buzz")
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should replace 15 with 'FizzBuzz'" do
|
23
|
+
@answer.fizz_buzz(15).last.should eq("FizzBuzz")
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: job_interview
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Micah Gates
|
9
|
+
- Jason Lewis
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2012-04-23 00:00:00.000000000 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rspec
|
17
|
+
requirement: &2156290640 !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ~>
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 2.9.0
|
23
|
+
type: :development
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: *2156290640
|
26
|
+
description: Programmer job interview answers, packaged for your convenience. Fibonacci,
|
27
|
+
FizzBuzz, and more! A project from BohConf 2012.
|
28
|
+
email:
|
29
|
+
- github@mgates.com
|
30
|
+
- canweriotnow@gmail.com
|
31
|
+
executables: []
|
32
|
+
extensions: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
files:
|
35
|
+
- lib/job_interview.rb
|
36
|
+
- lib/job_interview/fibonacci.rb
|
37
|
+
- lib/job_interview/fizz_buzz.rb
|
38
|
+
- lib/job_interview/knapsack.rb
|
39
|
+
- lib/job_interview/answer.rb
|
40
|
+
- spec/fibonacci_spec.rb
|
41
|
+
- spec/fizz_buzz_spec.rb
|
42
|
+
homepage: https://github.com/ruby-jokes/job_interview
|
43
|
+
licenses:
|
44
|
+
- GPLv3
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options: []
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ! '>='
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
requirements: []
|
62
|
+
rubyforge_project:
|
63
|
+
rubygems_version: 1.8.15
|
64
|
+
signing_key:
|
65
|
+
specification_version: 3
|
66
|
+
summary: Programmer job interview answers, packaged for your convenience
|
67
|
+
test_files:
|
68
|
+
- spec/fibonacci_spec.rb
|
69
|
+
- spec/fizz_buzz_spec.rb
|