From 146ad63aec0fde7d7649594cbcca0789bf6e0396 Mon Sep 17 00:00:00 2001 From: Maria Mashkovtseva Date: Mon, 17 Jun 2024 13:03:32 +0300 Subject: [PATCH 1/2] 3 practice --- tasks/practice1/practice1.py | 4 ++-- tasks/practice2/practice2.py | 43 +++++++++++++++++++++++++++++++++++- tasks/practice3/practice3.py | 38 ++++++++++++++++++++++++++++--- 3 files changed, 79 insertions(+), 6 deletions(-) diff --git a/tasks/practice1/practice1.py b/tasks/practice1/practice1.py index 030da70..48f8882 100644 --- a/tasks/practice1/practice1.py +++ b/tasks/practice1/practice1.py @@ -10,7 +10,7 @@ def concatenate_strings(a: str, b: str) -> str: # пиши свой код здесь - return result + return a + b def calculate_salary(total_compensation: int) -> float: @@ -24,4 +24,4 @@ def calculate_salary(total_compensation: int) -> float: # пиши свой код здесь - return result + return total_compensation * 0.87 diff --git a/tasks/practice2/practice2.py b/tasks/practice2/practice2.py index 008f6d1..3edad8c 100644 --- a/tasks/practice2/practice2.py +++ b/tasks/practice2/practice2.py @@ -1,4 +1,5 @@ from typing import Iterable +from random import uniform UNCULTURED_WORDS = ('kotleta', 'pirog') @@ -13,6 +14,7 @@ def greet_user(name: str) -> str: """ # пиши код здесь + greeting = "Привет, " + name + "! Рада знакомству :)" return greeting @@ -29,6 +31,9 @@ def get_amount() -> float: """ # пиши код здесь + + amount = round(uniform(100, 1000000), 2) + return amount @@ -43,6 +48,15 @@ def is_phone_correct(phone_number: str) -> bool: """ # пиши код здесь + + result = False + + if len(phone_number) == 12 and phone_number[:2] == "+7": + result = True + for x in range(2, len(phone_number)): + if not phone_number[x].isdigit(): + result = False + return result @@ -59,6 +73,12 @@ def is_amount_correct(current_amount: float, transfer_amount: str) -> bool: """ # пиши код здесь + + result = False + + if current_amount >= float(transfer_amount): + result = True + return result @@ -78,7 +98,14 @@ def moderate_text(text: str, uncultured_words: Iterable[str]) -> str: """ # пиши код здесь - return result + text = " ".join(text.split()) + text = text.capitalize() + text = text.replace("'", "") + text = text.replace('"', '') + for s in uncultured_words: + text = text.replace(s, "#"*len(s)) + + return text def create_request_for_loan(user_info: str) -> str: @@ -101,4 +128,18 @@ def create_request_for_loan(user_info: str) -> str: """ # пиши код здесь + + info = user_info.split(',') + surname = info[0] + name = info[1] + patronymic = info[2] + date = info[3] + sum = info[4] + + result = (f"Фамилия: {surname}\n" + + f"Имя: {name}\n" + + f"Отчество: {patronymic}\n" + + f"Дата рождения: {date}\n" + + f"Запрошенная сумма: {sum}") + return result diff --git a/tasks/practice3/practice3.py b/tasks/practice3/practice3.py index 9115c9c..ad84ed6 100644 --- a/tasks/practice3/practice3.py +++ b/tasks/practice3/practice3.py @@ -1,3 +1,4 @@ +import csv from pathlib import Path from typing import Dict, Any, List, Optional @@ -27,8 +28,23 @@ def count_words(text: str) -> Dict[str, int]: """ # пиши свой код здесь + words = text.split() + dictionary = {} + for word in words: + if any(x.isdigit() for x in word): + continue + w = "" + for letter in word: + if letter.isalpha(): + w += letter.lower() - return {} + if w != "": + if w in dictionary: + dictionary[w] += 1 + else: + dictionary[w] = 1 + + return dictionary def exp_list(numbers: List[int], exp: int) -> List[int]: @@ -41,8 +57,10 @@ def exp_list(numbers: List[int], exp: int) -> List[int]: """ # пиши свой код здесь + for x in range(len(numbers)): + numbers[x] = numbers[x] ** exp - return [] + return numbers def get_cashback(operations: List[Dict[str, Any]], special_category: List[str]) -> float: @@ -57,6 +75,13 @@ def get_cashback(operations: List[Dict[str, Any]], special_category: List[str]) :param special_category: список категорий повышенного кешбека :return: размер кешбека """ + result = 0 + + for operation in operations: + if operation['category'] in special_category: + result += operation['amount'] * 0.05 + else: + result += operation['amount'] * 0.01 return result @@ -100,5 +125,12 @@ def csv_reader(header: str) -> int: """ # пиши свой код здесь + path = get_path_to_file() + result = set() + + with open(path, 'r') as f: + read = csv.DictReader(f) + for x in read: + result.add(x[header]) - return 0 + return len(result) From ab0c2341097b143e3f28acffc2e247c5823a9f65 Mon Sep 17 00:00:00 2001 From: Maria Mashkovtseva Date: Mon, 17 Jun 2024 20:37:23 +0300 Subject: [PATCH 2/2] 4, 5 practice --- tasks/practice4/practice4.py | 14 ++++++++++++++ tasks/practice5/employee.py | 23 +++++++++++++++++++++++ tasks/practice5/team.py | 20 ++++++++++++++++++++ 3 files changed, 57 insertions(+) diff --git a/tasks/practice4/practice4.py b/tasks/practice4/practice4.py index a7d6b8d..a3c87e5 100644 --- a/tasks/practice4/practice4.py +++ b/tasks/practice4/practice4.py @@ -40,4 +40,18 @@ def search_phone(content: Any, name: str) -> Optional[str]: # пиши свой код здесь + if isinstance(content, list): + for cont in content: + phone = search_phone(cont, name) + if phone: + return phone + + elif isinstance(content, dict): + if content.get('name') == name: + return content.get('phone') + for cont in content.values(): + phone = search_phone(cont, name) + if phone: + return phone + return None diff --git a/tasks/practice5/employee.py b/tasks/practice5/employee.py index 1d7bad8..1bab70a 100644 --- a/tasks/practice5/employee.py +++ b/tasks/practice5/employee.py @@ -40,12 +40,20 @@ def __init__(self, name: str, position: str, salary: int): # пиши свой код здесь + if not (isinstance(name, str) and isinstance(position, str) and isinstance(salary, int)): + raise ValueError + + self.name = name + self.position = position + self._salary = salary + def get_salary(self) -> int: """ Метод возвращает зарплату сотрудника. """ # пиши свой код здесь + return self._salary def __eq__(self, other: object) -> bool: """ @@ -56,6 +64,16 @@ def __eq__(self, other: object) -> bool: """ # пиши свой код здесь + if isinstance(other, Employee): + try: + first = get_position_level(self.position) + second = get_position_level(other.position) + if first == second: + return True + return False + except NoSuchPositionError: + raise ValueError + raise TypeError def __str__(self): """ @@ -65,6 +83,8 @@ def __str__(self): # пиши свой код здесь + return 'name: ' + self.name + ' position: ' + self.position + def __hash__(self): return id(self) @@ -83,6 +103,8 @@ def __init__(self, name: str, salary: int, language: str): """ # пиши свой код здесь + super().__init__(name, self.position, salary) + self.language = language class Manager(Employee): @@ -98,3 +120,4 @@ def __init__(self, name: str, salary: int): """ # пиши свой код здесь + super().__init__(name, self.position, salary) diff --git a/tasks/practice5/team.py b/tasks/practice5/team.py index 934796c..fa50f83 100644 --- a/tasks/practice5/team.py +++ b/tasks/practice5/team.py @@ -29,6 +29,10 @@ def __init__(self, name: str, manager: Manager): # пиши свой код здесь + self.name = name + self.manager = manager + self.__members = set() + def add_member(self, member: Employee) -> None: """ Задача: реализовать метод добавления участника в команду. @@ -37,6 +41,11 @@ def add_member(self, member: Employee) -> None: # пиши свой код здесь + if isinstance(member, Employee): + self.__members.add(member) + else: + raise TypeError + def remove_member(self, member: Employee) -> None: """ Задача: реализовать метод удаления участника из команды. @@ -44,6 +53,13 @@ def remove_member(self, member: Employee) -> None: """ # пиши свой код здесь + if isinstance(member, Employee): + if member in self.__members: + self.__members.remove(member) + else: + raise NoSuchMemberError(self.name, member) + else: + raise TypeError def get_members(self) -> Set[Employee]: """ @@ -52,6 +68,10 @@ def get_members(self) -> Set[Employee]: """ # пиши свой код здесь + return self.__members.copy() + + def __str__(self) -> str: + return f'team: {self.name} manager: {self.manager.name} number of members: {len(self.__members)}' def show(self) -> None: """