Update Lesson 3 Task 3

This commit is contained in:
Stanislav Mykhailenko 2023-01-05 22:37:00 +02:00
parent 985e45eb08
commit 54e7f4e964
GPG key ID: 1E95E66A9C9D6A36

View file

@ -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 # Author: Stanislav Mykhailenko
# License: Unlicense # License: Unlicense
# Return codes: # Return codes:
# 0 - OK # 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: ") string = input("Enter a string: ")
if (string.count("A")): print("A - " + str(string.count("A"))) getOccurrences(string, occurrences, 0)
if (string.count("B")): print("B - " + str(string.count("B")))
if (string.count("C")): print("C - " + str(string.count("C"))) print("Occurrences sorted by letter: " + str(dict(sorted(occurrences.items()))))
if (string.count("D")): print("D - " + str(string.count("D"))) print("Occurrences sorted by number: " + str(dict(sorted(occurrences.items(), key=lambda item: item[1], reverse=True))))
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