This repository has been archived on 2024-10-24. You can view files and clone it, but cannot push or open issues or pull requests.
NG_2022_Stanislav_Mykhailenko/Lesson_2/task1.py
Stanislav Mykhailenko 013c7bca64
Add Lesson 2 Task 1
2022-10-27 22:36:59 +03:00

16 lines
580 B
Python

# 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))))