forked from gregmalcolm/python_koans
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathabout_range.py
More file actions
80 lines (49 loc) · 1.87 KB
/
about_range.py
File metadata and controls
80 lines (49 loc) · 1.87 KB
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
from runner.koan import *
import functools
import collections.abc
class AboutRange(Koan):
"""
range(stop)
range(start, stop[, step]) is a builtin function that returns an range object from start to stop by step.
"""
def test_range_is_a_sequence(self):
self.assertEqual(__, isinstance(range(1), collections.abc.Sequence))
def test_range_is_not_an_iterator(self):
with self.assertRaises(__):
next(range(1)) # should raise a TypeError
def test_range_can_be_an_iterator(self):
iter_range = iter(range(1))
self.assertEqual(__, next(iter_range))
def test_range_can_iterate(self):
result = []
for x in range(3):
result.append(x)
self.assertEqual(__, result)
def test_range_can_have_a_length(self):
self.assertEqual(__, len(range(5)))
def test_range_starts_with_zero_by_default(self):
self.assertEqual(__, range(1).start)
def test_range_can_return_value_at_index(self):
self.assertEqual(__, range(10).index(5))
def test_range_can_start_at_another_value(self):
result = []
for x in range(1, 4):
result.append(x)
self.assertEqual(__, result)
def test_range_can_step_by_value(self):
result = []
for x in range(0, 20, 5):
result.append(x)
self.assertEqual(__, result)
def test_range_can_go_negative(self):
result = []
for x in range(0, -5, -1):
result.append(x)
self.assertEqual(__, result)
def test_range_with_map(self):
result = list(map(lambda x: x * 2, range(5)))
self.assertEqual(__, result)
def test_range_can_work_with_other_iterators(self):
def factorial(n):
return functools.reduce(lambda x, total: x * total, range(1, n + 1))
self.assertEqual(__, factorial(2)) # 2!