ex06 part 2 ok
This commit is contained in:
39
d00/ex06/ft_filterstring.py
Normal file
39
d00/ex06/ft_filterstring.py
Normal file
@@ -0,0 +1,39 @@
|
||||
from ft_filter import ft_filter
|
||||
|
||||
|
||||
def get_args(argv: list[str]) -> dict[str, int]:
|
||||
"""check and return arguments"""
|
||||
|
||||
error_msg = "the arguments are bad"
|
||||
|
||||
assert len(argv) == 3, error_msg
|
||||
|
||||
try:
|
||||
msg_len = int(argv[2])
|
||||
except ValueError:
|
||||
raise AssertionError(error_msg)
|
||||
|
||||
return {"text": argv[1], "len": msg_len}
|
||||
|
||||
|
||||
def main(argv: list[str]):
|
||||
"""filter small words in string"""
|
||||
|
||||
try:
|
||||
args = get_args(argv)
|
||||
except AssertionError as err:
|
||||
print("AssertionError:", err)
|
||||
return
|
||||
|
||||
text = args["text"]
|
||||
maxlen = args["len"]
|
||||
|
||||
words = [word for word in text.split()]
|
||||
|
||||
filter_words = list(ft_filter(lambda word: len(word) > maxlen, words))
|
||||
print(filter_words)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import sys
|
||||
main(sys.argv)
|
||||
Reference in New Issue
Block a user