Skip to content

Instantly share code, notes, and snippets.

@EdisonChendi
Created September 28, 2016 15:13
Show Gist options
  • Save EdisonChendi/6b8d737ed6e24b6546f987f84118f148 to your computer and use it in GitHub Desktop.
Save EdisonChendi/6b8d737ed6e24b6546f987f84118f148 to your computer and use it in GitHub Desktop.
Code Drinker
# -*- coding: UTF-8 -*-
class ColaDrinker(object):
def __init__(self, money, cola_price, redeem_num):
self.cola_price = cola_price
self.redeem_num = redeem_num
self.bottles = 0
self.money = money
def buy(self):
self.cola = int(self.money // self.cola_price)
self.money = self.money - (self.cola * self.cola_price)
def drink(self):
self.bottles += self.cola
drinked = self.cola
self.cola = 0
return drinked
def redeem(self):
self.cola = self.bottles // self.redeem_num
self.bottles = self.bottles % self.redeem_num
def can_redeem(self):
return (self.bottles // self.redeem_num) > 0
def run(self):
self.buy()
records = []
records.append(self.drink())
while(self.can_redeem()):
self.redeem()
records.append(self.drink())
print(records)
return records
if __name__ == "__main__":
colordrinker = ColaDrinker(money=20, cola_price=1, redeem_num=2)
print(sum(colordrinker.run()))
colordrinker = ColaDrinker(money=1000, cola_price=2, redeem_num=3)
print(sum(colordrinker.run()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment