Вопрос задан 05.12.2023 в 13:40. Предмет Информатика. Спрашивает Коваленко Софья.

Отдаю последние баллы, помогите пожалуйста.. Задание из ЕГЭ по информатике. Написать программу,

которая ищет в файле цифру, встречающуюся чаще всего.​
0 0
Перейти к ответам

Ответы на вопрос

Внимание! Ответы на вопросы дают живые люди. Они могут содержать ошибочную информацию, заблуждения, а также ответы могут быть сгенерированы нейросетями. Будьте внимательны. Если вы уверены, что ответ неверный, нажмите кнопку "Пожаловаться" под ответом.
Отвечает Балобанова София.

На Питоне:

path = 'F:/file.txt'

with open(path, 'r', encoding='utf-8') as f:

   data = f.read()

   f.close

a = [0 for i in range(10)]

for char in data:

   if char == '0': a[0]+=1

   elif char == '1': a[1]+=1

   elif char == '2': a[2]+=1

   elif char == '3': a[3]+=1

   elif char == '4': a[4]+=1

   elif char == '5': a[5]+=1

   elif char == '6': a[6]+=1

   elif char == '7': a[7]+=1

   elif char == '8': a[8]+=1

   elif char == '9': a[9]+=1

print(a)

digit = max(a)

if digit > 0:

   print(f'Самая часто встречающаяся цифра в файле {a.index(digit)}. Она встречается {digit} раз.')

else:

   print('В файле нет цифр.')


0 0
Отвечает нейросеть ChatGpt. Будьте внимательны, ответы нейросети могут содержать ошибочные или выдуманные данные.

Program to Find the Most Frequently Occurring Digit in a File

To write a program that finds the digit that occurs most frequently in a file, you can follow these steps:

1. Read the contents of the file. 2. Iterate through each character in the file. 3. Check if the character is a digit. 4. If it is a digit, update the count for that digit in a dictionary. 5. After iterating through all the characters, find the digit with the highest count. 6. Print the digit that occurs most frequently.

Here's an example implementation in Python:

```python def find_most_frequent_digit(file_path): digit_count = {} # Dictionary to store the count of each digit

# Read the contents of the file with open(file_path, 'r') as file: content = file.read()

# Iterate through each character in the file for char in content: if char.isdigit(): # Update the count for the digit digit_count[char] = digit_count.get(char, 0) + 1

# Find the digit with the highest count most_frequent_digit = max(digit_count, key=digit_count.get)

return most_frequent_digit

# Usage example file_path = 'path/to/your/file.txt' most_frequent_digit = find_most_frequent_digit(file_path) print(f"The most frequently occurring digit in the file is: {most_frequent_digit}") ```

Make sure to replace `'path/to/your/file.txt'` with the actual path to your file.

Please note that this is a basic implementation and assumes that the file contains only digits and other characters. If the file contains additional formatting or special characters, you may need to modify the code accordingly.

I hope this helps! Let me know if you have any further questions.

0 0

Похожие вопросы

Топ вопросов за вчера в категории Информатика

Последние заданные вопросы в категории Информатика

Задать вопрос