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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
.venv
22 changes: 22 additions & 0 deletions prep-exercises/classes_objects.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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

imran = Person("Imran", 22, "Ubuntu")
print(imran.name)
#print(imran.address) # Person class do not have address attribute.

eliza = Person("Eliza", 34, "Arch Linux")
print(eliza.name)
#print(eliza.address) # again here Person class do not have address attribute.

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

print(is_adult(imran))

# This method will crash the program as there is no occupation attribute in the Person class
#def is_student(person: Person) -> bool:
# return person.occupation =="Software Engineer"
19 changes: 19 additions & 0 deletions prep-exercises/dataclass.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from dataclasses import dataclass
import datetime as dt

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

def is_adult(self) -> bool:
today = dt.date.today()
age = today.year - self.date_of_birth.year
if (today.month, today.day) < (self.date_of_birth.month, self.date_of_birth.day):
age -= 1
return age >= 18
imran = Person("Imran", "Ubuntu", dt.date(2009, 1, 24))
print(f"Is Imran an adult? {imran.is_adult()}")


95 changes: 95 additions & 0 deletions prep-exercises/enums.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
from dataclasses import dataclass
from enum import Enum
from typing import List
import sys

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


people = [
Person(name="Imran", age=22, preferred_operating_system=OperatingSystem.UBUNTU),
Person(name="Eliza", age=34, preferred_operating_system=OperatingSystem.ARCH),
]

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),
Laptop(id=5, manufacturer="Dell", model="XPS", screen_size_in_inches=13, operating_system=OperatingSystem.ARCH)
]


user_name = input(f"PLease enter your name: ")

user_age = input(f"PLease enter your age: ")
try :
user_age_int = int(user_age)
except ValueError :
sys.stderr.write(f" Error: {user_age} should be an number")
sys.exit(1)

user_os = input(f"PLease enter your preferred os: ")
try:
os_type = OperatingSystem(user_os)

except ValueError :
sys.stderr.write(f"Error: {user_os} should be a valid OS")
sys.exit(1)


new_data = Person(user_name,user_age_int,os_type)
amount_os =find_possible_laptops(laptops,new_data)
len_os =len(amount_os)
print(f"we found {len_os} available")

counts ={}
for os in OperatingSystem:
count =0

for laptop in laptops:
if laptop.operating_system ==os:
count+=1
counts[os]=count

max_value= max(counts.values())
better_choices = [max_value]

better_choices=[]
for os, count in counts.items():
if count == max_value:
better_choices.append(os.value)


multi_options = " OR ".join(better_choices)

if max_value > len_os:
print(f"We have better choices, you should consider: {multi_options}")
elif max_value==len_os :
print(f"we have multiple choices: {multi_options}, however {os_type.value} is still a good choice.")
20 changes: 20 additions & 0 deletions prep-exercises/generic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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 =5, children=[])
aisha = Person(name="Aisha",age =10, children=[])

imran = Person(name="Imran",age = 50, 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)
47 changes: 47 additions & 0 deletions prep-exercises/inheritance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
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())

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())

# Output for person1:
#1. line 26 it call the method get_name() from the Parent class the output will be "Elizaveta Alekseeva" as the Child class in inheriting the properties from the Parent class.
#2. line 27 it call the get_full_name() method from the Child class the output will be "Elizaveta Alekseeva " as the check statement will get skip entirely.
#3. line 28 we call the change_last_name() method to change last name of person1, the method started as an empty array then it append (end of the array)to it the new parameter value given output "Elezaveta Tyurina". CORRECTION: it will append the old last_name
#4. I think that line 29 and 30 will print as the 26 and 27, as they do not interact with the new array created by change_last_name method(). CORRECTION: line 30 the list now have leng 1, means it will add suffix value at the end.

#Output for person2:
#1. line 33 and 36 the output will be similar to line 26
#2. lines 34, 35, 37 all they will throw an error as the inheritance work as waterfall a child can access parent methods, however a parent class can not.
22 changes: 22 additions & 0 deletions prep-exercises/methods.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import datetime as dt # we import the datetime module

# this is similar to new Date() object in js, but only getting the date not hours etc.

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

def is_adult(self) -> bool:
today = dt.date.today()
age = today.year - self.date_of_birth.year
if (today.month, today.day) < (self.date_of_birth.month, self.date_of_birth.day):
# if the birthday date did not happened yet ad -1 else skip it, we use full age
age -= 1
return age >= 18

imran_dob = dt.date(1986, 12, 24)
imran = Person("Imran", "Ubuntu", imran_dob)

print(f"{imran.name} is adult: {imran.is_adult()}")
42 changes: 42 additions & 0 deletions prep-exercises/type_guided_refact.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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 == person.preferred_operating_systems:
possible_laptops.append(laptop)
return possible_laptops


people = [
Person(name="Imran", age=22, preferred_operating_systems=["Ubuntu"]),
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