Last active
August 29, 2015 14:16
-
-
Save kandra/76730e11fdde96ada740 to your computer and use it in GitHub Desktop.
Comprar aleatoriamente hasta gastar el presupuesto S/.100
This file contains 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
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