Created
September 28, 2017 09:49
-
-
Save desmondhume/9e3eb9701cfefd224c23bf8e73655116 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'benchmark' | |
# Solution 1 | |
def solution_1(list_of_inputs) | |
list_of_inputs | |
.sort { |x, y| x<=>y } | |
.first(3) | |
.reduce(&:*) | |
end | |
# Solution 2 | |
def solution_2(list_of_inputs) | |
x, y, z = [0,0,0] | |
a, b = [0,0] | |
list_of_inputs.each do |i| | |
if i < a | |
a, b = i, a | |
elsif i > a and i < b | |
b = i | |
elsif i > x | |
x, y, z = i, x, y | |
elsif i < x and i > y | |
y, z = i, y | |
elsif i < y and i > z | |
z = i | |
end | |
end | |
a * b > x * y ? a * b * x : x * y * z | |
end | |
n = 50000 | |
Benchmark.bm do |x| | |
list = (-1_000..1_000).to_a.shuffle | |
x.report("solution 2:") { n.times do solution_2(list) end } | |
x.report("solution 1:") { n.times do solution_1(list) end } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment