Advertisement
KateWilson

Сколько раз встреч.слово

Aug 11th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.78 KB | None | 0 0
  1. #Дан текст. Найдите слово, которое встречается чаще всего.
  2.  
  3. #Вариант 1
  4. words = input().split()
  5. d = dict()
  6.  
  7. for word in words:
  8.     if word in d:
  9.         d[word] += 1
  10.     else:
  11.         d[word] = 1
  12.  
  13. max_count_word = ""
  14. for word in d:
  15.     if max_count_word == "" or (d[word] >= d[max_count_word] and word < max_count_word):
  16.         max_count_word = word
  17. print(max_count_word)
  18.  
  19. #Вариант 2
  20. a = input().split()
  21. vocab = {}
  22. how_common = 0
  23. for i in a:
  24.     vocab[i] = vocab.get(i,0)+1 #считаем, сколько раз встречается слово
  25. for i in vocab:
  26.     if vocab[i] > how_common:
  27.         how_common = vocab[i]
  28. for i in sorted(vocab):
  29.     if vocab[i] == how_common:
  30.         print(i)
  31.         break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement