mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-01-12 20:38:02 +00:00
@@ -124,9 +124,7 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class StockTrader(object):\n",
|
||||
@@ -153,45 +151,41 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# %load test_max_profit.py\n",
|
||||
"from nose.tools import assert_equal\n",
|
||||
"from nose.tools import assert_raises\n",
|
||||
"from nose.tools import assert_true\n",
|
||||
"import unittest\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class TestMaxProfit(object):\n",
|
||||
"class TestMaxProfit(unittest.TestCase):\n",
|
||||
"\n",
|
||||
" def test_max_profit(self):\n",
|
||||
" stock_trader = StockTrader()\n",
|
||||
" assert_raises(TypeError, stock_trader.find_max_profit, None, None)\n",
|
||||
" assert_equal(stock_trader.find_max_profit(prices=[], k=0), [])\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",
|
||||
" assert_equal(stock_trader.find_max_profit(prices, k), (0, []))\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",
|
||||
" assert_equal(profit, 10)\n",
|
||||
" assert_true(Transaction(Type.SELL,\n",
|
||||
" self.assertEqual(profit, 10)\n",
|
||||
" self.assertTrue(Transaction(Type.SELL,\n",
|
||||
" day=7,\n",
|
||||
" price=3) in transactions)\n",
|
||||
" assert_true(Transaction(Type.BUY,\n",
|
||||
" self.assertTrue(Transaction(Type.BUY,\n",
|
||||
" day=6,\n",
|
||||
" price=1) in transactions)\n",
|
||||
" assert_true(Transaction(Type.SELL,\n",
|
||||
" self.assertTrue(Transaction(Type.SELL,\n",
|
||||
" day=4,\n",
|
||||
" price=4) in transactions)\n",
|
||||
" assert_true(Transaction(Type.BUY,\n",
|
||||
" self.assertTrue(Transaction(Type.BUY,\n",
|
||||
" day=3,\n",
|
||||
" price=1) in transactions)\n",
|
||||
" assert_true(Transaction(Type.SELL,\n",
|
||||
" self.assertTrue(Transaction(Type.SELL,\n",
|
||||
" day=2,\n",
|
||||
" price=7) in transactions)\n",
|
||||
" assert_true(Transaction(Type.BUY,\n",
|
||||
" self.assertTrue(Transaction(Type.BUY,\n",
|
||||
" day=0,\n",
|
||||
" price=2) in transactions)\n",
|
||||
" print('Success: test_max_profit')\n",
|
||||
@@ -232,9 +226,9 @@
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.5.0"
|
||||
"version": "3.7.2"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
"nbformat_minor": 1
|
||||
}
|
||||
|
||||
@@ -123,9 +123,7 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from enum import Enum # Python 2 users: Run pip install enum34\n",
|
||||
@@ -157,9 +155,7 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import sys\n",
|
||||
@@ -241,9 +237,7 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
@@ -255,39 +249,37 @@
|
||||
],
|
||||
"source": [
|
||||
"%%writefile test_max_profit.py\n",
|
||||
"from nose.tools import assert_equal\n",
|
||||
"from nose.tools import assert_raises\n",
|
||||
"from nose.tools import assert_true\n",
|
||||
"import unittest\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class TestMaxProfit(object):\n",
|
||||
"class TestMaxProfit(unittest.TestCase):\n",
|
||||
"\n",
|
||||
" def test_max_profit(self):\n",
|
||||
" stock_trader = StockTrader()\n",
|
||||
" assert_raises(TypeError, stock_trader.find_max_profit, None, None)\n",
|
||||
" assert_equal(stock_trader.find_max_profit(prices=[], k=0), [])\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",
|
||||
" assert_equal(stock_trader.find_max_profit(prices, k), (0, []))\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",
|
||||
" assert_equal(profit, 10)\n",
|
||||
" assert_true(Transaction(Type.SELL,\n",
|
||||
" self.assertEqual(profit, 10)\n",
|
||||
" self.assertTrue(Transaction(Type.SELL,\n",
|
||||
" day=7,\n",
|
||||
" price=3) in transactions)\n",
|
||||
" assert_true(Transaction(Type.BUY,\n",
|
||||
" self.assertTrue(Transaction(Type.BUY,\n",
|
||||
" day=6,\n",
|
||||
" price=1) in transactions)\n",
|
||||
" assert_true(Transaction(Type.SELL,\n",
|
||||
" self.assertTrue(Transaction(Type.SELL,\n",
|
||||
" day=4,\n",
|
||||
" price=4) in transactions)\n",
|
||||
" assert_true(Transaction(Type.BUY,\n",
|
||||
" self.assertTrue(Transaction(Type.BUY,\n",
|
||||
" day=3,\n",
|
||||
" price=1) in transactions)\n",
|
||||
" assert_true(Transaction(Type.SELL,\n",
|
||||
" self.assertTrue(Transaction(Type.SELL,\n",
|
||||
" day=2,\n",
|
||||
" price=7) in transactions)\n",
|
||||
" assert_true(Transaction(Type.BUY,\n",
|
||||
" self.assertTrue(Transaction(Type.BUY,\n",
|
||||
" day=0,\n",
|
||||
" price=2) in transactions)\n",
|
||||
" print('Success: test_max_profit')\n",
|
||||
@@ -305,9 +297,7 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
@@ -338,9 +328,9 @@
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.4.3"
|
||||
"version": "3.7.2"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
"nbformat_minor": 1
|
||||
}
|
||||
|
||||
@@ -1,36 +1,34 @@
|
||||
from nose.tools import assert_equal
|
||||
from nose.tools import assert_raises
|
||||
from nose.tools import assert_true
|
||||
import unittest
|
||||
|
||||
|
||||
class TestMaxProfit(object):
|
||||
class TestMaxProfit(unittest.TestCase):
|
||||
|
||||
def test_max_profit(self):
|
||||
stock_trader = StockTrader()
|
||||
assert_raises(TypeError, stock_trader.find_max_profit, None, None)
|
||||
assert_equal(stock_trader.find_max_profit(prices=[], k=0), [])
|
||||
self.assertRaises(TypeError, stock_trader.find_max_profit, None, None)
|
||||
self.assertEqual(stock_trader.find_max_profit(prices=[], k=0), [])
|
||||
prices = [5, 4, 3, 2, 1]
|
||||
k = 3
|
||||
assert_equal(stock_trader.find_max_profit(prices, k), (0, []))
|
||||
self.assertEqual(stock_trader.find_max_profit(prices, k), (0, []))
|
||||
prices = [2, 5, 7, 1, 4, 3, 1, 3]
|
||||
profit, transactions = stock_trader.find_max_profit(prices, k)
|
||||
assert_equal(profit, 10)
|
||||
assert_true(Transaction(Type.SELL,
|
||||
self.assertEqual(profit, 10)
|
||||
self.assertTrue(Transaction(Type.SELL,
|
||||
day=7,
|
||||
price=3) in transactions)
|
||||
assert_true(Transaction(Type.BUY,
|
||||
self.assertTrue(Transaction(Type.BUY,
|
||||
day=6,
|
||||
price=1) in transactions)
|
||||
assert_true(Transaction(Type.SELL,
|
||||
self.assertTrue(Transaction(Type.SELL,
|
||||
day=4,
|
||||
price=4) in transactions)
|
||||
assert_true(Transaction(Type.BUY,
|
||||
self.assertTrue(Transaction(Type.BUY,
|
||||
day=3,
|
||||
price=1) in transactions)
|
||||
assert_true(Transaction(Type.SELL,
|
||||
self.assertTrue(Transaction(Type.SELL,
|
||||
day=2,
|
||||
price=7) in transactions)
|
||||
assert_true(Transaction(Type.BUY,
|
||||
self.assertTrue(Transaction(Type.BUY,
|
||||
day=0,
|
||||
price=2) in transactions)
|
||||
print('Success: test_max_profit')
|
||||
@@ -42,4 +40,4 @@ def main():
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user