Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
21 changes: 21 additions & 0 deletions sprint5-prep-exercises/classes_and_objects.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Person:
def __init__(self, name: str, age: int, preferred_operating_system: str):
self.name = name
self.age = age
self.preferred_operating_system = preferred_operating_system


def is_adult(person: Person) -> bool:
return person.age >= 18


imran = Person("Imran", 22, "Ubuntu")
print(imran.name)
# there is no property 'address' in Person class
# print(imran.address)
print(is_adult(imran))

eliza = Person("Eliza", 34, "Arch Linux")
print(eliza.name)
# there is no property 'address' in Person class
# print(eliza.address)
24 changes: 24 additions & 0 deletions sprint5-prep-exercises/dataclasses_ex.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from dataclasses import dataclass
from datetime import date


@dataclass(frozen=True)
class Person:
name: str
date_of_birth: date
preferred_operating_system: str

def is_adult(self) -> bool:
current_date = date.today()
full_years = current_date.year - self.date_of_birth.year
if (current_date.month < self.date_of_birth.month) or (
current_date.month == self.date_of_birth.month
and current_date.day < self.date_of_birth.day
):
full_years -= 1
return full_years >= 18


imran = Person("Imran", date(2008, 1, 30), "Ubuntu")
print(imran)
print("Imran is adult: " + str(imran.is_adult()))
17 changes: 17 additions & 0 deletions sprint5-prep-exercises/double.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
def double(value):
return value * 2


def double2(number):
return number * 3


def main():
# "22" is a string --> it will return string: "2222"
print(double("22"))
# in this function that expected to double the number, it multiplied by 3 ( probably accidental mistype)
print(double2(10)) # return 30 instead of 20


if __name__ == "__main__":
main()
129 changes: 129 additions & 0 deletions sprint5-prep-exercises/enums.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
from enum import Enum

from dataclasses import dataclass
import sys
from typing import List


class OperatingSystem(Enum):
MACOS = "macOS"
ARCH = "Arch Linux"
UBUNTU = "Ubuntu"


@dataclass(frozen=True)
class Person:
name: str
age: int
preferred_operating_system: OperatingSystem


@dataclass(frozen=True)
class Laptop:
id: int
manufacturer: str
model: str
screen_size_in_inches: float
operating_system: OperatingSystem


def find_possible_laptops(laptops: List[Laptop], person: Person) -> List[Laptop]:
possible_laptops = []
for laptop in laptops:
if laptop.operating_system == person.preferred_operating_system:
possible_laptops.append(laptop)
return possible_laptops


def operating_system_laptop_num(
laptops: List[Laptop], system: OperatingSystem
) -> List[Laptop]:
sys_laptops = []
for laptop in laptops:
if laptop.operating_system == system:
sys_laptops.append(laptop)
return sys_laptops


def inputPerson() -> dict:
name = input("Enter name: ")
age = int(input("Enter age: "))
systemStr = input("Enter preferred operating system: ")
system = None
for system_variant in OperatingSystem:
if systemStr.lower() == system_variant.name.lower():
system = system_variant
if system == None:
print("Incorrect operating system input.")
return {}

return {"name": name, "age": age, "system": system}


def main():
input = inputPerson()
if len(input) == 0:
return

newPerson = Person(input["name"], input["age"], input["system"])
laptops = [
Laptop(
id=1,
manufacturer="Dell",
model="XPS",
screen_size_in_inches=13,
operating_system=OperatingSystem.ARCH,
),
Laptop(
id=2,
manufacturer="Dell",
model="XPS",
screen_size_in_inches=15,
operating_system=OperatingSystem.UBUNTU,
),
Laptop(
id=3,
manufacturer="Dell",
model="XPS",
screen_size_in_inches=15,
operating_system=OperatingSystem.UBUNTU,
),
Laptop(
id=4,
manufacturer="Apple",
model="macBook",
screen_size_in_inches=13,
operating_system=OperatingSystem.MACOS,
),
]

preferred_laptops_num = len(find_possible_laptops(laptops, newPerson))
max_other_laptops = 0
other_laptops_sys = None
for system_variant in OperatingSystem:
if system_variant == input["system"]:
continue
laptops_num = len(operating_system_laptop_num(laptops, system_variant))
if laptops_num > preferred_laptops_num:
max_other_laptops = laptops_num
other_laptops_sys = system_variant

