Skip to content

Commit c944dac

Browse files
PHeanEXbehrtam
authored andcommitted
list-ops: use lists as in and outputs (#413)
1 parent 02928df commit c944dac

File tree

2 files changed

+28
-43
lines changed

2 files changed

+28
-43
lines changed

exercises/list-ops/example.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
def map_clone(function, xs):
2-
return (function(elem) for elem in xs)
2+
return [function(elem) for elem in xs]
33

44

55
def length(xs):
6-
return sum(1 for x in xs)
6+
return sum(1 for _ in xs)
77

88

99
def filter_clone(function, xs):
10-
return (x for x in xs if function(x))
10+
return [x for x in xs if function(x)]
1111

1212

1313
def reverse(xs):
@@ -23,14 +23,14 @@ def append(xs, y):
2323

2424

2525
def foldl(function, xs, acc):
26-
if(len(xs) == 0):
26+
if len(xs) == 0:
2727
return acc
2828
else:
2929
return foldl(function, xs[1:], function(acc, xs[0]))
3030

3131

3232
def foldr(function, xs, acc):
33-
if(len(xs) == 0):
33+
if len(xs) == 0:
3434
return acc
3535
else:
3636
return function(xs[0], foldr(function, xs[1:], acc))
@@ -39,7 +39,7 @@ def foldr(function, xs, acc):
3939
def flat(xs):
4040
out = []
4141
for item in xs:
42-
if isinstance(item, (list, tuple)):
42+
if isinstance(item, list):
4343
out.extend(flat(item))
4444
else:
4545
out.append(item)

exercises/list-ops/list_ops_test.py

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,70 +9,60 @@ class ListOpsTest(unittest.TestCase):
99
# tests for map
1010
def test_map_square(self):
1111
self.assertEqual(
12-
(1, 4, 9, 16, 25, 36, 49, 64, 81, 100),
13-
tuple(list_ops.map_clone(
14-
lambda x: x**2, (1, 2, 3, 4, 5, 6, 7, 8, 9, 10))
15-
)
16-
)
12+
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100],
13+
list_ops.map_clone(
14+
lambda x: x**2, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]))
1715

1816
def test_map_cube(self):
1917
self.assertEqual(
20-
(-1, 8, -27, 64, -125, 216, -343, 512, -729, 1000),
21-
tuple(list_ops.map_clone(
22-
lambda x: x**3, (-1, 2, -3, 4, -5, 6, -7, 8, -9, 10))
23-
)
24-
)
18+
[-1, 8, -27, 64, -125, 216, -343, 512, -729, 1000],
19+
list_ops.map_clone(
20+
lambda x: x**3, [-1, 2, -3, 4, -5, 6, -7, 8, -9, 10]))
2521

2622
def test_map_absolute(self):
2723
self.assertEqual(
28-
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
29-
tuple(list_ops.map_clone(
30-
lambda x: abs(x), (-1, 2, -3, 4, -5, 6, -7, 8, -9, 10))
31-
)
32-
)
24+
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
25+
list_ops.map_clone(
26+
lambda x: abs(x), [-1, 2, -3, 4, -5, 6, -7, 8, -9, 10]))
3327

3428
def test_map_empty(self):
35-
self.assertEqual([], list(list_ops.map_clone(operator.index, [])))
29+
self.assertEqual([], list_ops.map_clone(operator.index, []))
3630

3731
# tests for length
3832
def test_pos_leng(self):
39-
self.assertEqual(10, list_ops.length((-1, 2, -3, 4, -5, 6, -7, 8, -9, 10)))
33+
self.assertEqual(10, list_ops.length([-1, 2, -3, 4, -5, 6, -7, 8, -9, 10]))
4034

4135
def test_empty_len(self):
4236
self.assertEqual(0, list_ops.length([]))
4337

4438
# tests for filter
4539
def test_filter_odd(self):
4640
self.assertEqual(
47-
(1, 3, 5),
48-
tuple(list_ops.filter_clone(lambda x: x % 2 != 0, [1, 2, 3, 4, 5, 6]))
49-
)
41+
[1, 3, 5],
42+
list_ops.filter_clone(lambda x: x % 2 != 0, [1, 2, 3, 4, 5, 6]))
5043

5144
def test_filter_even(self):
5245
self.assertEqual(
53-
(2, 4, 6),
54-
tuple(list_ops.filter_clone(lambda x: x % 2 == 0, [1, 2, 3, 4, 5, 6]))
55-
)
46+
[2, 4, 6],
47+
list_ops.filter_clone(lambda x: x % 2 == 0, [1, 2, 3, 4, 5, 6]))
5648

5749
# tests for reverse
5850
def test_reverse_small(self):
5951
self.assertEqual([1, 2, 3], list_ops.reverse([3, 2, 1]))
6052

6153
def test_reverse_mixed_types(self):
6254
self.assertEqual(
63-
(1, "cat", 4.0, "xyz"),
64-
list_ops.reverse(("xyz", 4.0, "cat", 1))
65-
)
55+
[1, "cat", 4.0, "xyz"],
56+
list_ops.reverse(["xyz", 4.0, "cat", 1]))
6657

6758
def test_reverse_empty(self):
68-
self.assertEqual([], list_ops.reverse(()))
59+
self.assertEqual([], list_ops.reverse([]))
6960

7061
# tests for append
7162
def test_append_tuple(self):
7263
self.assertEqual(
7364
["10", "python", "hello"],
74-
list_ops.append(["10", "python"], "hello")
75-
)
65+
list_ops.append(["10", "python"], "hello"))
7666

7767
def test_append_range(self):
7868
self.assertEqual([100, range(1000)], list_ops.append([100], range(1000)))
@@ -116,20 +106,15 @@ def test_flatten_empty(self):
116106
def test_concat_two(self):
117107
self.assertEqual(
118108
[1, 3, 5, 8, 9, 4, 5, 6],
119-
list_ops.concat([1, 3, 5, 8], [9, 4, 5, 6])
120-
)
109+
list_ops.concat([1, 3, 5, 8], [9, 4, 5, 6]))
121110

122111
def test_concat_nothing(self):
123112
self.assertEqual(
124113
["orange", "apple", "banana"],
125-
list_ops.concat(['orange', 'apple', 'banana'], None)
126-
)
114+
list_ops.concat(['orange', 'apple', 'banana'], None))
127115

128116
def test_concat_empty(self):
129-
self.assertEqual(
130-
[],
131-
list_ops.concat([], [])
132-
)
117+
self.assertEqual([], list_ops.concat([], []))
133118

134119

135120
if __name__ == '__main__':

0 commit comments

Comments
 (0)