ex05 ok
This commit is contained in:
53
d00/ex05/building.py
Normal file
53
d00/ex05/building.py
Normal 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
0
d00/ex05/python
Normal file
Reference in New Issue
Block a user