-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_own_map.py
32 lines (24 loc) · 908 Bytes
/
create_own_map.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
from typing import Callable, Iterable
# Task - create own map function
def my_map(func: Callable, iterable_object: Iterable) -> list:
"""Apply func to each element of the iterable object."""
result = []
iterator = iter(iterable_object)
while True:
try:
item = next(iterator)
except StopIteration:
break
else:
result.append(func(item))
return result
# Tests:
assert my_map(str, [1, 2, 3]) == ['1', '2', '3']
assert my_map(str, (1, 2, 3)) == ['1', '2', '3']
assert my_map(str, {1, 2, 3}) == ['1', '2', '3']
assert my_map(str, range(1, 4)) == ['1', '2', '3']
assert my_map(str, '123') == ['1', '2', '3']
assert my_map(str, []) == []
assert my_map(str, {1: 1, 2: 2, 3: 3}.keys()) == ['1', '2', '3']
assert my_map(str, {1: 1, 2: 2, 3: 3}.values()) == ['1', '2', '3']
assert my_map(str, (i for i in '123')) == ['1', '2', '3']