Skip to content

Instantly share code, notes, and snippets.

@kandra
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kandra/76730e11fdde96ada740 to your computer and use it in GitHub Desktop.
Save kandra/76730e11fdde96ada740 to your computer and use it in GitHub Desktop.
Comprar aleatoriamente hasta gastar el presupuesto S/.100
wishlist = {"libro" => 5.25,
"perfume" => 15.00,
"chocolate" => 0.27 }
presupuesto = 100.00
gastado = 0.00
comprado = {"libro" => 0,
"perfume" => 0,
"chocolate" => 0 }
elegido = ""
loop do
# Elección al azar del item a comprar
elegido = wishlist.keys.sample
puts elegido
item = wishlist[elegido]
# Me alcanza el dinero?
break if gastado + item > 100.00
# Sí me alcanza, así que compremos!
gastado = gastado + item
comprado[elegido] += 1
end
puts "He gastado S/. #{gastado}"
puts "He comprado #{comprado['libro']} libro(s), #{comprado['perfume']} perfume(s) y #{comprado['chocolate']} chocolate(s)"
puts "No pude comprar #{elegido} (S/. #{wishlist[elegido]})"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment