From 3821c3cdf9e1bc4e927d1dbd552f2ddeb155e1ee Mon Sep 17 00:00:00 2001 From: Stanislav Mykhailenko Date: Fri, 6 Jan 2023 00:00:50 +0200 Subject: [PATCH] Add Lesson 3 Task 2 --- Lesson_3/task2.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 Lesson_3/task2.py diff --git a/Lesson_3/task2.py b/Lesson_3/task2.py new file mode 100644 index 0000000..05addbf --- /dev/null +++ b/Lesson_3/task2.py @@ -0,0 +1,36 @@ +# Lesson 3 Task 2: provide a menu with various functions for a string +# Author: Stanislav Mykhailenko +# License: Unlicense + +# Return codes: +# 0 - OK + +def get_vowels(string): + vowels = "AaEeIiOoUu" + result = [each for each in string if each in vowels] + return result + +def get_consonants(string): + consonants = "BbCcDdFfGgHhJjKkLlMmNnPpQqRrSsTtVvWwXxYyZz" + result = [each for each in string if each in consonants] + return result + + +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") + +if operation == str(1): + print("Sorted string: " + str(sorted(string))) +elif operation == str(2): + print("String length: " + str(len(string))) +elif operation == str(3): + print("Vowels: " + str(get_vowels(string))) +elif operation == str(4): + print("Consonants: " + str(get_consonants(string))) +elif operation == str(5): + reversed = reversed(string.split (" ")) + print("String words in reverse order: ") + print([each for each in reversed]) +elif operation == str(6): + print("Original string: " + string)