require "Alimento/alimentos.rb" require 'benchmark' include Benchmark class Array NUM_ALIMENTOS = 5 def burbuja_for (array) #n=a.length a = Array.new a = array for i in 0...a.length-1 for j in 0...a.length-i-1 if a[j]>a[j+1] temp=a[j] a[j]=a[j+1] a[j+1]=temp end end end return a end def burbuja_each (a) tmp = 0 array = Array.new array = a # nuevo = new.Array #array = array2.split(" ") (0..array.length-1).each do |i| (0..array.length-2).each do |j| if(array[j] > array[j+1]) tmp = array[j] array[j] = array[j+1] array[j+1] = tmp end end end return array; end end class Ordenacion NUM_ALIMENTOS = 5 def self.description "clase que muestra informacion de nutrientes de un Alimento" end attr_accessor :huevo_frito, :leche_de_vaca ,:yogurt ,:cerdo ,:ternera, :pollo ,:bacalao, :atun ,:salmon, :aceita_de_oliva ,:chocolate ,:azucar ,:arroz ,:lentejas, :papas, :tomate, :cebolla ,:manzana, :platano attr_accessor :alimentos def initialize () @huevo_frito= ['Huevo',14.1,0.0,19.5] @leche_de_vaca = ['Leche de vaca',3.3,4.8,3.2] @yogurt = ['Yogurt',3.8,4.9,3.8] @cerdo = ['Cerdo',21.5,0.0,6.3] @ternera = ['Ternera',21.1,0.0,3.1] @pollo= ['Pollo',20.6,0.0,5.6] @bacalao = ['Bacalao',17.7,0.0,0.4] @atun = ['Atun',21.5,0.0,15.5] @salmon = ['Salmon',19.9,0.0,13.6] @aceita_de_oliva = ['Aceite de oliva',0.0,0.2,99.6] @chocolate = ['Chocolate',5.3,47.0,30.0] @azucar = ['Azucar',0.0,99.8,0.0] @arroz = ['Arroz',6.8,77.7,0.6] @lentejas = ['Lentejas',23.5,52.0,1.4] @papas = ['Papas',2.0,15.4,0.1] @tomate = ['Tomate',1.0,3.5,0.2] @cebolla = ['Cebolla',1.3,5.8,0.3] @manzana = ['Manzana',0.3,12.4,0.4] @platano = ['Platano',1.2,21.4,0.2] @alimentos = [@huevo_frito,@leche_de_vaca,@yogurt,@cerdo,@ternera,@pollo,@bacalao,@atun,@salmon,@aceita_de_oliva,@chocolate,@azucar,@arroz,@lentejas,@papas,@tomate,@cebolla,@manzana,@platano] @alimentor = [@huevo_frito,@leche_de_vaca,@yogurt,@cerdo,@ternera,@pollo,@bacalao] @prueba_extrema = [999999,88888888,777777777,6666666,5555555,4444444,33333,22222,11111] @prueba_extrema2 = [9,8,7,6,5,4,3,2,1] end def calculo_calorias (alimento,cantidad) #a = /\p{N}/.match (cantidad) number = cantidad.match(/\d+/)[0] #puts (number) result = [] #@alimentor.each_with_index do |alimento,i| if (number.to_f > 3.0) result = (alimento[1]*4.0) + (alimento[2]*4.0) + (alimento[3]*9.0) else result = (alimento[1]*4.0*number.to_f) + (alimento[2]*4.0*number.to_f) + (alimento[3]*9.0*number.to_f) #result.to_s end #end return (result) end def to_s i = 0 j = 0 r = [] #each_byte result = "" #a = [[11111,222222,33333333],[4444,5555,6666],[7777,88888,99999]] for i in 0...@alimentos.length-1 for j in 0...@alimentos.length-1 result << "#{@alimentos[i][j]}" result << " " end result << "\n" end return (result) end end #class #@a = [] #################################################3 #class Plato < Ordenacion class Plato attr_accessor :name, :vegetales, :frutas, :granos_integrales, :proteinas_saludables, :aceites, :aguas, :compo_nutri attr_accessor :agua, :aceite, :proteina, :cereal, :vegetal, :fruta attr_accessor :busqueda, :to_s def initialize(name, &block) #super (huevo_frito) @name = name @vegetales = [] @frutas = [] @granos_integrales = [] @proteinas_saludables = [] @aceites = [] @aguas = [] @compo_nutri = [] @sumatorio = 0.0 # super(huevo_frito,leche_de_vaca,yogurt,cerdo,ternera,pollo,bacalao,atun,salmon,aceita_de_oliva,chocolate,azucar,arroz,lentejas,papas,tomate,cebolla,manzana,platano) #super(alimentos) if block_given? if block.arity == 1 yield self else instance_eval(&block) end end end ################################################################################### #@alimentos es de la clase ordenacion #elemento = alimento #arreglar def busqueda(ingrediente,cantidad) @datos = Ordenacion.new() final = "" retorna = "" salida = "" @datos.alimentos.each_with_index do |alimento, index| if (ingrediente == alimento[0]) #puts (alimento[0]) result = @datos.calculo_calorias(alimento,cantidad) @sumatorio += result #puts (@sumatorio) a = alimento[0].to_s b = alimento[1].to_s c = alimento[2].to_s d = alimento[3].to_s e = result.to_s retorna <<" "<total:", ">avg:") do |x| # tf = x.report("con bucle for") { @prueba.burbuja_for(@a) } # tf = x.report("con bucle each") { @prueba.burbuja_each(@a) } # tf = x.report("con metodo sort") { @a.sort } # end