Advertisement
fkudinov

10. Popular Words / Вирішуємо задачі на Python CheckIO Українською

Aug 28th, 2023
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.28 KB | Source Code | 0 0
  1. # ----------------------        tasks/popular_words.py ---------------
  2.  
  3. from collections import defaultdict, Counter
  4.  
  5.  
  6. def popular_words(text: str, words: list) -> dict:
  7.  
  8.     res = {}
  9.  
  10.     text = text.lower().split()
  11.  
  12.     for word in words:
  13.         res[word] = text.count(word)
  14.  
  15.     return res
  16.  
  17.  
  18. def popular_words_list(text: str, words: list) -> dict:
  19.  
  20.     text = text.lower().split()
  21.     return dict([word, text.count(word)] for word in words)
  22.  
  23.  
  24. def popular_words_comp(text: str, words: list) -> dict:
  25.  
  26.     text = text.lower().split()
  27.     return {word: text.count(word) for word in words}
  28.  
  29.  
  30. def popular_words_perf(text: str, words: list) -> dict:
  31.  
  32.     words = set(words)
  33.  
  34.     res = dict.fromkeys(words, 0)
  35.     for word in text.lower().split():
  36.         if word in words:
  37.             res[word] = res[word] + 1
  38.  
  39.     return res
  40.  
  41.  
  42. def popular_words_all(text: str, words: list) -> dict:
  43.  
  44.     res = {}
  45.     for word in text.lower().split():
  46.         res[word] = res.get(word, 0) + 1
  47.  
  48.     return {word: res.get(word, 0) for word in words}
  49.  
  50.  
  51. def popular_words_defaultdict(text: str, words: list) -> dict:
  52.  
  53.     res = defaultdict(int)
  54.     for word in text.lower().split():
  55.         res[word] += 1
  56.  
  57.     return {word: res[word] for word in words}
  58.  
  59.  
  60. def popular_words_counter(text: str, words: list) -> dict:
  61.     res = Counter(text.lower().split())
  62.     return {word: res[word] for word in words}
  63.  
  64.  
  65.  
  66. # --------------  tests/test_popular_words.py ------------------
  67.  
  68. import pytest
  69.  
  70. from tasks.popular_words import (popular_words, popular_words_list,
  71.                                  popular_words_comp, popular_words_perf,
  72.                                  popular_words_all, popular_words_defaultdict,
  73.                                  popular_words_counter)
  74.  
  75.  
  76. @pytest.mark.parametrize("callable", [
  77.     popular_words,
  78.     popular_words_list,
  79.     popular_words_comp,
  80.     popular_words_perf,
  81.     popular_words_all,
  82.     popular_words_defaultdict,
  83.     popular_words_counter])
  84. def test_popular_words(callable):
  85.     assert callable('''
  86.    When I was One
  87.    I had just begun
  88.    When I was Two
  89.    I was nearly new
  90.    ''', ['i', 'was', 'three', 'near']) == {
  91.             'i': 4,
  92.             'was': 3,
  93.             'three': 0,
  94.             'near': 0
  95.         }
  96.  
  97.  
Tags: checkio
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement