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_3/task3.py

23 lines
797 B
Python
Raw Normal View History

2023-01-05 20:37:00 +00:00
# Lesson 3 Task 3: calculate a number of each letter occurrences in a string without using loops
2022-11-03 17:41:32 +00:00
# Author: Stanislav Mykhailenko
# License: Unlicense
# Return codes:
# 0 - OK
2023-01-05 20:37:00 +00:00
def getOccurrences(string, occurrences, currentElement):
if string[currentElement] in occurrences: occurrences[string[currentElement]] = occurrences[string[currentElement]] + 1
else: occurrences[string[currentElement]] = 1
if currentElement + 1 < len(string):
getOccurrences(string, occurrences, currentElement + 1)
occurrences = {}
2022-11-03 17:41:32 +00:00
string = input("Enter a string: ")
2023-01-05 20:37:00 +00:00
getOccurrences(string, occurrences, 0)
print("Occurrences sorted by letter: " + str(dict(sorted(occurrences.items()))))
print("Occurrences sorted by number: " + str(dict(sorted(occurrences.items(), key=lambda item: item[1], reverse=True))))