@des_de

Как подсчитать кол-во строк и вывести наибольшее число повторений?

Есть задание: "На вход программе подается некоторое количество предложений, разделенных символом табуляции ('\t'). С помощью метода строк strip() уберите пробельные символы в начале и конце каждого предложения. Далее подсчитайте, какое предложение встречается в тексте наибольшее число раз и выведите это число повторений на экран."
Пыталась прописать так:

a = (input('Введите данные: ').strip()).split()
res = {}
for i in a:
    res[i] = res.get(i, 0) +1
print(max(res.values()))

Но ничего не получилось.
Например: на вход дается "Interpreter is a computer program that performs instructions without previously compiling them into a machine language program linker is a program that takes one or more object files generated by a compiler and combines them into a single executable file library file or another object file the linker also takes care of arranging the objects in a program's address space the linker also takes care of arranging the objects in a program's address space". Корректный ответ должен быть 2, а у меня получается 7.
  • Вопрос задан
  • 170 просмотров
Решения вопроса 1
Mike_Ro
@Mike_Ro Куратор тега Python
Python, JS, WordPress, SEO, Bots, Adversting
Но ничего не получилось=(

Больше упорства! ;)

Разбиваем не по словам, а по предложениям split('\t'), strip() нужно разбивать каждое предложение, а не все строку, ну и в итоге нужно считать уже разбитые предложения:
# a = input('Введите данные: ').split('\t')
a = "Interpreter is a computer program that performs instructions without previously compiling them into a machine language program\tlinker is a program that takes one or more object files generated by a compiler and combines them into a single executable file library file or another object file\tthe linker also takes care of arranging the objects in a program's address space\tthe linker also takes care of arranging the objects in a program's address space".split('\t')

res = {}

for i in a:
    sentence = i.strip()
    res[sentence] = res.get(sentence, 0) + 1
print(max(res.values()))  # 2
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы