Skip to content

Commit 8775d8b

Browse files
committed
Solution to problem1 with rspec tests.
1 parent 6b6215f commit 8775d8b

File tree

4 files changed

+123
-0
lines changed

4 files changed

+123
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Given an array of numbers of length N, find both the minimum and maximum using less than 2 * (N - 2) comparisons.
2+
3+
class Problems
4+
def self.find_min_max_in_ary(values)
5+
min, max = 0, 0
6+
comparisons = 0
7+
comparisons += 1
8+
if(values.size == 1)
9+
min = values[0]
10+
max = values[0]
11+
return min, max
12+
end
13+
comparisons += 1
14+
if(values[0] <= values[1])
15+
min = values[0]
16+
max = values[1]
17+
else
18+
min = values[1]
19+
max = values[0]
20+
end
21+
values.shift
22+
values.shift
23+
values.each do |value|
24+
comparisons += 1
25+
if(value < min)
26+
min = value
27+
end
28+
comparisons += 1
29+
if(value > max)
30+
max = value
31+
end
32+
end
33+
[min, max, comparisons]
34+
end
35+
end
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Given a list of numbers and a number k, return whether any two numbers
2+
# from the list add up to k.
3+
#
4+
# For example, given [10, 15, 3, 7] and k of 17, return true since 10 +
5+
# 7 is 17.
6+
#
7+
# Asumptions:
8+
# The given array and k will always contain positive numbers.
9+
#
10+
class Problem1
11+
def self.solution(values, k)
12+
size = values.size
13+
if(size <= 1 || k < 1)
14+
return false
15+
elsif(size == 2)
16+
return (values[0] + values[1]) == k
17+
end
18+
(0...size).each do |i|
19+
(1...size).each do |j|
20+
if(values[i]+values[j] == k)
21+
return true
22+
end
23+
end
24+
end
25+
return false
26+
end
27+
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
describe Problems do
2+
3+
# Given an array of numbers of length N, find both the minimum and
4+
# maximum using less than 2 * (N - 2) comparisons.
5+
describe '#find_min_max_in_ary' do
6+
it 'return min and max value of array with "2 * (n-1)" comparison' do
7+
values = [9, 2, 4, 6, 10, 1]
8+
comparisons = 2 * (values.length - 2)
9+
expect(Problems.find_min_max_in_ary(values)).to eq([1, 10, comparisons])
10+
end
11+
end
12+
end
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Given a list of numbers and a number k, return whether any two numbers
2+
# from the list add up to k.
3+
#
4+
# For example, given [10, 15, 3, 7] and k of 17, return true since 10 +
5+
# 7 is 17.
6+
7+
describe Problem1 do
8+
describe '#solution' do
9+
context 'Valid inputs' do
10+
it 'Return false when array size greater than 2' do
11+
values = [10, 15, 3, 7]
12+
k = 15
13+
expect(described_class.solution(values, k)).to eq(false)
14+
end
15+
16+
it 'Return true when array size greater than 2' do
17+
values = [10, 15, 3, 7]
18+
k = 17
19+
expect(described_class.solution(values, k)).to eq(true)
20+
end
21+
22+
it 'Return false when array size is 2' do
23+
values = [10, 15]
24+
k = 15
25+
expect(described_class.solution(values, k)).to eq(false)
26+
end
27+
28+
it 'Rreturn true when array size is 2' do
29+
values = [10, 15]
30+
k = 25
31+
expect(described_class.solution(values, k)).to eq(true)
32+
end
33+
end
34+
35+
context 'Invalid Inputs' do
36+
it 'Return false when k value is negative' do
37+
values = [10, 15]
38+
k = -35
39+
expect(described_class.solution(values, k)).to eq(false)
40+
end
41+
42+
it 'Return false when array size is 1' do
43+
values = [10]
44+
k = 35
45+
expect(described_class.solution(values, k)).to eq(false)
46+
end
47+
end
48+
end
49+
end

0 commit comments

Comments
 (0)