diff options
| author | mo khan <mo@mokhan.ca> | 2021-11-21 18:02:26 -0700 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2021-11-21 18:02:26 -0700 |
| commit | e1316a4ac1aa7726336b3a07eeef25de8290e6c6 (patch) | |
| tree | 9121b87d3edbb3636d4cb065eee0c576e6fd777a /exercises | |
| parent | f035459b7bff6ac141d63d6b0a00f07ea336d84c (diff) | |
write recursive solution in python
Diffstat (limited to 'exercises')
| -rw-r--r-- | exercises/15.1/rod_cutting.py | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/exercises/15.1/rod_cutting.py b/exercises/15.1/rod_cutting.py new file mode 100644 index 0000000..ec24930 --- /dev/null +++ b/exercises/15.1/rod_cutting.py @@ -0,0 +1,21 @@ +import unittest + +def cutRod(p, n): + if n == 0: + return 0 + q = -1 + for i in range(1, n+1): + q = max(q, p[i] + cutRod(p, n-i)) + return q + +class TestRodCutting(unittest.TestCase): + def setUp(self): + self.p = [0, 1, 5, 8, 9, 10, 17, 17, 20, 24, 30] + self.r = [0, 1, 5, 8, 10, 13, 17, 18, 22, 25, 30] + + def test_recursive(self): + for n in range(10): + self.assertEqual(self.r[n], cutRod(self.p, n)) + +if __name__ == '__main__': + unittest.main() |
