Files
learn-python3/notebooks/beginner/exercises/classes_exercise.ipynb
Jerry Pussinen 46450175dc Initial commit
2018-05-01 13:59:37 +02:00

164 lines
3.9 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 1. Fill the missing pieces of the `Calculator` class\n",
"Fill `____` pieces of the `Calculator` implemention in order to pass the assertions."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class Calculator:\n",
" def __init__(self, var1, ____):\n",
" self.____ = var1\n",
" self.____ = _____\n",
" \n",
" def calculate_power(self):\n",
" return self.____ ** ____.____\n",
" \n",
" def calculate_sum(____, var3):\n",
" return ____.____ + ____.____ + var3"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"editable": false
},
"outputs": [],
"source": [
"calc = Calculator(2, 3)\n",
"assert calc.calculate_power() == 8\n",
"assert calc.calculate_sum(4) == 9"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 2. Finalize `StringManipulator` class\n",
"Fill `____` pieces and create implementation for `stripped_title()`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class StringManipulator:\n",
" \"\"\"____\"\"\"\n",
" \n",
" category = ____\n",
" \n",
" def __init__(self, original):\n",
" self.string = ____\n",
" \n",
" def reverse_words(self):\n",
" words = self.string.____\n",
" self.string = ' '.join(reversed(____))\n",
" \n",
" def make_title(self):\n",
" # Create implementation for this\n",
" \n",
" def get_manipulated(____):\n",
" return self._____"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"editable": false
},
"outputs": [],
"source": [
"assert StringManipulator.__doc__ == 'Docstring of StringManipulator'\n",
"assert StringManipulator.category == 'Manipulator'\n",
"\n",
"str_manip = StringManipulator('cOOL pyThON')\n",
"\n",
"str_manip.reverse_words()\n",
"assert str_manip.get_manipulated() == 'pyThON cOOL'\n",
"\n",
"str_manip.make_title()\n",
"assert str_manip.get_manipulated() == 'Python Cool'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 3. Create `Dog` class\n",
"Create `Dog` class which has the following specification:\n",
"* Dogs consume their energy by barking and gain energy by sleeping\n",
"* A fresh `Dog` instance has 10 units of energy\n",
"* `Dog` has a method `sleep` which gives 2 units of energy\n",
"* `Dog` has a method `bark` which consumes 1 unit of energy\n",
"* `Dog` has a method `get_energy` which returns the amount of energy left "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class Dog:\n",
" # Your implementation here"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"editable": false
},
"outputs": [],
"source": [
"doge = Dog()\n",
"assert doge.get_energy() == 10\n",
"\n",
"doge.bark()\n",
"doge.bark()\n",
"doge.bark()\n",
"assert doge.get_energy() == 7\n",
"\n",
"doge.sleep()\n",
"assert doge.get_energy() == 9\n",
"\n",
"another_doge = Dog()\n",
"assert another_doge.get_energy() == 10"
]
}
],
"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.5.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}