Update Lesson 3 Task 2 to simplify letter checks and use match-case statement

This commit is contained in:
Stanislav Mykhailenko 2023-01-15 12:48:31 +02:00
parent 062b3646a4
commit 46f0c669d1
GPG key ID: 1E95E66A9C9D6A36

View file

@ -6,13 +6,13 @@
# 0 - OK # 0 - OK
def get_vowels(string): def get_vowels(string):
vowels = "AaEeIiOoUu" vowels = "aeiou"
result = [each for each in string if each in vowels] result = [each for each in string if each.lower() in vowels]
return result return result
def get_consonants(string): def get_consonants(string):
consonants = "BbCcDdFfGgHhJjKkLlMmNnPpQqRrSsTtVvWwXxYyZz" consonants = "bcdfghjklmnpqrstvwxyz"
result = [each for each in string if each in consonants] result = [each for each in string if each.lower() in consonants]
return result return result
@ -20,17 +20,18 @@ string = input("Enter a string: ")
operation = input("Enter operation number\n1. Sort the string\n2. Count the elements number\n3. Output vowels\n4. Output consonants\n5. Output the words in reverse order\n6. Output the original string\n\nAny other input will exit the program.\n") operation = input("Enter operation number\n1. Sort the string\n2. Count the elements number\n3. Output vowels\n4. Output consonants\n5. Output the words in reverse order\n6. Output the original string\n\nAny other input will exit the program.\n")
if operation == str(1): match operation:
print("Sorted string: " + str(sorted(string))) case '1':
elif operation == str(2): print("Sorted string: " + str(sorted(string)))
print("String length: " + str(len(string))) case '2':
elif operation == str(3): print("String length: " + str(len(string)))
print("Vowels: " + str(get_vowels(string))) case '3':
elif operation == str(4): print("Vowels: " + str(get_vowels(string)))
print("Consonants: " + str(get_consonants(string))) case '4':
elif operation == str(5): print("Consonants: " + str(get_consonants(string)))
reversed = reversed(string.split (" ")) case '5':
print("String words in reverse order: ") reversed = reversed(string.split (" "))
print([each for each in reversed]) print("String words in reverse order: ")
elif operation == str(6): print([each for each in reversed])
print("Original string: " + string) case '6':
print("Original string: " + string)