Created
September 10, 2010 14:07
-
-
Save tarsisazevedo/573693 to your computer and use it in GitHub Desktop.
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
=begin | |
>> custo_cds(10, 1) | |
=>custo total: 10 | |
=>custo medio: 1 | |
>> custo_cds(10, 1,2,3,1,5,3,7,1,8,2) | |
=>custo_total: 33 | |
=>custo medio: 3,3 | |
>> custo_cds(10, 1.5) | |
custo total: 15 | |
custo medio: 1.5 | |
=end | |
def custo_cds (cds, *custo) | |
if custo.length != 1 | |
@p=0 | |
somatorio_custo = custo.each {|p| @p+=p} | |
else | |
somatorio_custo = custo[0].to_f() | |
end | |
custo_total = cds*somatorio_custo | |
custo_medio = custo_total/cds | |
puts "Custo total: "+custo_total.to_s() | |
puts "Custo medio: "+custo_medio.to_s() | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Substituir as linhas 14 a 19 por:
custo_total = cds*custo.reduce{|a,b| a+b}
Ele soma cada par do array, por exemplo: custo[1]+custo[2], o resultado+custo[3], etc...
Dica do @rodrigomanhaes