Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion example_homework/cubing.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@ def cube(x):
value : number
The square of `x`.
"""
pass
if type(x) != list:
return x * x * x
else:
return [cube(a) for a in x ]
5 changes: 4 additions & 1 deletion example_homework/squaring.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@ def square(x):
value : number
The square of `x`.
"""
pass
if type(x) != list:
return x * x
else:
return [square(a) for a in x]
13 changes: 13 additions & 0 deletions test_example_homework.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,20 @@ 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([]), [])

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`.

Expand Down