Skip to content

Commit 520c06e

Browse files
author
Mauricio Klein
committed
Add solution for challenge #2
1 parent 251ba69 commit 520c06e

File tree

7 files changed

+123
-0
lines changed

7 files changed

+123
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
# Challenges list
22

33
- [x] [Challenge 1: Spirally traversing a matrix](challenge-1/README.md)
4+
- [x] [Challenge 2: Largest subset whose all elements are Fibonacci numbers](challenge-2/README.md)

challenge-2/Gemfile

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Gemfile
2+
source "https://rubygems.org"
3+
4+
gem "rspec"

challenge-2/Gemfile.lock

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
GEM
2+
remote: https://rubygems.org/
3+
specs:
4+
diff-lcs (1.3)
5+
rspec (3.8.0)
6+
rspec-core (~> 3.8.0)
7+
rspec-expectations (~> 3.8.0)
8+
rspec-mocks (~> 3.8.0)
9+
rspec-core (3.8.0)
10+
rspec-support (~> 3.8.0)
11+
rspec-expectations (3.8.2)
12+
diff-lcs (>= 1.2.0, < 2.0)
13+
rspec-support (~> 3.8.0)
14+
rspec-mocks (3.8.0)
15+
diff-lcs (>= 1.2.0, < 2.0)
16+
rspec-support (~> 3.8.0)
17+
rspec-support (3.8.0)
18+
19+
PLATFORMS
20+
ruby
21+
22+
DEPENDENCIES
23+
rspec
24+
25+
BUNDLED WITH
26+
1.16.6

challenge-2/README.md

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Largest subset whose all elements are Fibonacci numbers
2+
3+
The original challenge description can be found [here](https://www.geeksforgeeks.org/largest-subset-whose-all-elements-are-fibonacci-numbers/)
4+
5+
## Description
6+
7+
Given an array with positive number the task is that we find largest subset from array that contain elements which are Fibonacci numbers.
8+
9+
## Examples
10+
11+
```
12+
Input : [1, 4, 3, 9, 10, 13, 7]
13+
Output : [1, 3, 13]
14+
```
15+
16+
```
17+
Input : [0, 2, 8, 5, 2, 1, 4, 13, 23]
18+
Output : [0, 2, 8, 5, 2, 1, 13, 23]
19+
```

challenge-2/lib/solver.rb

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Solver
2+
def call(arr)
3+
return [] if arr.empty?
4+
5+
max = arr.max
6+
fib_lookup = generate_fib_lookup(max)
7+
result = []
8+
9+
arr.each do |val|
10+
if fib_lookup.has_key?(val)
11+
result << val
12+
end
13+
end
14+
15+
result
16+
end
17+
18+
private
19+
20+
def generate_fib_lookup(max)
21+
lookup = {
22+
0 => nil,
23+
1 => nil
24+
}
25+
26+
first, second = 1, 1
27+
next_val = nil
28+
29+
while second < max
30+
next_val = first + second
31+
lookup[next_val] = nil
32+
33+
first = second
34+
second = next_val
35+
end
36+
37+
lookup
38+
end
39+
end

challenge-2/spec/solver_spec.rb

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
require 'spec_helper'
2+
3+
RSpec.describe Solver do
4+
subject { described_class.new.call(arr) }
5+
6+
context "Test case 1" do
7+
let(:arr) { [1, 4, 3, 9, 10, 13, 7] }
8+
let(:expected_result) { [1, 3, 13] }
9+
10+
it { is_expected.to eq(expected_result) }
11+
end
12+
13+
context "Test case 2" do
14+
let(:arr) { [0, 2, 8, 5, 2, 1, 4, 13, 23] }
15+
let(:expected_result) { [0, 2, 8, 5, 2, 1, 13] }
16+
17+
it { is_expected.to eq(expected_result) }
18+
end
19+
20+
context "Test case 3" do
21+
let(:arr) { (50..1000).to_a }
22+
let(:expected_result) { [55, 89, 144, 233, 377, 610, 987] }
23+
24+
it { is_expected.to eq(expected_result) }
25+
end
26+
27+
context "Test case 4" do
28+
let(:arr) { [] }
29+
let(:expected_result) { [] }
30+
31+
it { is_expected.to eq(expected_result) }
32+
end
33+
end

challenge-2/spec/spec_helper.rb

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Dir["#{__dir__}/../lib/*.rb"].each { |file| require file }

0 commit comments

Comments
 (0)