This commit is contained in:
hugogogo
2025-10-26 19:48:26 +01:00
parent bce4015ad4
commit 42d00c7ea1
2 changed files with 53 additions and 0 deletions

53
d00/ex05/building.py Normal file
View File

@@ -0,0 +1,53 @@
def get_message(argv: list[str]) -> str:
"""check arguments and return string"""
# https://docs.python.org/3.10/reference/simple_stmts.html?highlight=assert#the-assert-statement
assert len(argv) < 3, "too much arguments"
if len(argv) == 2:
return argv[1]
# stdin https://docs.python.org/3/library/sys.html#sys.stdin
print("What is the text to count?")
msg = sys.stdin.readline()
return msg
def main(argv: list[str]):
"""displays stats on string"""
try:
msg = get_message(argv)
except AssertionError as err:
print("AssertionError:", err)
return
char_count = len(msg)
print("The text contains", char_count,"characters:")
# filter() https://docs.python.org/3.10/library/functions.html#filter
upper_count = len(list(filter(str.isupper, msg)))
print(upper_count, "upper letters")
# generator expressions https://docs.python.org/3.10/reference/expressions.html#generator-expressions
lower_count = sum(1 for char in msg if char.islower())
print(lower_count, "lower letters")
# in https://docs.python.org/3/reference/expressions.html#in
punctuation = r"""!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"""
punctuation_count = sum(1 for char in msg if char in punctuation)
print(punctuation_count, "punctuation marks")
# array.count() https://docs.python.org/3.10/library/array.html?highlight=count#array.array.count
space_count = msg.count(" ") + msg.count("\n")
print(space_count, "spaces")
# list comprehension https://docs.python.org/3.10/tutorial/datastructures.html#list-comprehensions
digits_count = len([char for char in msg if char.isdigit()])
print(digits_count, "digits")
if __name__ == "__main__":
import sys
main(sys.argv)

0
d00/ex05/python Normal file
View File