7
7
# from typing import list
8
8
9
9
10
+ import unittest
11
+
12
+ # from typing import list
13
+
14
+
10
15
class Solution :
11
16
def solve (
12
17
self , prices : list [int ], index : int , fee : int , buy : int , dp : list [list [int ]]
@@ -43,19 +48,40 @@ def solve(
43
48
44
49
def maxprofit (self , prices : list [int ], fee : int ) -> int :
45
50
"""
46
- Calculate the maximum profit achievable when buying and
47
- selling stocks with a fee.
48
-
51
+ Calculate the maximum profit achievable when buying
52
+ and selling stocks with a fee.
49
53
:param prices: list of stock prices for each day.
50
54
:param fee: Transaction fee.
51
55
:return: Maximum profit achievable.
56
+ Example:
57
+ >>> s = Solution()
58
+ >>> s.maxprofit([1, 3, 2, 8, 4, 9], 2)
59
+ 8
60
+ >>> s.maxprofit([1, 4, 3, 6, 8, 2, 7], 3)
61
+ 6
52
62
"""
53
63
n = len (prices )
54
64
dp = [[- 1 ] * 2 for _ in range (n )]
55
65
56
66
return self .solve (prices , 0 , fee , 1 , dp )
57
67
58
68
69
+ class TestSolution (unittest .TestCase ):
70
+ def test_maxprofit (self ):
71
+ s = Solution ()
72
+
73
+ # Test case 1: Example input
74
+ # Test case 1: Example input
75
+ prices1 = [1 , 3 , 2 , 8 , 4 , 9 ]
76
+ fee1 = 2
77
+ assert s .maxprofit (prices1 , fee1 ) == 8 # Use assert instead of self.assertEqual
78
+
79
+ # Test case 2: Another example
80
+ prices2 = [1 , 4 , 3 , 6 , 8 , 2 , 7 ]
81
+ fee2 = 3
82
+ assert s .maxprofit (prices2 , fee2 ) == 6
83
+
84
+
59
85
def main ():
60
86
"""
61
87
This is the function for taking input and output.
@@ -73,4 +99,4 @@ def main():
73
99
import doctest
74
100
75
101
doctest .testmod ()
76
- main ()
102
+ unittest . main ()
0 commit comments