-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_fn.py
61 lines (51 loc) · 1.63 KB
/
gen_fn.py
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
# Define the `char_range` generator here
def char_range(start, stop, step=1):
stop_modifier = 1
start_code = ord(start)
stop_code = ord(stop)
if start_code > stop_code:
step *= -1
stop_modifier *= -1
for value in range(start_code,stop_code + stop_modifier,step):
yield chr(value)
# Tests to verify the implementation of char_range
# *Do not modify
##################################################
# Ensure that `char_range` is a generator function
from inspect import isgeneratorfunction
assert isgeneratorfunction(
char_range
), f"Expected char_range to be a generator function but was not."
print(char_range)
# Ensure that the result *does* includes the stop character
assert list(char_range("a", "e")) == [
"a",
"b",
"c",
"d",
"e",
], f"Expected ['a', 'b', 'c', 'd', 'e'] but got {repr(list(char_range('a', 'e')))}"
print(char_range("a","e"))
# Iterate backwards if the start code point is higher than the stop code point
assert list(char_range("e", "a")) == [
"e",
"d",
"c",
"b",
"a",
], f"Expected ['e', 'd', 'c', 'b', 'a'] but got {repr(list(char_range('e', 'a')))}"
print(char_range('e','a'))
# Properly step if a step value is provided
assert list(char_range("a", "e", 2)) == [
"a",
"c",
"e",
], f"Expected ['a', 'c', 'e'] but got {repr(list(char_range('a', 'e', 2)))}"
print(char_range('a','e',2))
# Step properly if the start code point is higher than the stop code point
assert list(char_range("e", "a", 2)) == [
"e",
"c",
"a",
], f"Expected ['e', 'c', 'a'] but got {repr(list(char_range('e', 'a', 2)))}"
print(char_range('e','a',2))