interactive-coding-challenges/recursion_dynamic/max_profit_k/max_profit_challenge.ipynb

235 lines
6.5 KiB
Python

{
"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 stock prices on each consecutive day, determine the max profits with k transactions.\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",
"* Is k the number of sell transactions?\n",
" * Yes\n",
"* Can we assume the prices input is an array of ints?\n",
" * Yes\n",
"* Can we assume the inputs are valid?\n",
" * No\n",
"* If the prices are all decreasing and there is no opportunity to make a profit, do we just return 0?\n",
" * Yes\n",
"* Should the output be the max profit and days to buy and sell?\n",
" * Yes\n",
"* Can we assume this fits memory?\n",
" * Yes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Test Cases\n",
"\n",
"<pre>\n",
"* Prices: None or k: None -> None\n",
"* Prices: [] or k <= 0 -> []\n",
"* Prices: [0, -1, -2, -3, -4, -5]\n",
" * (max profit, list of transactions)\n",
" * (0, [])\n",
"* Prices: [2, 5, 7, 1, 4, 3, 1, 3] k: 3\n",
" * (max profit, list of transactions)\n",
" * (10, [Type.SELL day: 7 price: 3, \n",
" Type.BUY day: 6 price: 1, \n",
" Type.SELL day: 4 price: 4, \n",
" Type.BUY day: 3 price: 1, \n",
" Type.SELL day: 2 price: 7, \n",
" Type.BUY day: 0 price: 2])\n",
"</pre>"
]
},
{
"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": [
"from enum import Enum # Python 2 users: Run pip install enum34\n",
"\n",
"\n",
"class Type(Enum):\n",
" SELL = 0\n",
" BUY = 1\n",
"\n",
"\n",
"class Transaction(object):\n",
"\n",
" def __init__(self, type, day, price):\n",
" self.type = type\n",
" self.day = day\n",
" self.price = price\n",
"\n",
" def __eq__(self, other):\n",
" return self.type == other.type and \\\n",
" self.day == other.day and \\\n",
" self.price == other.price\n",
"\n",
" def __repr__(self):\n",
" return str(self.type) + ' day: ' + \\\n",
" str(self.day) + ' price: ' + \\\n",
" str(self.price)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class StockTrader(object):\n",
"\n",
" def find_max_profit(self, prices, k):\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": {},
"outputs": [],
"source": [
"# %load test_max_profit.py\n",
"import unittest\n",
"\n",
"\n",
"class TestMaxProfit(unittest.TestCase):\n",
"\n",
" def test_max_profit(self):\n",
" stock_trader = StockTrader()\n",
" self.assertRaises(TypeError, stock_trader.find_max_profit, None, None)\n",
" self.assertEqual(stock_trader.find_max_profit(prices=[], k=0), [])\n",
" prices = [5, 4, 3, 2, 1]\n",
" k = 3\n",
" self.assertEqual(stock_trader.find_max_profit(prices, k), (0, []))\n",
" prices = [2, 5, 7, 1, 4, 3, 1, 3]\n",
" profit, transactions = stock_trader.find_max_profit(prices, k)\n",
" self.assertEqual(profit, 10)\n",
" self.assertTrue(Transaction(Type.SELL,\n",
" day=7,\n",
" price=3) in transactions)\n",
" self.assertTrue(Transaction(Type.BUY,\n",
" day=6,\n",
" price=1) in transactions)\n",
" self.assertTrue(Transaction(Type.SELL,\n",
" day=4,\n",
" price=4) in transactions)\n",
" self.assertTrue(Transaction(Type.BUY,\n",
" day=3,\n",
" price=1) in transactions)\n",
" self.assertTrue(Transaction(Type.SELL,\n",
" day=2,\n",
" price=7) in transactions)\n",
" self.assertTrue(Transaction(Type.BUY,\n",
" day=0,\n",
" price=2) in transactions)\n",
" print('Success: test_max_profit')\n",
"\n",
"\n",
"def main():\n",
" test = TestMaxProfit()\n",
" test.test_max_profit()\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"
}
},
"nbformat": 4,
"nbformat_minor": 1
}