Skip to content

Commit c2149da

Browse files
committed
Completed problem 2 and using guard to re-run specs.
1 parent 50b6011 commit c2149da

File tree

6 files changed

+127
-5
lines changed

6 files changed

+127
-5
lines changed

Gemfile

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ source "https://rubygems.org"
55
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
66

77
# gem "rails"
8-
gem "rspec"
9-
gem "byebug"
10-
gem "rake"
8+
gem 'rspec'
9+
gem 'rake'
10+
gem 'guard-rspec'

Gemfile.lock

+35-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,40 @@
11
GEM
22
remote: https://rubygems.org/
33
specs:
4-
byebug (11.1.3)
4+
coderay (1.1.3)
55
diff-lcs (1.4.4)
6+
ffi (1.14.2)
7+
formatador (0.2.5)
8+
guard (2.16.2)
9+
formatador (>= 0.2.4)
10+
listen (>= 2.7, < 4.0)
11+
lumberjack (>= 1.0.12, < 2.0)
12+
nenv (~> 0.1)
13+
notiffany (~> 0.0)
14+
pry (>= 0.9.12)
15+
shellany (~> 0.0)
16+
thor (>= 0.18.1)
17+
guard-compat (1.2.1)
18+
guard-rspec (4.7.3)
19+
guard (~> 2.1)
20+
guard-compat (~> 1.1)
21+
rspec (>= 2.99.0, < 4.0)
22+
listen (3.3.3)
23+
rb-fsevent (~> 0.10, >= 0.10.3)
24+
rb-inotify (~> 0.9, >= 0.9.10)
25+
lumberjack (1.2.8)
26+
method_source (1.0.0)
27+
nenv (0.3.0)
28+
notiffany (0.1.3)
29+
nenv (~> 0.1)
30+
shellany (~> 0.0)
31+
pry (0.13.1)
32+
coderay (~> 1.1)
33+
method_source (~> 1.0)
634
rake (12.3.3)
35+
rb-fsevent (0.10.4)
36+
rb-inotify (0.10.1)
37+
ffi (~> 1.0)
738
rspec (3.9.0)
839
rspec-core (~> 3.9.0)
940
rspec-expectations (~> 3.9.0)
@@ -17,12 +48,14 @@ GEM
1748
diff-lcs (>= 1.2.0, < 2.0)
1849
rspec-support (~> 3.9.0)
1950
rspec-support (3.9.3)
51+
shellany (0.0.1)
52+
thor (1.0.1)
2053

2154
PLATFORMS
2255
ruby
2356

2457
DEPENDENCIES
25-
byebug
58+
guard-rspec
2659
rake
2760
rspec
2861

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# This problem was asked by Uber.
2+
#
3+
# Given an array of integers, return a new array such that each element at
4+
# index i of the new array is the product of all the numbers in the original
5+
# array except the one at i.
6+
#
7+
# For example, if our input was [1, 2, 3, 4, 5], the expected output would be
8+
# [120, 60, 40, 30, 24]. If our input was [3, 2, 1], the expected output would
9+
# be [2, 3, 6].
10+
#
11+
# Follow-up: what if you can't use division?
12+
class Problem002
13+
# O(n) space and time complexity
14+
def self.solution(values)
15+
result, size = [], values.size
16+
prefix_products = []
17+
(0...size).each do |i|
18+
if prefix_products.empty?
19+
prefix_products.append(values[i])
20+
else
21+
prefix_products.append(prefix_products[-1] * values[i])
22+
end
23+
end
24+
suffix_products = []
25+
values.reverse.each do |val|
26+
if suffix_products.empty?
27+
suffix_products << val
28+
else
29+
suffix_products << suffix_products[-1] * val
30+
end
31+
end
32+
suffix_products = suffix_products.reverse
33+
(0...size).each do |i|
34+
if(i==0)
35+
result.append(suffix_products[i + 1])
36+
elsif(i == size-1)
37+
result.append(prefix_products[i - 1])
38+
else
39+
result.append(prefix_products[i-1] * suffix_products[i+1])
40+
end
41+
end
42+
return result
43+
end
44+
45+
# O(n^2) time complexity
46+
def self.solution1(values)
47+
result, size = [], values.size
48+
(0...size).each do |i|
49+
mult = 1
50+
(0...size).each do |j|
51+
unless(i == j)
52+
mult *= values[j]
53+
end
54+
end
55+
result << mult
56+
end
57+
return result
58+
end
59+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# This problem was asked by Uber.
2+
#
3+
# Given an array of integers, return a new array such that each element at
4+
# index i of the new array is the product of all the numbers in the original
5+
# array except the one at i.
6+
#
7+
# For example, if our input was [1, 2, 3, 4, 5], the expected output would be
8+
# [120, 60, 40, 30, 24]. If our input was [3, 2, 1], the expected output would
9+
# be [2, 3, 6].
10+
#
11+
# Follow-up: what if you can't use division?
12+
13+
describe Problem002 do
14+
context 'Valid inputs' do
15+
it '#solution inc order number' do
16+
values = [1, 2, 3, 4, 5]
17+
expected_values = [120, 60, 40, 30, 24]
18+
expect(described_class.solution(values)).to eq(expected_values)
19+
end
20+
21+
it '#solution desc order number' do
22+
values = [3, 2, 1]
23+
expected_values = [2, 3, 6]
24+
expect(described_class.solution(values)).to eq(expected_values)
25+
end
26+
end
27+
end

spec/spec_helper.rb

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
require_relative '../lib/Algorithm-Sorting/bubble_sort.rb'
22
require_relative '../lib/Data-Structures-Array/my_array'
33
require_relative '../lib/Algorithm-Sorting/insertion_sort.rb'
4+
require_relative '../lib/Problems/daily-coding-problems/problem1.rb'
5+
require_relative '../lib/Problems/daily-coding-problems/problem002.rb'
6+
require_relative '../lib/Problems/daily-coding-problems/problem235.rb'
47

58
# This file was generated by the `rspec --init` command. Conventionally, all
69
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.

0 commit comments

Comments
 (0)