interactive-coding-challenges/templates/foo_solution.ipynb

162 lines
2.9 KiB
Python
Raw Normal View History

2015-07-03 11:01:32 +08:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This notebook was prepared by [Author](https://github.com/). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges)."
2015-07-03 11:01:32 +08:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Solution Notebook"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Problem: Implement foo(val), which returns val\n",
"\n",
"* [Constraints](#Constraints)\n",
"* [Test Cases](#Test-Cases)\n",
"* [Algorithm](#Algorithm)\n",
"* [Code](#Code)\n",
"* [Unit Test](#Unit-Test)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Constraints\n",
"\n",
2015-08-01 06:48:53 +08:00
"* Does foo do anything else?\n",
" * No"
2015-07-03 11:01:32 +08:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Test Cases\n",
"\n",
"* foo(val) -> val"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Algorithm\n",
"\n",
"Return the input, val\n",
" \n",
"Complexity:\n",
"* Time: O(1)\n",
"* Space: O(1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Code"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
2015-07-03 11:01:32 +08:00
"outputs": [],
"source": [
"def foo(val):\n",
" return val"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Unit Test"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
2015-07-03 11:01:32 +08:00
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting test_foo.py\n"
]
}
],
"source": [
"%%writefile test_foo.py\n",
"import unittest\n",
2015-07-03 11:01:32 +08:00
"\n",
"\n",
"class TestFoo(unittest.TestCase):\n",
2015-07-03 11:01:32 +08:00
"\n",
" def test_foo(self):\n",
" self.assertEqual(foo(None), None)\n",
" self.assertEqual(foo(0), 0)\n",
" self.assertEqual(foo('bar'), 'bar')\n",
2015-07-03 11:01:32 +08:00
" print('Success: test_foo')\n",
"\n",
2015-08-01 06:48:53 +08:00
"\n",
2015-07-03 11:01:32 +08:00
"def main():\n",
" test = TestFoo()\n",
" test.test_foo()\n",
"\n",
2015-08-01 06:48:53 +08:00
"\n",
2015-07-03 11:01:32 +08:00
"if __name__ == '__main__':\n",
" main()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
2015-07-03 11:01:32 +08:00
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Success: test_foo\n"
]
}
],
"source": [
"%run -i test_foo.py"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
2015-07-03 11:01:32 +08:00
"language": "python",
"name": "python3"
2015-07-03 11:01:32 +08:00
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
2015-07-03 11:01:32 +08:00
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.2"
2015-07-03 11:01:32 +08:00
}
},
"nbformat": 4,
"nbformat_minor": 1
2015-07-03 11:01:32 +08:00
}