forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_dp.py
138 lines (102 loc) · 4.31 KB
/
test_dp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
from algorithms.dp import (
max_profit_naive, max_profit_optimized,
climb_stairs, climb_stairs_optimized,
count,
combination_sum_topdown, combination_sum_bottom_up,
edit_distance,
egg_drop,
fib_recursive, fib_list, fib_iter,
hosoya_testing,
house_robber,
Job, schedule,
Item, get_maximum_value,
longest_increasing_subsequence
)
import unittest
class TestBuySellStock(unittest.TestCase):
def test_max_profit_naive(self):
self.assertEqual(max_profit_naive([7, 1, 5, 3, 6, 4]), 5)
self.assertEqual(max_profit_naive([7, 6, 4, 3, 1]), 0)
def test_max_profit_optimized(self):
self.assertEqual(max_profit_optimized([7, 1, 5, 3, 6, 4]), 5)
self.assertEqual(max_profit_optimized([7, 6, 4, 3, 1]), 0)
class TestClimbingStairs(unittest.TestCase):
def test_climb_stairs(self):
self.assertEqual(climb_stairs(2), 2)
self.assertEqual(climb_stairs(10), 89)
def test_climb_stairs_optimized(self):
self.assertEqual(climb_stairs_optimized(2), 2)
self.assertEqual(climb_stairs_optimized(10), 89)
class TestCoinChange(unittest.TestCase):
def test_count(self):
self.assertEqual(count([1, 2, 3], 4), 4)
self.assertEqual(count([2, 5, 3, 6], 10), 5)
class TestCombinationSum(unittest.TestCase):
def test_combination_sum_topdown(self):
self.assertEqual(combination_sum_topdown([1, 2, 3], 4), 7)
def test_combination_sum_bottom_up(self):
self.assertEqual(combination_sum_bottom_up([1, 2, 3], 4), 7)
class TestEditDistance(unittest.TestCase):
def test_edit_distance(self):
self.assertEqual(edit_distance('food', 'money'), 4)
self.assertEqual(edit_distance('horse', 'ros'), 3)
class TestEggDrop(unittest.TestCase):
def test_egg_drop(self):
self.assertEqual(egg_drop(1, 2), 2)
self.assertEqual(egg_drop(2, 6), 3)
self.assertEqual(egg_drop(3, 14), 4)
class TestFib(unittest.TestCase):
def test_fib_recursive(self):
self.assertEqual(fib_recursive(10), 55)
self.assertEqual(fib_recursive(30), 832040)
def test_fib_list(self):
self.assertEqual(fib_list(10), 55)
self.assertEqual(fib_list(30), 832040)
def test_fib_iter(self):
self.assertEqual(fib_iter(10), 55)
self.assertEqual(fib_iter(30), 832040)
class TestHosoyaTriangle(unittest.TestCase):
"""[summary]
Test for the file hosoya_triangle
Arguments:
unittest {[type]} -- [description]
"""
def test_hosoya(self):
self.assertEqual([1], hosoya_testing(1))
self.assertEqual([1,
1, 1,
2, 1, 2,
3, 2, 2, 3,
5, 3, 4, 3, 5,
8, 5, 6, 6, 5, 8],
hosoya_testing(6))
self.assertEqual([1,
1, 1,
2, 1, 2,
3, 2, 2, 3,
5, 3, 4, 3, 5,
8, 5, 6, 6, 5, 8,
13, 8, 10, 9, 10, 8, 13,
21, 13, 16, 15, 15, 16, 13, 21,
34, 21, 26, 24, 25, 24, 26, 21, 34,
55, 34, 42, 39, 40, 40, 39, 42, 34, 55],
hosoya_testing(10))
class TestHouseRobber(unittest.TestCase):
def test_house_robber(self):
self.assertEqual(44, house_robber([1, 2, 16, 3, 15, 3, 12, 1]))
class TestJobScheduling(unittest.TestCase):
def test_job_scheduling(self):
job1, job2 = Job(1, 3, 2), Job(2, 3, 4)
self.assertEqual(4, schedule([job1, job2]))
class TestKnapsack(unittest.TestCase):
def test_get_maximum_value(self):
item1, item2, item3 = Item(60, 10), Item(100, 20), Item(120, 30)
self.assertEqual(220, get_maximum_value([item1, item2, item3], 50))
item1, item2, item3, item4 = Item(60, 5), Item(50, 3), Item(70, 4), Item(30, 2)
self.assertEqual(80, get_maximum_value([item1, item2, item3, item4], 5))
class TestLongestIncreasingSubsequence(unittest.TestCase):
def test_longest_increasing_subsequence(self):
sequence = [1, 101, 10, 2, 3, 100, 4, 6, 2]
self.assertEqual(5, longest_increasing_subsequence(sequence))
if __name__ == '__main__':
unittest.main()