job_interview 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/job_interview/hello_world.rb +11 -0
- data/lib/job_interview/knapsack.rb +63 -3
- data/lib/job_interview/questions.rb +3 -2
- data/lib/job_interview/quine.rb +2 -1
- data/spec/knapsack_spec.rb +43 -0
- data/spec/quine_spec.rb +4 -0
- metadata +30 -12
@@ -1,7 +1,67 @@
|
|
1
1
|
module JobInterview
|
2
2
|
module Knapsack
|
3
|
-
|
4
|
-
|
3
|
+
|
4
|
+
# Given a set of items, each with a weight and a value, determines
|
5
|
+
# the maximum profit you can have while keeping the overall weight
|
6
|
+
# smaller than the capacity of the knapsack.
|
7
|
+
#
|
8
|
+
# @param items [Array<Array>] An array of pairs (weight, profit) representing the items
|
9
|
+
# @param capacity [Integer] The capacity of the knapsack
|
10
|
+
# @param algorithm [:memoize, :dynamic] The algorithm used to solve this, defaults to :dynamic
|
11
|
+
#
|
12
|
+
def knapsack(items, capacity, algorithm = :dynamic)
|
13
|
+
if algorithm == :memoize
|
14
|
+
knapsack_memoize(items, capacity)
|
15
|
+
elsif algorithm == :dynamic
|
16
|
+
knapsack_dynamic_programming(items, capacity)
|
17
|
+
end
|
5
18
|
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def knapsack_memoize(items, capacity)
|
23
|
+
cache = Array.new(items.length + 1) { [0] }
|
24
|
+
cache[0] = Array.new(capacity + 1, 0)
|
25
|
+
knapsack_memoize_recursive(items, capacity, items.length, cache)
|
26
|
+
end
|
27
|
+
|
28
|
+
def knapsack_dynamic_programming(items, capacity)
|
29
|
+
cache = Array.new(items.length + 1) { [0] }
|
30
|
+
cache[0] = Array.new(capacity + 1, 0)
|
31
|
+
|
32
|
+
(0..capacity).each do |remaining_weight|
|
33
|
+
items.each_with_index do |item, index|
|
34
|
+
weight, profit = item
|
35
|
+
cache[index + 1][remaining_weight] = if weight <= remaining_weight then
|
36
|
+
[
|
37
|
+
profit + cache[index][remaining_weight - weight], # Take item
|
38
|
+
cache[index][remaining_weight] # Don't take item
|
39
|
+
].max
|
40
|
+
else
|
41
|
+
cache[index][remaining_weight] # Don't take item
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
cache[items.length][capacity]
|
47
|
+
end
|
48
|
+
|
49
|
+
def knapsack_memoize_recursive(items, capacity, position, cache)
|
50
|
+
return cache[position][capacity] if cache[position][capacity]
|
51
|
+
|
52
|
+
weight, profit = items[position - 1]
|
53
|
+
|
54
|
+
cache[position][capacity] = if weight <= capacity then
|
55
|
+
[
|
56
|
+
profit + kmr(items, capacity - weight, position - 1, cache), # Take item
|
57
|
+
kmr(items, capacity, position - 1, cache) # Don't take item
|
58
|
+
].max
|
59
|
+
else
|
60
|
+
kmr(items, capacity, position - 1, cache) # Don't take item
|
61
|
+
end
|
62
|
+
|
63
|
+
cache[position][capacity]
|
64
|
+
end
|
65
|
+
alias_method :kmr, :knapsack_memoize_recursive
|
6
66
|
end
|
7
|
-
end
|
67
|
+
end
|
@@ -18,7 +18,7 @@ module JobInterview
|
|
18
18
|
"seeking to",
|
19
19
|
"leaving because I can't" ,
|
20
20
|
"leaving because I have to",
|
21
|
-
"not happy with the
|
21
|
+
"not happy with the opportunities I have to"
|
22
22
|
].sample + " " +
|
23
23
|
Faker::Company.bs + "."
|
24
24
|
end
|
@@ -73,7 +73,8 @@ module JobInterview
|
|
73
73
|
[
|
74
74
|
"If it does, we can kiss encryption goodbye.",
|
75
75
|
"With our current models of computation, answering that question remains infeasible.",
|
76
|
-
"I doubt it, but it would make life easier for traveling salesmen."
|
76
|
+
"I doubt it, but it would make life easier for traveling salesmen.",
|
77
|
+
"Sure! P = 0 or N = 1!"
|
77
78
|
].sample
|
78
79
|
end
|
79
80
|
|
data/lib/job_interview/quine.rb
CHANGED
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module KnapsackSpec
|
4
|
+
|
5
|
+
describe "When called" do
|
6
|
+
before(:each) do
|
7
|
+
@answer = JobInterview::Answer.new
|
8
|
+
@items = [
|
9
|
+
[4, 3],
|
10
|
+
[1, 2],
|
11
|
+
[12, 20],
|
12
|
+
[6, 4]
|
13
|
+
]
|
14
|
+
end
|
15
|
+
|
16
|
+
cases = [
|
17
|
+
[0, 0],
|
18
|
+
[1, 2],
|
19
|
+
[2, 2],
|
20
|
+
[3, 2],
|
21
|
+
[4, 3],
|
22
|
+
[5, 5],
|
23
|
+
[6, 5],
|
24
|
+
[7, 6],
|
25
|
+
[8, 6],
|
26
|
+
[9, 6],
|
27
|
+
[10, 7],
|
28
|
+
[11, 9],
|
29
|
+
[12, 20],
|
30
|
+
[100, 29]
|
31
|
+
]
|
32
|
+
|
33
|
+
cases.each do |capacity, profit|
|
34
|
+
it "should return #{profit} in memoize mode with capacity #{capacity}" do
|
35
|
+
@answer.knapsack(@items, capacity, :memoize).should == profit
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should return #{profit} in dynamic programming mode with capacity #{capacity}" do
|
39
|
+
@answer.knapsack(@items, capacity, :dynamic).should == profit
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/spec/quine_spec.rb
CHANGED
@@ -9,6 +9,10 @@ module QuineSpec
|
|
9
9
|
it "should return the source of the calling file" do
|
10
10
|
@answer.quine(__FILE__).should == File.read(__FILE__)
|
11
11
|
end
|
12
|
+
|
13
|
+
it "should find the calling file if not given" do
|
14
|
+
@answer.quine.should == File.read(__FILE__)
|
15
|
+
end
|
12
16
|
end
|
13
17
|
|
14
18
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: job_interview
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,11 +10,11 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date:
|
13
|
+
date: 2013-03-18 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: faker
|
17
|
-
requirement:
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
18
18
|
none: false
|
19
19
|
requirements:
|
20
20
|
- - ~>
|
@@ -22,10 +22,15 @@ dependencies:
|
|
22
22
|
version: 0.9.5
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
|
-
version_requirements:
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ~>
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
version: 0.9.5
|
26
31
|
- !ruby/object:Gem::Dependency
|
27
32
|
name: rspec
|
28
|
-
requirement:
|
33
|
+
requirement: !ruby/object:Gem::Requirement
|
29
34
|
none: false
|
30
35
|
requirements:
|
31
36
|
- - ~>
|
@@ -33,23 +38,33 @@ dependencies:
|
|
33
38
|
version: 2.9.0
|
34
39
|
type: :development
|
35
40
|
prerelease: false
|
36
|
-
version_requirements:
|
41
|
+
version_requirements: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ~>
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 2.9.0
|
37
47
|
- !ruby/object:Gem::Dependency
|
38
48
|
name: rake
|
39
|
-
requirement:
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
40
50
|
none: false
|
41
51
|
requirements:
|
42
52
|
- - ~>
|
43
53
|
- !ruby/object:Gem::Version
|
44
|
-
version: 0.
|
54
|
+
version: 10.0.3
|
45
55
|
type: :development
|
46
56
|
prerelease: false
|
47
|
-
version_requirements:
|
57
|
+
version_requirements: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ~>
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: 10.0.3
|
48
63
|
description: Programmer job interview answers, packaged for your convenience. Fibonacci,
|
49
64
|
FizzBuzz, and more! A project from BohConf 2012.
|
50
65
|
email:
|
51
66
|
- github@mgates.com
|
52
|
-
-
|
67
|
+
- jason@decomplecting.org
|
53
68
|
executables: []
|
54
69
|
extensions: []
|
55
70
|
extra_rdoc_files: []
|
@@ -59,16 +74,18 @@ files:
|
|
59
74
|
- lib/job_interview/fizz_buzz.rb
|
60
75
|
- lib/job_interview/knapsack.rb
|
61
76
|
- lib/job_interview/answer.rb
|
77
|
+
- lib/job_interview/hello_world.rb
|
62
78
|
- lib/job_interview/questions.rb
|
63
79
|
- lib/job_interview/primes.rb
|
64
80
|
- lib/job_interview/quine.rb
|
65
81
|
- spec/fibonacci_spec.rb
|
66
82
|
- spec/fizz_buzz_spec.rb
|
67
83
|
- spec/hello_world_spec.rb
|
84
|
+
- spec/knapsack_spec.rb
|
68
85
|
- spec/primes_spec.rb
|
69
86
|
- spec/questions_spec.rb
|
70
87
|
- spec/quine_spec.rb
|
71
|
-
homepage: https://github.com/
|
88
|
+
homepage: https://ruby-jokes.github.com/job_interview
|
72
89
|
licenses:
|
73
90
|
- GPLv3
|
74
91
|
post_install_message:
|
@@ -89,7 +106,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
89
106
|
version: '0'
|
90
107
|
requirements: []
|
91
108
|
rubyforge_project:
|
92
|
-
rubygems_version: 1.8.
|
109
|
+
rubygems_version: 1.8.24
|
93
110
|
signing_key:
|
94
111
|
specification_version: 3
|
95
112
|
summary: Programmer job interview answers, packaged for your convenience
|
@@ -97,6 +114,7 @@ test_files:
|
|
97
114
|
- spec/fibonacci_spec.rb
|
98
115
|
- spec/fizz_buzz_spec.rb
|
99
116
|
- spec/hello_world_spec.rb
|
117
|
+
- spec/knapsack_spec.rb
|
100
118
|
- spec/primes_spec.rb
|
101
119
|
- spec/questions_spec.rb
|
102
120
|
- spec/quine_spec.rb
|