Лише одну
import re
def remove_invalid_characters(text):
"""Видаляє з рядка символи, заборонені у назві файлів для Windows."""
invalid_characters = r'[\\/*?":<>|().,«» ]'
return re.sub(invalid_characters, '', text)
def count_letter_frequency(text, letter):
"""Рахує кількість входжень заданої літери в тексті."""
count = 0
for char in text:
if char == letter:
count += 1
return count
text_input = input('Введіть текст: ').lower()
target_letter = input('Яку літеру ви хочете знайти? ').lower()
cleaned_string = remove_invalid_characters(text_input)
string_length = len(cleaned_string)
if string_length > 0:
letter_count = count_letter_frequency(cleaned_string, target_letter)
probability = letter_count / string_length
print(f"Очищений рядок: {cleaned_string}")
print(f"Довжина рядка: {string_length}")
print(f"Кількість входжень літери '{target_letter}': {letter_count}")
print(f"Ймовірність появи літери '{target_letter}': {probability:.4f}")
else:
print("Введений рядок порожній.")
Всі в українському алфавіті
import re
def remove_invalid_characters(text):
"""Видаляє з рядка символи, заборонені у назві файлів для Windows."""
invalid_characters = r'[\\/*?":<>|().,«» ]'
return re.sub(invalid_characters, '', text)
def count_letter_frequency(text, letter):
"""Рахує кількість входжень заданої літери в тексті."""
count = 0
for char in text:
if char == letter:
count += 1
return count
ukrainian_alphabet_lower = 'абвгґдеєжзиіїйклмнопрстуфхцчшщьюя'
ukrainian_alphabet_upper = 'АБВГҐДЕЄЖЗИІЇЙКЛМНОПРСТУФХЦЧШЩЬЮЯ'
ukrainian_alphabet = ukrainian_alphabet_lower + ukrainian_alphabet_upper
text_input = input('Введіть текст: ')
cleaned_string = remove_invalid_characters(text_input)
string_length = len(cleaned_string)
if string_length > 0:
letter_probabilities = {}
for letter in ukrainian_alphabet_lower:
count = count_letter_frequency(cleaned_string.lower(), letter) # Переводимо текст до нижнього регістру для нечутливості
probability = count / string_length
letter_probabilities[letter] = probability
print(f"Очищений рядок: {cleaned_string}")
print(f"Довжина рядка: {string_length}")
print("\nЙмовірність входження кожної літери українського алфавіту:")
for letter, probability in sorted(letter_probabilities.items()):
print(f"Літера '{letter}': {probability:.4f}")
else:
print("Введений рядок порожній.")
Коментарі
Дописати коментар