print(
"We have "
+ str(preferred_laptops_num)
+ " with "
+ input["system"].name
+ " installed."
)
if max_other_laptops > 0:
print(
"We also have "
+ str(max_other_laptops)
+ " with "
+ other_laptops_sys.name
+ " installed."
)


if __name__ == "__main__":
main()
35 changes: 35 additions & 0 deletions sprint5-prep-exercises/generics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from dataclasses import dataclass
from typing import List
from datetime import date


@dataclass(frozen=True)
class Person:
name: str
date_of_birth: date
children: List["Person"]

def getAge(self) -> int:
current_date = date.today()
full_years = current_date.year - self.date_of_birth.year
if (current_date.month < self.date_of_birth.month) or (
current_date.month == self.date_of_birth.month
and current_date.day < self.date_of_birth.day
):
full_years -= 1
return full_years


fatma = Person(name="Fatma", date_of_birth=date(2020, 4, 12), children=[])
aisha = Person(name="Aisha", date_of_birth=date(2024, 7, 5), children=[])

imran = Person(name="Imran", date_of_birth=date(2000, 1, 30), children=[fatma, aisha])


def print_family_tree(person: Person) -> None:
print(person.name)
for child in person.children:
print(f"- {child.name} ({child.getAge()})")


print_family_tree(imran)
40 changes: 40 additions & 0 deletions sprint5-prep-exercises/inheritance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class Parent:
def __init__(self, first_name: str, last_name: str):
self.first_name = first_name
self.last_name = last_name

def get_name(self) -> str:
return f"{self.first_name} {self.last_name}"


class Child(Parent):
def __init__(self, first_name: str, last_name: str):
super().__init__(first_name, last_name)
self.previous_last_names = []

def change_last_name(self, last_name) -> None:
self.previous_last_names.append(self.last_name)
self.last_name = last_name

def get_full_name(self) -> str:
suffix = ""
if len(self.previous_last_names) > 0:
suffix = f" (née {self.previous_last_names[0]})"
return f"{self.first_name} {self.last_name}{suffix}"


person1 = Child("Elizaveta", "Alekseeva")
print(person1.get_name())
print(person1.get_full_name())
person1.change_last_name("Tyurina")
print(person1.get_name())
print(person1.get_full_name())

# class person has no methods get_full_name() and change_last_name()
# when program will reach lines with this methods applied to person class it will throw an error
person2 = Parent("Elizaveta", "Alekseeva")
print(person2.get_name())
# print(person2.get_full_name())
# person2.change_last_name("Tyurina")
print(person2.get_name())
# print(person2.get_full_name())
26 changes: 26 additions & 0 deletions sprint5-prep-exercises/methods.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from datetime import date


class Person:

def __init__(self, name: str, date_of_birth: date):
self.name = name
self.date_of_birth = date_of_birth

def is_adult(self) -> bool:
current_date = date.today()
full_years = current_date.year - self.date_of_birth.year
if (current_date.month < self.date_of_birth.month) or (
current_date.month == self.date_of_birth.month
and current_date.day < self.date_of_birth.day
):
full_years -= 1
return full_years >= 18


imran = Person("Imran", date(2008, 1, 30))
eliza = Person("Eliza", date(2088, 7, 30))
someone = Person("Someone", date(2021, 7, 30))
print("Imran is adult: " + str(imran.is_adult()))
print("Eliza is adult: " + str(eliza.is_adult()))
print("Someone is adult: " + str(someone.is_adult()))
33 changes: 33 additions & 0 deletions sprint5-prep-exercises/type_checking.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
def open_account(balances: dict, name: str, amount: int) -> None:
balances[name] = amount


def sum_balances(accounts: dict) -> int:
total = 0
for name, pence in accounts.items():
print(f"{name} had balance {pence}")
total += pence
return total


def format_pence_as_string(total_pence: int) -> str:
if total_pence < 100:
return f"{total_pence}p"
pounds = int(total_pence / 100)
pence = total_pence % 100
return f"£{pounds}.{pence:02d}"


balances = {
"Sima": 700,
"Linn": 545,
"Georg": 831,
}

open_account("Tobi", 9.13)
open_account("Olya", "£7.13")

total_pence = sum_balances(balances)
total_string = format_pence_as_str(total_pence)

print(f"The bank accounts total {total_string}")
Loading
Loading