Skip to content

Instantly share code, notes, and snippets.

@andyfangdz
Last active January 21, 2016 22:28
Show Gist options
  • Save andyfangdz/3903f6bf68c7bdf9338b to your computer and use it in GitHub Desktop.
Save andyfangdz/3903f6bf68c7bdf9338b to your computer and use it in GitHub Desktop.
Alzheimer.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 76,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# 最底下有答案\n",
"\n",
"def record_step(func):\n",
" def func_wrapper(self, n):\n",
" self.actions.append((func.__name__, n))\n",
" func(self, n)\n",
" return func_wrapper\n",
" \n",
"\n",
"class BottleBuyer:\n",
" def __init__(self):\n",
" self.price = 2\n",
" self.caps_per_bottle = 4\n",
" self.empties_per_bottle = 2\n",
" self.money = 10\n",
" self.bottles = 0\n",
" self.bottles_drinked = 0\n",
" self.caps = 0\n",
" self.empties = 0\n",
" self.empty_debt = 0\n",
" self.cap_debt = 0\n",
" self.bottle_debt = 0\n",
" self.actions = []\n",
" \n",
" def print_stats(self):\n",
" template = '''Current Status:\n",
" Bottles: %d,\n",
" Caps: %d,\n",
" Empties: %d,\n",
" Money: %d,\n",
" Bottles Drinked: %d,\n",
" Cap Debt: %d,\n",
" Empty Debt: %d,\n",
" Bottle Debt: %d\n",
" '''\n",
" print(template % (\n",
" self.bottles,\n",
" self.caps,\n",
" self.empties,\n",
" self.money,\n",
" self.bottles_drinked,\n",
" self.cap_debt,\n",
" self.empty_debt,\n",
" self.bottle_debt\n",
" ))\n",
"\n",
" def print_steps(self):\n",
" for action in self.actions:\n",
" print(\"%s %s\" % action)\n",
" \n",
" @record_step\n",
" def buy(self, n):\n",
" self.money -= n * self.price\n",
" self.bottles += n\n",
" \n",
" @record_step\n",
" def drink(self, n):\n",
" self.bottles -= n\n",
" self.empties += n\n",
" self.caps += n\n",
" self.bottles_drinked += n\n",
" \n",
" @record_step\n",
" def exchange_cap(self, n):\n",
" self.caps -= n\n",
" self.bottles += n / self.caps_per_bottle\n",
" \n",
" @record_step\n",
" def exchange_empty(self, n):\n",
" self.empties -= n\n",
" self.bottles += n / self.empties_per_bottle\n",
" \n",
" @record_step\n",
" def borrow_empty(self, n):\n",
" self.empties += n\n",
" self.empty_debt += n\n",
" \n",
" @record_step\n",
" def borrow_cap(self, n):\n",
" self.caps += n\n",
" self.cap_debt += n\n",
" \n",
" @record_step\n",
" def borrow_bottle(self, n):\n",
" self.bottles += n\n",
" self.bottle_debt += n\n",
" \n",
" @record_step\n",
" def return_empty(self, n):\n",
" self.empties -= n\n",
" self.empty_debt -= n\n",
" \n",
" @record_step\n",
" def return_cap(self, n):\n",
" self.caps -= n\n",
" self.cap_debt -= n\n",
" \n",
" @record_step\n",
" def return_bottle(self, n):\n",
" self.bottles -= n\n",
" self.bottle_debt -= n\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 77,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Current Status:\n",
" Bottles: 0,\n",
" Caps: 0,\n",
" Empties: 0,\n",
" Money: 0,\n",
" Bottles Drinked: 20,\n",
" Cap Debt: 0,\n",
" Empty Debt: 0,\n",
" Bottle Debt: 0\n",
" \n",
"buy 5\n",
"drink 5\n",
"exchange_cap 4\n",
"drink 1\n",
"exchange_empty 6\n",
"drink 3\n",
"exchange_empty 2\n",
"drink 1\n",
"exchange_cap 4\n",
"exchange_empty 2\n",
"drink 2\n",
"exchange_cap 4\n",
"exchange_empty 2\n",
"drink 2\n",
"exchange_empty 2\n",
"drink 1\n",
"borrow_bottle 5\n",
"drink 5\n",
"exchange_cap 8\n",
"exchange_empty 6\n",
"return_bottle 5\n"
]
}
],
"source": [
"b = BottleBuyer()\n",
"b.buy(5)\n",
"b.drink(5)\n",
"b.exchange_cap(4)\n",
"b.drink(1)\n",
"b.exchange_empty(6)\n",
"b.drink(3)\n",
"b.exchange_empty(2)\n",
"b.drink(1)\n",
"b.exchange_cap(4)\n",
"b.exchange_empty(2)\n",
"b.drink(2)\n",
"b.exchange_cap(4)\n",
"b.exchange_empty(2)\n",
"b.drink(2)\n",
"b.exchange_empty(2)\n",
"b.drink(1)\n",
"b.borrow_bottle(5)\n",
"b.drink(5)\n",
"b.exchange_cap(8)\n",
"b.exchange_empty(6)\n",
"b.return_bottle(5)\n",
"b.print_stats()\n",
"b.print_steps()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.4.3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment