Compare commits
2 Commits
42d00c7ea1
...
6f83006187
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6f83006187 | ||
|
|
cba9d9be86 |
12
d00/ex06/ft_filter.py
Normal file
12
d00/ex06/ft_filter.py
Normal file
@@ -0,0 +1,12 @@
|
||||
# filter https://docs.python.org/3.10/library/functions.html#filter
|
||||
def ft_filter(function, iterable):
|
||||
"""filter(function or None, iterable) --> filter object
|
||||
|
||||
Return an iterator yielding those items of iterable for which function(item)
|
||||
is true. If function is None, return the items that are true."""
|
||||
|
||||
# iter https://docs.python.org/3.10/library/functions.html?highlight=iter#iter
|
||||
if function is None:
|
||||
return iter([item for item in iterable if item])
|
||||
else:
|
||||
return iter([item for item in iterable if function(item)])
|
||||
19
d00/ex06/tester.py
Normal file
19
d00/ex06/tester.py
Normal file
@@ -0,0 +1,19 @@
|
||||
from ft_filter import ft_filter
|
||||
|
||||
print("- 0 filter :\n\n", filter.__doc__)
|
||||
print("\n ft_filter :\n\n", ft_filter.__doc__)
|
||||
print("")
|
||||
|
||||
msg = "Hello World"
|
||||
|
||||
print("- 1 filter :", filter(None, msg))
|
||||
print(" ft_filter :", ft_filter(None, msg))
|
||||
|
||||
print("- 2 filter :", filter(str.isupper, msg))
|
||||
print(" ft_filter :", ft_filter(str.isupper, msg))
|
||||
|
||||
print("- 3 filter :", list(filter(None, msg)))
|
||||
print(" ft_filter :", list(ft_filter(None, msg)))
|
||||
|
||||
print("- 4 filter :", list(filter(str.isupper, msg)))
|
||||
print(" ft_filter :", list(ft_filter(str.isupper, msg)))
|
||||
Reference in New Issue
Block a user