Last active
July 21, 2016 00:47
-
-
Save kaiguogit/ead6f76d13a79374a34fddcf24cb39f5 to your computer and use it in GitHub Desktop.
bottle recycling
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
def recycle_bottle (full,empty,caps) | |
b = ((full + empty) / 2).floor | |
c = ((full + caps) / 4).floor | |
@fromB += b | |
@fromC += c | |
empty = (full + empty) % 2 | |
caps = (full + caps) % 4 | |
full = b + c | |
unless full == 0 | |
recycle_bottle(full,empty,caps) | |
else | |
@lb = empty | |
@lc = caps | |
end | |
end | |
def main(num) | |
begin | |
command = get_input.strip.downcase | |
clear | |
@purchasedbottle = num / 2 | |
@fromB = 0 | |
@fromC = 0 | |
@lb = 0 | |
@lc = 0 | |
recycle_bottle((command.to_i / 2).floor, 0, 0) | |
puts "Input dollar #{num}" | |
puts "From purchase #{@purchasedbottle}" | |
puts "From empty bottle #{@fromB}" | |
puts "From caps #{@fromC}" | |
puts "Leftover bottle #{@lb}" | |
puts "Leftover caps #{@lc}" | |
puts "" | |
puts "" | |
end while command != "exit" | |
end | |
def get_input | |
puts "Enter the dollor amout you want to spend" | |
puts "Enter exit to leave" | |
print ">" | |
gets | |
end | |
#http://stackoverflow.com/questions/116593/how-do-you-clear-the-irb-console | |
def clear | |
system("cls") || system("clear") || puts("\e[H\e[2J") | |
end | |
clear | |
puts main(20) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment