Skip to content

Instantly share code, notes, and snippets.

@armoucar
Last active June 16, 2016 18:51
Show Gist options
  • Save armoucar/c0042168759af663a5cdfa7d4509c893 to your computer and use it in GitHub Desktop.
Save armoucar/c0042168759af663a5cdfa7d4509c893 to your computer and use it in GitHub Desktop.
# Solução rápida.
(1..100).each do |counter|
nama_team = "#{'Nama' if counter % 5 == 0 } #{'Team' if counter % 7 == 0 }".strip
nama_team = counter if nama_team.empty?
puts nama_team
end
# Solução mais elegante
# Explicação: caso um novo multiplo precise ser adicionado, tanto para 'Nama' quanto para 'Team',
# será necessário apenas adicionar o novo multiplo ao Array correspondente.
class NamaTeam
NAMA_MULTIPLES = [5]
TEAM_MULTIPLES = [7]
def multiple_result(number)
nama_team = ''
nama_team << 'Nama ' if nama_multiple? number
nama_team << 'Team' if team_multiple? number
nama_team = number.to_s if nama_team.empty?
nama_team.strip
end
private
def nama_multiple?(number)
is_there_a_multiple_in_the_list?(number, NAMA_MULTIPLES)
end
def team_multiple?(number)
is_there_a_multiple_in_the_list?(number, TEAM_MULTIPLES)
end
def is_there_a_multiple_in_the_list?(number, multiples_list)
multiples_list.select { |multiple| number % multiple == 0 }.length > 0
end
end
require 'minitest/autorun'
class TestNamaTeam < MiniTest::Unit::TestCase
def setup
@nama_team = NamaTeam.new
end
def test_multiples_of_5
assert_equal "Nama", @nama_team.multiple_result(5)
assert_equal "Nama", @nama_team.multiple_result(10)
assert_equal "Nama", @nama_team.multiple_result(20)
assert_equal "Nama", @nama_team.multiple_result(55)
assert_equal "Nama", @nama_team.multiple_result(75)
assert_equal "Nama", @nama_team.multiple_result(100)
# 5 and 7
refute_equal "Nama", @nama_team.multiple_result(35)
refute_equal "Nama", @nama_team.multiple_result(70)
end
def test_multiples_of_7
assert_equal "Team", @nama_team.multiple_result(7)
assert_equal "Team", @nama_team.multiple_result(14)
assert_equal "Team", @nama_team.multiple_result(42)
assert_equal "Team", @nama_team.multiple_result(84)
# 5 and 7
refute_equal "Team", @nama_team.multiple_result(35)
refute_equal "Team", @nama_team.multiple_result(70)
end
def test_multiples_of_5_and_7
assert_equal "Nama Team", @nama_team.multiple_result(35)
assert_equal "Nama Team", @nama_team.multiple_result(70)
end
end
nt = NamaTeam.new
(1..100).each { |i| puts nt.multiple_result(i) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment