-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass_ UserString.py
More file actions
52 lines (38 loc) · 1.95 KB
/
class_ UserString.py
File metadata and controls
52 lines (38 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from collections import UserString
# Всі ці класи поводяться точно як вбудовані контейнери з тією лише відмінністю, що самі дані лежать у полі data у цих класів і ви можете використовувати це поле на свій розсуд.
# Створення класу, який розширює UserString
class MyString(UserString):
# Додавання методу, який перевіряє, чи рядок є паліндромом
def is_palindrome(self):
return self.data == self.data[::-1]
# Створення екземпляру MyString
my_string = MyString("radar")
print("Рядок:", my_string)
print("Чи є паліндромом?", my_string.is_palindrome())
# Створення іншого екземпляру MyString
another_string = MyString("hello")
print("Рядок:", another_string)
print("Чи є паліндромом?", another_string.is_palindrome())
'''
Рядок: radar
Чи є паліндромом? True
Рядок: hello
Чи є паліндромом? False
'''
from collections import UserString
template = [
"You can achieve anything. All it takes is a little effort and some books.",
"This smartphone is the real deal. Big, bright screen, powerful processor, all in a small gadget.",
"Collecting infinity stones is easy if you're a born hero.",
"Mastering layout is easy. Pick up a new book and put all the exercises into practice.",
"It's not hard to fight procrastination. Just take action. In small steps.",
"Programming isn't as hard as they say.",
"Simple daily exercises will help you succeed.",
]
class Comments(UserString):
def get_limit_comment(self, max_len=15):
return f"{self.data[:max_len-3]}..."
comments = [Comments(el) for el in template]
for comment in comments:
# print(comment.get_limit_comment())
print(comment.get_limit_comment(35))