Skip to content

Instantly share code, notes, and snippets.

@paulorodriguesxv
Last active November 13, 2017 13:00
Show Gist options
  • Save paulorodriguesxv/e4d4c721437b2b2b9d47f15635a70e58 to your computer and use it in GitHub Desktop.
Save paulorodriguesxv/e4d4c721437b2b2b9d47f15635a70e58 to your computer and use it in GitHub Desktop.
Nama First Quest
Finished in 0.002514 seconds.
------------------------------------------------------------------------------------------------------------------------
3 tests, 3 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
100% passed
------------------------------------------------------------------------------------------------------------------------
1193.32 tests/s, 1193.32 assertions/s
c:\temp\ruby>ruby first_quest.rb
user system total real
0.000000 0.000000 0.000000 ( 0.000069)
0.000000 0.000000 0.000000 ( 0.000096)
0.000000 0.000000 0.000000 ( 0.000058)
c:\temp\ruby>ruby first_quest.rb
user system total real
0.000000 0.000000 0.000000 ( 0.000067)
0.000000 0.000000 0.000000 ( 0.000081)
0.000000 0.000000 0.000000 ( 0.000047)
c:\temp\ruby>ruby first_quest.rb
user system total real
0.000000 0.000000 0.000000 ( 0.000066)
0.000000 0.000000 0.000000 ( 0.000092)
0.000000 0.000000 0.000000 ( 0.000059)
require 'benchmark'
class OneHundred
# function to generate array of names for multiples of 5, 7 and 35
def multiples_one()
a =*(1..100)
(5..100).step(5) { |n| a[n-1] = 'Nama' }
(7..100).step(7) { |n| a[n-1] = 'Team' }
(35..100).step(35) { |n| a[n-1] = 'Nama Team' }
a.join(", ")
end
# function to generate array of names for multiples of 5, 7 and 35
def multiples_two()
a = []
(1..100).each do |n|
case
when n % 35 == 0
a.push('Nama Team')
when n % 5 == 0
a.push('Nama')
when n % 7 == 0
a.push('Team')
else
a.push(n)
end
end
return a.join(", ")
end
# function to generate array of names for multiples of 5, 7 and 35
def multiples_three()
i_5 = 5
i_7 = 7
i_35 = 35
a = [*1..100]
while i_5 <= 100 do
a[i_5-1] = "Nama"
i_5 += 5
end
while i_7 <= 100 do
a[i_7-1] = "Team"
i_7 += 7
end
while i_35 <= 100 do
a[i_35-1] = "Nama Team"
i_35 += 35
end
return a.join(", ")
end
end
def execute_banchmark()
one_hundred = OneHundred.new()
Benchmark.bm do |x|
x.report { one_hundred.multiples_one() }
x.report { one_hundred.multiples_two() }
x.report { one_hundred.multiples_three() }
end
end
# execute benchmark only if directly called
if __FILE__ == $0
execute_banchmark()
end
# Test Case
require_relative "first_quest"
require "test/unit"
class TestOneHundred < Test::Unit::TestCase
def setup
@test_string = "1, 2, 3, 4, Nama, 6, Team, 8, 9, Nama, 11, 12, 13, Team, Nama, 16, 17, 18, 19, Nama, Team, 22, 23, 24, Nama, 26, 27, Team, 29, Nama, 31, 32, 33, 34, Nama Team, 36, 37, 38, 39, Nama, 41, Team, 43, 44, Nama, 46, 47, 48, Team, Nama, 51, 52, 53, 54, Nama, Team, 57, 58, 59, Nama, 61, 62, Team, 64, Nama, 66, 67, 68, 69, Nama Team, 71, 72, 73, 74, Nama, 76, Team, 78, 79, Nama, 81, 82, 83, Team, Nama, 86, 87, 88, 89, Nama, Team, 92, 93, 94, Nama, 96, 97, Team, 99, Nama"
end
def test_multiples_one
assert_equal(@test_string, OneHundred.new().multiples_one() )
end
def test_multiples_two
assert_equal(@test_string, OneHundred.new().multiples_two() )
end
def test_multiples_three
assert_equal(@test_string, OneHundred.new().multiples_three() )
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment