2020-07-14 09:26:50 +08:00
|
|
|
import unittest
|
2017-03-27 17:22:19 +08:00
|
|
|
|
|
|
|
|
2020-07-14 09:26:50 +08:00
|
|
|
class TestMaxProfit(unittest.TestCase):
|
2017-03-27 17:22:19 +08:00
|
|
|
|
|
|
|
def test_max_profit(self):
|
|
|
|
stock_trader = StockTrader()
|
2020-07-14 09:26:50 +08:00
|
|
|
self.assertRaises(TypeError, stock_trader.find_max_profit, None, None)
|
|
|
|
self.assertEqual(stock_trader.find_max_profit(prices=[], k=0), [])
|
2017-03-27 17:22:19 +08:00
|
|
|
prices = [5, 4, 3, 2, 1]
|
|
|
|
k = 3
|
2020-07-14 09:26:50 +08:00
|
|
|
self.assertEqual(stock_trader.find_max_profit(prices, k), (0, []))
|
2017-03-27 17:22:19 +08:00
|
|
|
prices = [2, 5, 7, 1, 4, 3, 1, 3]
|
|
|
|
profit, transactions = stock_trader.find_max_profit(prices, k)
|
2020-07-14 09:26:50 +08:00
|
|
|
self.assertEqual(profit, 10)
|
|
|
|
self.assertTrue(Transaction(Type.SELL,
|
2017-03-27 17:22:19 +08:00
|
|
|
day=7,
|
|
|
|
price=3) in transactions)
|
2020-07-14 09:26:50 +08:00
|
|
|
self.assertTrue(Transaction(Type.BUY,
|
2017-03-27 17:22:19 +08:00
|
|
|
day=6,
|
|
|
|
price=1) in transactions)
|
2020-07-14 09:26:50 +08:00
|
|
|
self.assertTrue(Transaction(Type.SELL,
|
2017-03-27 17:22:19 +08:00
|
|
|
day=4,
|
|
|
|
price=4) in transactions)
|
2020-07-14 09:26:50 +08:00
|
|
|
self.assertTrue(Transaction(Type.BUY,
|
2017-03-27 17:22:19 +08:00
|
|
|
day=3,
|
|
|
|
price=1) in transactions)
|
2020-07-14 09:26:50 +08:00
|
|
|
self.assertTrue(Transaction(Type.SELL,
|
2017-03-27 17:22:19 +08:00
|
|
|
day=2,
|
|
|
|
price=7) in transactions)
|
2020-07-14 09:26:50 +08:00
|
|
|
self.assertTrue(Transaction(Type.BUY,
|
2017-03-27 17:22:19 +08:00
|
|
|
day=0,
|
|
|
|
price=2) in transactions)
|
|
|
|
print('Success: test_max_profit')
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
test = TestMaxProfit()
|
|
|
|
test.test_max_profit()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2020-07-14 09:26:50 +08:00
|
|
|
main()
|