Skip to content

Instantly share code, notes, and snippets.

@tarsisazevedo
Created September 10, 2010 14:07
Show Gist options
  • Save tarsisazevedo/573693 to your computer and use it in GitHub Desktop.
Save tarsisazevedo/573693 to your computer and use it in GitHub Desktop.
=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
@tarsisazevedo
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment