interactive-coding-challenges/recursion_dynamic/matrix_mult/find_min_cost_challenge.ipynb

180 lines
4.2 KiB
Python
Raw Normal View History

2017-03-27 17:23:15 +08:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This notebook was prepared by [Donne Martin](https://github.com/donnemartin). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Challenge Notebook"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Problem: Given a list of 2x2 matrices, minimize the cost of matrix multiplication.\n",
"\n",
"* [Constraints](#Constraints)\n",
"* [Test Cases](#Test-Cases)\n",
"* [Algorithm](#Algorithm)\n",
"* [Code](#Code)\n",
"* [Unit Test](#Unit-Test)\n",
"* [Solution Notebook](#Solution-Notebook)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Constraints\n",
"\n",
"* Do we just want to calculate the cost and not list the actual order of operations?\n",
" * Yes\n",
"* Can we assume the inputs are valid?\n",
" * No\n",
"* Can we assume this fits memory?\n",
" * Yes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Test Cases\n",
"\n",
"* None -> Exception\n",
"* [] -> 0\n",
"* [Matrix(2, 3), Matrix(3, 6), Matrix(6, 4), Matrix(4, 5)] -> 124"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Algorithm\n",
"\n",
"Refer to the [Solution Notebook](). If you are stuck and need a hint, the solution notebook's algorithm discussion might be a good place to start."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Code"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"class Matrix(object):\n",
"\n",
" def __init__(self, first, second):\n",
" self.first = first\n",
" self.second = second"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
2017-03-27 17:23:15 +08:00
"outputs": [],
"source": [
"class MatrixMultiplicationCost(object):\n",
"\n",
" def find_min_cost(self, matrices):\n",
" # TODO: Implement me\n",
" pass"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Unit Test"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**The following unit test is expected to fail until you solve the challenge.**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
2017-03-27 17:23:15 +08:00
"outputs": [],
"source": [
"# %load test_find_min_cost.py\n",
"import unittest\n",
2017-03-27 17:23:15 +08:00
"\n",
"\n",
"class TestMatrixMultiplicationCost(unittest.TestCase):\n",
2017-03-27 17:23:15 +08:00
"\n",
" def test_find_min_cost(self):\n",
" matrix_mult_cost = MatrixMultiplicationCost()\n",
" self.assertRaises(TypeError, matrix_mult_cost.find_min_cost, None)\n",
" self.assertEqual(matrix_mult_cost.find_min_cost([]), 0)\n",
2017-03-27 17:23:15 +08:00
" matrices = [Matrix(2, 3),\n",
" Matrix(3, 6),\n",
" Matrix(6, 4),\n",
" Matrix(4, 5)]\n",
" expected_cost = 124\n",
" self.assertEqual(matrix_mult_cost.find_min_cost(matrices), expected_cost)\n",
2017-03-27 17:23:15 +08:00
" print('Success: test_find_min_cost')\n",
"\n",
"\n",
"def main():\n",
" test = TestMatrixMultiplicationCost()\n",
" test.test_find_min_cost()\n",
"\n",
"\n",
"if __name__ == '__main__':\n",
" main()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Solution Notebook\n",
"\n",
"Review the [Solution Notebook]() for a discussion on algorithms and code solutions."
]
}
],
"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.7.2"
2017-03-27 17:23:15 +08:00
}
},
"nbformat": 4,
"nbformat_minor": 1
2017-03-27 17:23:15 +08:00
}