-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
24 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,25 @@ | ||
def count_occurrences(phrase: str, letter: str) -> int: | ||
# write your code here | ||
pass | ||
""" | ||
Подсчитывает количество вхождений буквы в строку (регистронезависимо). | ||
:param phrase: Строка, в которой нужно искать букву. | ||
:param letter: Буква, чьи вхождения нужно подсчитать. | ||
:return: Количество вхождений буквы в строке. | ||
""" | ||
# Проверка корректности входных данных | ||
if not isinstance(phrase, str) or not isinstance(letter, str): | ||
raise ValueError( | ||
"Оба параметра, phrase и letter, должны быть строками." | ||
) | ||
if len(letter) != 1: | ||
raise ValueError("Параметр letter должен быть одним символом.") | ||
|
||
# Приведение строки и буквы к нижнему регистру и подсчет | ||
return phrase.lower().count(letter.lower()) | ||
|
||
|
||
# Примеры использования | ||
print(count_occurrences("letter", "t")) # 2 | ||
print(count_occurrences("abc", "a")) # 1 | ||
print(count_occurrences("abc", "d")) # 0 | ||
print(count_occurrences("ABC", "a")) # 1 |