From 41bd186cf1db72dafde51319869c0a5b77c5a00b Mon Sep 17 00:00:00 2001 From: OSA1011 Date: Mon, 17 Jun 2024 21:46:38 +0300 Subject: [PATCH] all homework done --- tasks/practice1/practice1.py | 4 ++-- tasks/practice2/practice2.py | 28 +++++++++++++++++++++----- tasks/practice3/practice3.py | 39 ++++++++++++++++++++++++++++++------ tasks/practice4/practice4.py | 13 ++++++++++++ tasks/practice5/employee.py | 19 ++++++++++++++++++ tasks/practice5/team.py | 23 +++++++++++++++++++++ 6 files changed, 113 insertions(+), 13 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..d0d184e 100644 --- a/tasks/practice2/practice2.py +++ b/tasks/practice2/practice2.py @@ -1,3 +1,4 @@ +import random from typing import Iterable UNCULTURED_WORDS = ('kotleta', 'pirog') @@ -13,7 +14,7 @@ def greet_user(name: str) -> str: """ # пиши код здесь - return greeting + return f"Hello, {name}!" def get_amount() -> float: @@ -29,7 +30,7 @@ def get_amount() -> float: """ # пиши код здесь - return amount + return float(random.randint(100, 1000000)) def is_phone_correct(phone_number: str) -> bool: @@ -43,7 +44,12 @@ def is_phone_correct(phone_number: str) -> bool: """ # пиши код здесь - return result + if phone_number[:2] != "+7": + return False + for x in phone_number[2:]: + if x not in "0123456789": + return False + return True def is_amount_correct(current_amount: float, transfer_amount: str) -> bool: @@ -59,7 +65,7 @@ def is_amount_correct(current_amount: float, transfer_amount: str) -> bool: """ # пиши код здесь - return result + return current_amount >= float(transfer_amount) def moderate_text(text: str, uncultured_words: Iterable[str]) -> str: @@ -78,7 +84,14 @@ def moderate_text(text: str, uncultured_words: Iterable[str]) -> str: """ # пиши код здесь - return result + word_list = text.lower().replace("\'", "").replace("\"", "").strip().split() + result = word_list[0] + for word in word_list[1:]: + result += " " + word + for nword in uncultured_words: + if nword in result: + result = result.replace(nword, '#' * len(nword)) + return result.replace(result[0], result[0].upper()) def create_request_for_loan(user_info: str) -> str: @@ -101,4 +114,9 @@ def create_request_for_loan(user_info: str) -> str: """ # пиши код здесь + fields = ["Фамилия: ", "Имя: ", "Отчество: ", "Дата рождения: ", "Запрошенная сумма: "] + data = user_info.split(',') + for i in range(len(fields)): + fields[i] += data[i] + result = "\n".join(fields) return result diff --git a/tasks/practice3/practice3.py b/tasks/practice3/practice3.py index 9115c9c..fbb8b97 100644 --- a/tasks/practice3/practice3.py +++ b/tasks/practice3/practice3.py @@ -27,8 +27,19 @@ def count_words(text: str) -> Dict[str, int]: """ # пиши свой код здесь - - return {} + import string + for i in string.punctuation: + text = text.replace(i, " ") + word_list = text.split() + result = dict() + for word in word_list: + word = word.lower() + if len(word) != 0 and all(c.isalpha() for c in word): + if word in result: + result[word] += 1 + else: + result[word] = 1 + return result def exp_list(numbers: List[int], exp: int) -> List[int]: @@ -42,7 +53,7 @@ def exp_list(numbers: List[int], exp: int) -> List[int]: # пиши свой код здесь - return [] + return [x ** exp for x in numbers] def get_cashback(operations: List[Dict[str, Any]], special_category: List[str]) -> float: @@ -58,6 +69,12 @@ def get_cashback(operations: List[Dict[str, Any]], special_category: List[str]) :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 @@ -99,6 +116,16 @@ def csv_reader(header: str) -> int: :return: количество уникальных элементов в столбце """ - # пиши свой код здесь - - return 0 + import csv + with open(get_path_to_file(), mode='r') as file: + csvfile = csv.reader(file, delimiter=",") + flag = True + res_list = [] + for row in csvfile: + if flag: + header_index = row.index(header) + else: + res_list.append(row[header_index]) + flag = False + result = len(dict.fromkeys(res_list)) + return result diff --git a/tasks/practice4/practice4.py b/tasks/practice4/practice4.py index a7d6b8d..6541e34 100644 --- a/tasks/practice4/practice4.py +++ b/tasks/practice4/practice4.py @@ -40,4 +40,17 @@ def search_phone(content: Any, name: str) -> Optional[str]: # пиши свой код здесь + if isinstance(content, dict): + if 'name' in content.keys() and 'phone' in content.keys(): + if content['name'] == name: + return content['phone'] + for key in content.values(): + result = search_phone(key, name) + if result is not None: + return result + elif isinstance(content, list): + for item in content: + result = search_phone(item, name) + if result is not None: + return result return None diff --git a/tasks/practice5/employee.py b/tasks/practice5/employee.py index 1d7bad8..b9b0e3c 100644 --- a/tasks/practice5/employee.py +++ b/tasks/practice5/employee.py @@ -39,6 +39,14 @@ def __init__(self, name: str, position: str, salary: int): """ # пиши свой код здесь + if not isinstance(salary, int): + raise ValueError + + self.name = name + self.position = position + self._salary = salary + + def get_salary(self) -> int: """ @@ -46,6 +54,7 @@ def get_salary(self) -> int: """ # пиши свой код здесь + return self._salary def __eq__(self, other: object) -> bool: """ @@ -56,6 +65,12 @@ def __eq__(self, other: object) -> bool: """ # пиши свой код здесь + if not isinstance(other, Employee): + raise TypeError + try: + return get_position_level(other.position) == get_position_level(self.position) + except NoSuchPositionError as e: + raise ValueError from e def __str__(self): """ @@ -64,6 +79,7 @@ def __str__(self): """ # пиши свой код здесь + return f"name: {self.name} position: {self.position}" def __hash__(self): return id(self) @@ -83,6 +99,8 @@ def __init__(self, name: str, salary: int, language: str): """ # пиши свой код здесь + self.language = language + super().__init__(name, self.position, salary) class Manager(Employee): @@ -98,3 +116,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..84095cc 100644 --- a/tasks/practice5/team.py +++ b/tasks/practice5/team.py @@ -28,6 +28,12 @@ def __init__(self, name: str, manager: Manager): """ # пиши свой код здесь + if not isinstance(manager, Manager): + raise TypeError + + self.name = name + self.manager = manager + self.__members = set() def add_member(self, member: Employee) -> None: """ @@ -37,6 +43,10 @@ def add_member(self, member: Employee) -> None: # пиши свой код здесь + if not isinstance(member, Employee): + raise TypeError + self.__members.add(member) + def remove_member(self, member: Employee) -> None: """ Задача: реализовать метод удаления участника из команды. @@ -45,6 +55,13 @@ def remove_member(self, member: Employee) -> None: # пиши свой код здесь + if not isinstance(member, Employee): + raise TypeError + try: + self.__members.remove(member) + except KeyError as e: + raise NoSuchMemberError(self.name, member) from e + def get_members(self) -> Set[Employee]: """ Задача: реализовать метод возвращения списка участков команды та, @@ -53,6 +70,12 @@ def get_members(self) -> Set[Employee]: # пиши свой код здесь + result = self.__members.copy() + return result + + def __str__(self): + return f"team: {self.name} manager: {self.manager.name} number of members: {len(self.__members)}" + def show(self) -> None: """ DO NOT EDIT!