Last active
August 9, 2017 17:06
-
-
Save WallasFaria/1eb8296ef5543aee23263cb4d435a52f to your computer and use it in GitHub Desktop.
Exercício: maior produto de quatro números consecutivos na diagonal
This file contains hidden or 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 'forwardable' | |
class Matriz | |
include Enumerable | |
extend Forwardable | |
def_delegators :@valores, :each, :[], :size | |
def initialize(n_linhas, n_colunas) | |
raise ArgumentError, 'linhas e colunas devem do tipo inteiro' unless n_linhas.is_a?(Integer) && n_colunas.is_a?(Integer) | |
raise ArgumentError, 'linhas e colunas devem ser maior que 4' unless n_linhas > 4 && n_colunas > 4 | |
@valores = preencher(n_linhas, n_colunas) | |
end | |
def maiores_na_diagonal() | |
maior_atual = 0 | |
resultado = '' | |
(@valores.size - 4).times do |i| | |
@valores[i].each_index do |j| | |
if j < @valores[i].size - 4 | |
soma = @valores[i][j] + @valores[i+1][j+1] + @valores[i+2][j+2] + @valores[i+3][j+3] | |
if soma > maior_atual | |
maior_atual = soma | |
resultado = "#{i}:#{j} + #{i+1}:#{j+1} + #{i+2}:#{j+2} + #{i+3}:#{j+3} = #{soma}" | |
end | |
end | |
if j >= 3 | |
soma = @valores[i][j] + @valores[i+1][j-1] + @valores[i+2][j-2] + @valores[i+3][j-3] | |
if soma > maior_atual | |
maior_atual = soma | |
resultado = "#{i}:#{j} + #{i+1}:#{j-1} + #{i+2}:#{j-2} + #{i+3}:#{j-3} = #{soma}" | |
end | |
end | |
end | |
end | |
resultado | |
end | |
private | |
def preencher(linhas, colunas) | |
[*1..linhas].map{ [*1..colunas].map{ Random.rand(1..100) } } | |
end | |
end | |
if __FILE__ == $0 | |
m = Matriz.new(10, 10) | |
m.each { |l| l.each { |c| printf("%0.3d ", c) }; puts '' } | |
puts m.maiores_na_diagonal | |
end |
This file contains hidden or 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 './matriz' | |
RSpec.describe Matriz do | |
it 'deve ter número de linhas e colunas do tipo inteiro' do | |
expect { Matriz.new('a', 5) }.to raise_error(ArgumentError) | |
expect { Matriz.new(5, '4') }.to raise_error(ArgumentError) | |
end | |
it 'deve ter número de linhas e colunas maior que 4' do | |
expect { Matriz.new(4, 5) }.to raise_error(ArgumentError) | |
expect { Matriz.new(5, 4) }.to raise_error(ArgumentError) | |
end | |
it 'verifica se o tamaho da matriz está correto' do | |
m = Matriz.new(5, 6) | |
expect(m.size).to eq 5 | |
m.each { |l| expect(l.size).to eq 6 } | |
end | |
describe '#maiores_na_diagonal' do | |
before do | |
allow_any_instance_of(Matriz).to receive(:preencher) do | |
[ | |
[683, 405, 838, 532, 831, 847, 136, 64, 19, 994], | |
[182, 813, 731, 520, 61, 327, 826, 438, 169, 215], | |
[178, 165, 365, 3, 186, 639, 384, 44, 452, 593], | |
[294, 406, 162, 214, 510, 828, 692, 702, 932, 124], | |
[285, 549, 963, 811, 283, 355, 573, 409, 540, 255], | |
[791, 507, 863, 991, 784, 995, 347, 242, 952, 345], | |
[787, 571, 796, 302, 856, 324, 850, 62, 36, 102], | |
[388, 563, 25, 996, 33, 253, 701, 545, 814, 988], | |
[380, 504, 959, 369, 829, 646, 896, 137, 734, 480], | |
[885, 207, 412, 104, 876, 592, 426, 371, 301, 497] | |
] | |
end | |
end | |
it 'retorna o maior produto de quatro numeros consecutivos na diagonal' do | |
m = Matriz.new(10, 10) | |
expect(m.maiores_na_diagonal).to eq('5:5 + 6:4 + 7:3 + 8:2 = 3806') | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment