Advertisement
MushroomMaula

Summer Jam 2020 Code

Jul 8th, 2020
950
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.29 KB | None | 0 0
  1. """
  2. Use this file to write your solution for the Summer Code Jam 2020 Qualifier.
  3.  
  4. Important notes for submission:
  5.  
  6. - Do not change the names of the two classes included below. The test suite we
  7.  will use to test your submission relies on existence these two classes.
  8.  
  9. - You can leave the `ArticleField` class as-is if you do not wish to tackle the
  10.  advanced requirements.
  11.  
  12. - Do not include "debug"-code in your submission. This means that you should
  13.  remove all debug prints and other debug statements before you submit your
  14.  solution.
  15. """
  16. import datetime
  17. import typing
  18. from collections import defaultdict
  19.  
  20.  
  21. class ArticleField:
  22.     """The `ArticleField` class for the Advanced Requirements."""
  23.  
  24.     def __init__(self, field_type: typing.Type[typing.Any]):
  25.         self.field_type = field_type
  26.  
  27.     def __set_name__(self, owner, name):
  28.         self.name = name
  29.  
  30.     def __set__(self, instance, value: "ArticleField"):
  31.         if isinstance(value, self.field_type):
  32.             instance.__dict__[self.name] = value
  33.         else:
  34.             error_msg = "expected an instance of type '{type}' for attribute '{name}', got '{provided_type}' instead"
  35.             raise TypeError(
  36.                 error_msg.format(
  37.                     type=self.field_type.__name__,
  38.                     name=self.name,
  39.                     provided_type=type(value).__name__
  40.                 )
  41.             )
  42.  
  43.     def __get__(self, instance, owner):
  44.         return instance.__dict__[self.name]
  45.  
  46.  
  47. class Article:
  48.     """The `Article` class you need to write for the qualifier."""
  49.  
  50.     counter = 0
  51.  
  52.     def __init__(self, title: str, author: str, publication_date: datetime.datetime, content: str):
  53.         self.id = Article.counter
  54.         self.title = title
  55.         self.author = author
  56.         self.publication_date = publication_date
  57.         self._content = content
  58.         self.last_edited = None
  59.  
  60.         Article.counter += 1
  61.  
  62.     @property
  63.     def content(self):
  64.         return self._content
  65.  
  66.     @content.setter
  67.     def content(self, value):
  68.         self._content = value
  69.         self.last_edited = datetime.datetime.now()
  70.  
  71.     def short_introduction(self, n_characters):
  72.         if len(self) < n_characters:
  73.             n_characters = len(self)
  74.  
  75.         intro = []
  76.         for word in self.content.split():
  77.             if self.content.index(word) + len(word) < n_characters:
  78.                 intro.append(word)
  79.         return ' '.join(intro)
  80.  
  81.     def most_common_words(self, n_words):
  82.         counter = defaultdict(int)
  83.         word = ""
  84.         for char in self.content:
  85.             if not char.isspace() and char.isalpha():
  86.                 word += char
  87.             else:
  88.                 if not word:
  89.                     continue
  90.                 counter[word.lower()] += 1
  91.                 word = ""
  92.         else:
  93.             # add the last word
  94.             counter[word.lower()] += 1
  95.  
  96.         return dict(sorted(counter.items(), key=lambda x: x[1], reverse=True)[:n_words])
  97.  
  98.     def __repr__(self):
  99.         return f"<Article title=\"{self.title}\" author='{self.author}' publication_date='{self.publication_date.isoformat()}'>"
  100.  
  101.     def __len__(self):
  102.         return len(self.content)
  103.  
  104.     def __lt__(self, other: "Article"):
  105.         return self.publication_date < other.publication_date
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement