diff --git a/Lesson_3/task3.py b/Lesson_3/task3.py index bca2111..1820798 100644 --- a/Lesson_3/task3.py +++ b/Lesson_3/task3.py @@ -1,37 +1,22 @@ -# Lesson 3 Task 3: calculate a number of each letter occurences in a string without using loops +# Lesson 3 Task 3: calculate a number of each letter occurrences in a string without using loops # Author: Stanislav Mykhailenko # License: Unlicense # Return codes: # 0 - OK -occurences = {} +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 = {} string = input("Enter a string: ") -if (string.count("A")): print("A - " + str(string.count("A"))) -if (string.count("B")): print("B - " + str(string.count("B"))) -if (string.count("C")): print("C - " + str(string.count("C"))) -if (string.count("D")): print("D - " + str(string.count("D"))) -if (string.count("E")): print("E - " + str(string.count("E"))) -if (string.count("F")): print("F - " + str(string.count("F"))) -if (string.count("G")): print("G - " + str(string.count("G"))) -if (string.count("H")): print("H - " + str(string.count("H"))) -if (string.count("I")): print("I - " + str(string.count("I"))) -if (string.count("J")): print("J - " + str(string.count("J"))) -if (string.count("K")): print("K - " + str(string.count("K"))) -if (string.count("L")): print("L - " + str(string.count("L"))) -if (string.count("M")): print("M - " + str(string.count("M"))) -if (string.count("N")): print("N - " + str(string.count("N"))) -if (string.count("O")): print("O - " + str(string.count("O"))) -if (string.count("P")): print("P - " + str(string.count("P"))) -if (string.count("Q")): print("Q - " + str(string.count("Q"))) -if (string.count("R")): print("R - " + str(string.count("R"))) -if (string.count("S")): print("S - " + str(string.count("S"))) -if (string.count("T")): print("T - " + str(string.count("T"))) -if (string.count("U")): print("U - " + str(string.count("U"))) -if (string.count("V")): print("V - " + str(string.count("V"))) -if (string.count("W")): print("W - " + str(string.count("W"))) -if (string.count("X")): print("X - " + str(string.count("X"))) -if (string.count("Y")): print("Y - " + str(string.count("Y"))) -if (string.count("Z")): print("Z - " + str(string.count("Z"))) -# and so on for each allowed Unicode character +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))))