From 013c7bca6431c070904a6b49b3498deb8b9b2d5d Mon Sep 17 00:00:00 2001 From: Stanislav Mykhailenko Date: Thu, 27 Oct 2022 22:36:59 +0300 Subject: [PATCH] Add Lesson 2 Task 1 --- Lesson_2/task1.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 Lesson_2/task1.py diff --git a/Lesson_2/task1.py b/Lesson_2/task1.py new file mode 100644 index 0000000..8b62c5a --- /dev/null +++ b/Lesson_2/task1.py @@ -0,0 +1,16 @@ +# Lesson 2 Task 1: calculate a number of each letter occurences in a string, output it sorted by letters and by numbers +# Author: Stanislav Mykhailenko +# License: Unlicense + +# Return codes: +# 0 - OK + +occurences = {} +string = input("Enter a string: ") + +for element in string: + if element in occurences: occurences[element] = occurences[element] + 1 + else: occurences[element] = 1 + +print("Occurrences sorted by letter: " + str(dict(sorted(occurences.items())))) +print("Occurrences sorted by number: " + str(dict(sorted(occurences.items(), key=lambda item: item[1], reverse=True))))