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
24 changes: 24 additions & 0 deletions Sprint5-exercise/classes-and-object.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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


def double(person: Person) -> int:
return person.price * 2


imran = Person("Imran", 22, "Ubuntu")
print(imran.name)


eliza = Person("Eliza", 34, "Arch Linux")
print(eliza.name)


print(is_adult(imran))
25 changes: 25 additions & 0 deletions Sprint5-exercise/dataclass.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from datetime import date
from dataclasses import dataclass


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

def is_adult(self) -> bool:
current_date = date.today()
age = current_date.year - self.date_of_birth.year

if (current_date.month, current_date.day) < (
self.date_of_birth.month,
self.date_of_birth.day,
):
age -= 1

return age >= 18


imran = Person("Imran", date(1995, 10, 16), "Ubuntu")
print(imran.is_adult())
153 changes: 153 additions & 0 deletions Sprint5-exercise/enumexercise.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
from dataclasses import dataclass
from typing import List
from enum import Enum
import sys


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


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


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


def parse_age(age_txt: str) -> int:
try:
age = int(age_txt)
except ValueError:
print("Error:age must be a valid integer", file=sys.stderr)
sys.exit(1)

if age < 0:
print("Error: age should be a positive number", file=sys.stderr)
sys.exit(1)

return age


def parse_operating_system(op_tx: str) -> OperatingSystem:
normalized = op_tx.strip().lower()

mapping = {
"makos": OperatingSystem.MACOS,
"ubuntu": OperatingSystem.UBUNTU,
"arch": OperatingSystem.ARCH,
"arch linux": OperatingSystem.ARCH,
}

op: OperatingSystem = mapping.get(normalized)

if op is None:
print(
"Error: operating system must be one of: Ubuntu, Arch Linux, macOS.",
file=sys.stderr,
)
sys.exit(1)

return op


def find_matching_laptops(
laptops: List[Laptop], operating_system: OperatingSystem
) -> int:
counter: int = 0
for laptop in laptops:
if laptop.operating_system == operating_system:
counter += 1

return counter


def find_most_available(laptops: List[Laptop]) -> OperatingSystem:
count = {}
for laptop in laptops:
if laptop.operating_system in count:
count[laptop.operating_system] += 1
else:
count[laptop.operating_system] = 1
return max(count, key=count.get)


def main() -> None:

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,
),
]

name = input("Enter your name: ").strip()
if name == "":
print("Error: name must not be empty", file=sys.stderr)
sys.exit(1)
age_txt = input("Enter your age: ")
operating_system_txt = input("Enter operating system: ")

age = parse_age(age_txt)
operating_system = parse_operating_system(operating_system_txt)

person = Person(
name=name,
age=age,
preferred_operating_system=operating_system,
)

matching_laptops_count = find_matching_laptops(
laptops, person.preferred_operating_system
)
print(
f"Library has {matching_laptops_count} laptop(s) with {person.preferred_operating_system}"
)

most_available_os = find_most_available(laptops)

if most_available_os != person.preferred_operating_system:
count_most_available_os = find_matching_laptops(laptops, most_available_os)
print(
f"If you're willing to accept {most_available_os.value}, "
f"you're more likely to get a laptop because the library has "
f"{count_most_available_os} with that operating system."
)

if __name__ == "__main__":
main()
25 changes: 25 additions & 0 deletions Sprint5-exercise/generics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from dataclasses import dataclass
from typing import List


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

age: int
children: List["Person"]


fatma = Person(name="Fatma", age=22, children=[])
aisha = Person(name="Aisha", age=25, children=[])

imran = Person(name="Imran", age=40, children=[fatma, aisha])


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


print_family_tree(imran)
24 changes: 24 additions & 0 deletions Sprint5-exercise/methods.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from datetime import date


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

def is_adult(self) -> bool:
current_date = date.today()
age = current_date.year - self.date_of_birth.year

if (current_date.month, current_date.day) < (
self.date_of_birth.month,
self.date_of_birth.day,
):
age -= 1

return age >= 18


imran = Person("Imran", date(1995, 10, 16), "Ubuntu")
print(imran.is_adult())
33 changes: 33 additions & 0 deletions Sprint5-exercise/type-checking-with-mypy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
def open_account(balances: dict[str, int], name: str, amount: int) -> None:
balances[name] = amount


def sum_balances(accounts: dict[str, int]) -> 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(balances, "Tobi", 913)
open_account(balances, "Olya", 713)

total_pence = sum_balances(balances)
total_string = format_pence_as_string(total_pence)

print(f"The bank accounts total {total_string}")
68 changes: 68 additions & 0 deletions Sprint5-exercise/type-guided.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
from dataclasses import dataclass
from typing import List


@dataclass(frozen=True)
class Person:
name: str
age: int
preferred_operating_systems: List[str]


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


def find_possible_laptops(laptops: List[Laptop], person: Person) -> List[Laptop]:
possible_laptops = []
for laptop in laptops:

if laptop.operating_system in person.preferred_operating_systems:
possible_laptops.append(laptop)
return possible_laptops


people = [
Person(name="Imran", age=22, preferred_operating_systems=["Ubuntu", "Arch Linux"]),
Person(name="Eliza", age=34, preferred_operating_systems=["Arch Linux"]),
]

laptops = [
Laptop(
id=1,
manufacturer="Dell",
model="XPS",
screen_size_in_inches=13,
operating_system="Arch Linux",
),
Laptop(
id=2,
manufacturer="Dell",
model="XPS",
screen_size_in_inches=15,
operating_system="Ubuntu",
),
Laptop(
id=3,
manufacturer="Dell",
model="XPS",
screen_size_in_inches=15,
operating_system="Ubuntu",
),
Laptop(
id=4,
manufacturer="Apple",
model="macBook",
screen_size_in_inches=13,
operating_system="macOS",
),
]

for person in people:
possible_laptops = find_possible_laptops(laptops, person)
print(f"Possible laptops for {person.name}: {possible_laptops}")
Loading