From 5b41ff3f64be347b788b530e76fff69cddf5b34b Mon Sep 17 00:00:00 2001 From: Aly Farahat Date: Mon, 4 Apr 2016 23:21:02 +0000 Subject: [PATCH 1/2] Implemented square(), cube() and a couple of test cases --- example_homework/cubing.py | 5 ++++- example_homework/squaring.py | 5 ++++- test_example_homework.py | 9 +++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/example_homework/cubing.py b/example_homework/cubing.py index 1153359..eebff3c 100644 --- a/example_homework/cubing.py +++ b/example_homework/cubing.py @@ -27,4 +27,7 @@ def cube(x): value : number The square of `x`. """ - pass + if type(x) != list: + return x * x * x + else: + return [a * a * a for a in x ] diff --git a/example_homework/squaring.py b/example_homework/squaring.py index 344bdbc..e22b623 100644 --- a/example_homework/squaring.py +++ b/example_homework/squaring.py @@ -27,4 +27,7 @@ def square(x): value : number The square of `x`. """ - pass + if type(x) != list: + return x * x + else: + return [a * a for a in x] diff --git a/test_example_homework.py b/test_example_homework.py index 9ccafae..eddea1c 100644 --- a/test_example_homework.py +++ b/test_example_homework.py @@ -37,6 +37,15 @@ def test_two_three_four(self): def test_negative(self): self.assertEqual(square(-1), 1) + def test_complex(self): + self.assertEqual(square(1 - 1j), (1 - 1j)*(1 - 1j)) + + def test_list(self): + self.assertEqual(square([2, -2, 1j]), [4, 4, -1]) + + def test_empty_list(self): + self.assertEqual(square([]), []) + class TestCube(unittest.TestCase): """Test the `cube` function defined in `example_homework.cube`. From 5cd16a5ffbed8eb800a89dd19e340199a66951a4 Mon Sep 17 00:00:00 2001 From: Aly Farahat Date: Tue, 5 Apr 2016 00:34:38 +0000 Subject: [PATCH 2/2] Allow Recursive List Types --- example_homework/cubing.py | 2 +- example_homework/squaring.py | 2 +- test_example_homework.py | 4 ++++ 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/example_homework/cubing.py b/example_homework/cubing.py index eebff3c..ce626f1 100644 --- a/example_homework/cubing.py +++ b/example_homework/cubing.py @@ -30,4 +30,4 @@ def cube(x): if type(x) != list: return x * x * x else: - return [a * a * a for a in x ] + return [cube(a) for a in x ] diff --git a/example_homework/squaring.py b/example_homework/squaring.py index e22b623..46d5483 100644 --- a/example_homework/squaring.py +++ b/example_homework/squaring.py @@ -30,4 +30,4 @@ def square(x): if type(x) != list: return x * x else: - return [a * a for a in x] + return [square(a) for a in x] diff --git a/test_example_homework.py b/test_example_homework.py index eddea1c..3785b3d 100644 --- a/test_example_homework.py +++ b/test_example_homework.py @@ -46,7 +46,11 @@ def test_list(self): def test_empty_list(self): self.assertEqual(square([]), []) + def test_empty_list_recursive(self): + self.assertEqual(square([[],[[],[]]]), [[],[[],[]]]) + def test_list_recursive(self): + self.assertEqual(square([1, [1, 2], 3, [1, 2, [1, 3]]] ), [1, [1, 4], 9, [1, 4, [1, 9]]] ) class TestCube(unittest.TestCase): """Test the `cube` function defined in `example_homework.cube`.