From 211c28c340af5d6c50d06cb12cb8414a3b800ada Mon Sep 17 00:00:00 2001 From: Ayobami Alaran A Date: Fri, 3 Feb 2023 10:42:59 +0100 Subject: [PATCH] Added new python exercise with solution (#346) * added new python exercise and solution * add sort exercise and solutions * changed heading1 * fixed header description --- topics/python/class_0x00.md | 3 + .../python/solutions/class_0x00_solution.md | 55 +++++++++++++++++++ topics/python/solutions/sort_solution.md | 26 +++++++++ topics/python/sort.md | 6 ++ 4 files changed, 90 insertions(+) create mode 100644 topics/python/class_0x00.md create mode 100644 topics/python/solutions/class_0x00_solution.md create mode 100644 topics/python/solutions/sort_solution.md create mode 100644 topics/python/sort.md diff --git a/topics/python/class_0x00.md b/topics/python/class_0x00.md new file mode 100644 index 0000000..65a0048 --- /dev/null +++ b/topics/python/class_0x00.md @@ -0,0 +1,3 @@ +## Class + +write a simple class that has two attributes of which one has a default value and has two methods diff --git a/topics/python/solutions/class_0x00_solution.md b/topics/python/solutions/class_0x00_solution.md new file mode 100644 index 0000000..6bb0284 --- /dev/null +++ b/topics/python/solutions/class_0x00_solution.md @@ -0,0 +1,55 @@ +## Class 0x00 - Solution + +1. write a simple class that has two attributes of which one has a default value and has two methods + +```python +from typing import Optional +""" Student Module + + """ + + +class Student: + def __init__(self, name: str, department: Optional[str] = None) -> None: + """ Instance Initialization function + + Args: + name (str): Name of student + department (Optional[str], optional): Department. Defaults to None. + """ + self.name = name + self.department = department + + def getdetails(self) -> str: + """ Gets the students details + + Returns: + str: A formatted string + """ + return f"Name is {self.name}, I'm in department {self.department}" + + def change_department(self, new_deparment: str) -> None: + """Changes the department of the student object + + Args: + new_deparment (str): Assigns the new deparment value to dept attr + """ + self.department = new_deparment + +# student1 instantiation +student1 = Student("Ayobami", "Statistics") + +print(student1.getdetails()) + +# Calling the change_department function to change the department of student +student1.change_department("CS") + +print(student1.department) +``` + +Output + +``` +Name is Ayobami, I'm in department Statistics +CS +``` diff --git a/topics/python/solutions/sort_solution.md b/topics/python/solutions/sort_solution.md new file mode 100644 index 0000000..9ba86e5 --- /dev/null +++ b/topics/python/solutions/sort_solution.md @@ -0,0 +1,26 @@ +## Sort Descending - Solution + +1. write a function that sorts the following list of list without using the `sorted()` and `.sort()` + function in descending order + + - mat_list = [[1, 2, 3], [2, 4, 4], [5, 5, 5]] -> [[5, 5, 5], [2, 4, 4], [1, 2, 3]] + +```python +def sort_desc(mat: list) -> list: + """ Sorts a list in descending order + + Args: + mat (list): paresd list + + Returns: + list: A new list + """ + new_list = [] + while mat != []: + maxx = max(mat) + new_list.append(maxx) + mat.remove(maxx) + return new_list + +print(sort_func(mat_list)) +``` diff --git a/topics/python/sort.md b/topics/python/sort.md new file mode 100644 index 0000000..9f4eefa --- /dev/null +++ b/topics/python/sort.md @@ -0,0 +1,6 @@ +## Sort Descending + +1. write a function that sorts the following list of list without using the `sorted()` and `.sort()` + function in descending order + + - list = [[1, 2, 3], [2, 4, 4], [5, 5, 5]] -> [[5, 5, 5], [2, 4, 4], [1, 2, 3]]