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
37 changes: 37 additions & 0 deletions prep-exercises/classes_and_objects.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# ------------------------
# Q. Read the error, and make sure you understand what it’s telling you.
# ------------------------

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)

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

# A. The error is telling me that the 'address' attribute cannot be accessed because it does not exist on the 'Person' class


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


print(is_adult(imran))


# ------------------------
# Q. Write a new function in the file that accepts a Person as a parameter and tries to access a property that doesn’t exist. Run it through mypy and check that it does report an error.
# ------------------------

# def get_address(person: Person) -> str:
# return person.address

# A. As in the previous example, the attribute cannot be accessed because it does not exist on the 'Person' class
30 changes: 30 additions & 0 deletions prep-exercises/dataclasses_ex.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# ------------------------
# Q. Write a Person class using @datatype which uses a datetime.date for date of birth, rather than an int for age.

# Re-add the is_adult method to it.
# ------------------------

# A.

import datetime as dt
from dataclasses import dataclass


@dataclass
class Person:
name: str
birthdate: dt.date
preferred_operating_system: str

def is_adult(self) -> bool:
today = dt.date.today()
age = today.year - self.birthdate.year
birthday_this_year = dt.date(
today.year, self.birthdate.month, self.birthdate.day)
if today < birthday_this_year:
age -= 1
return age >= 18


imran = Person("Imran", dt.date(2008, 3, 21), "Ubuntu")
print(imran.is_adult())
100 changes: 100 additions & 0 deletions prep-exercises/enums.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# ------------------------
# Write a program which:

# Already has a list of Laptops that a library has to lend out.
# Accepts user input to create a new Person - it should use the input function to read a person’s name, age, and preferred operating system.
# Tells the user how many laptops the library has that have that operating system.
# If there is an operating system that has more laptops available, tells the user that if they’re willing to accept that operating system they’re more likely to get a laptop.
# You should convert the age and preferred operating system input from the user into more constrained types as quickly as possible, and should output errors to stderr and terminate the program with a non-zero exit code if the user input bad values.
# ------------------------

# A.

import sys
from dataclasses import dataclass
from enum import Enum
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: List[Laptop] = []
for laptop in laptops:
if laptop.operating_system == person.preferred_operating_system:
possible_laptops.append(laptop)
return possible_laptops


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

try:
user_name = input('Enter your name: ')
if not user_name.strip():
raise ValueError("Name cannot be empty")
except ValueError:
sys.stderr.write("Error: Please provide a valid name\n")
sys.exit(1)

try:
user_age_input = input('Enter your age: ')
user_age = int(user_age_input)
if user_age < 0:
raise ValueError("Age cannot be negative")
except ValueError:
sys.stderr.write("Error: Please provide a valid age\n")
sys.exit(1)

try:
user_os_input = input(
'Enter your preferred operating system (macOS, Arch Linux, Ubuntu): ')
user_os = OperatingSystem(user_os_input)
except ValueError:
sys.stderr.write("Error: Please enter a valid operating system\n")
sys.exit(1)

person = Person(name=user_name, age=user_age,
preferred_operating_system=user_os)

possible_laptops = find_possible_laptops(laptops, person)
print(f"We have {len(possible_laptops)} laptop(s) with {user_os.value}")

# Find OS with most laptops
os_counts: dict[OperatingSystem, int] = {}
for laptop in laptops:
os_counts[laptop.operating_system] = os_counts.get(
laptop.operating_system, 0) + 1

best_os = max(os_counts, key=lambda os: os_counts[os])
if best_os != user_os and os_counts[best_os] > len(possible_laptops):
print(
f"If you're willing to accept {best_os.value}, you're more likely to get a laptop ({os_counts[best_os]} available)")
30 changes: 30 additions & 0 deletions prep-exercises/generics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# ------------------------
# Fix the above code so that it works. You must not change the print on line 17 - we do want to print the children’s ages. (Feel free to invent the ages of Imran’s children.)
# ------------------------

# A.

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=11, children=[])
aisha = Person(name="Aisha", age=6, children=[])

imran = Person(name="Imran", age=43, 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)
66 changes: 66 additions & 0 deletions prep-exercises/inheritance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# ------------------------
# Play computer with this code. Predict what you expect each line will do. Then run the code and check your predictions. (If any lines cause errors, you may need to comment them out to check later lines).
# ------------------------

# A.

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: list[str] = []

def change_last_name(self, last_name: str) -> 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")
# Predict: person1 has first_name="Elizaveta", last_name="Alekseeva", previous_last_names=[]

print(person1.get_name())
# Checked: prints "Elizaveta Alekseeva"

print(person1.get_full_name())
# Predict + checked: prints "Elizaveta Alekseeva"

person1.change_last_name("Tyurina")
# Predict: previous_last_names becomes ["Alekseeva"], last_name becomes "Tyurina"

print(person1.get_name())
# Predict + checked: prints "Elizaveta Tyurina"

print(person1.get_full_name())
# Predict + checked: prints "Elizaveta Tyurina (née Alekseeva)"

person2 = Parent("Elizaveta", "Alekseeva")
# Predict: person2 has first_name and last_name only

print(person2.get_name())
# Predict + checked: prints "Elizaveta Alekseeva"

# print(person2.get_full_name())
# Predict + checked: AttributeError, because Parent has no get_full_name method

# person2.change_last_name("Tyurina")
# Predict + checked: AttributeError, because Parent has no change_last_name method

print(person2.get_name())
# Predict + checked: still prints "Elizaveta Alekseeva"

# print(person2.get_full_name())
# Predict + checked: AttributeError again for missing Parent.get_full_name
39 changes: 39 additions & 0 deletions prep-exercises/methods.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# ------------------------
# Q. Think of the advantages of using methods instead of free functions.
# ------------------------

# A.
# - Keeps related data together
# - Dot notation makes it clear what the operation is acting on
# - Doesn't pollute global namespace
# - Easier to maintain since all methods are inside the Class, whereas free functions can be scattered across different modules

# ------------------------
# Q. Change the Person class to take a date of birth (using the standard library’s datetime.date class) and store it in a field instead of age.

# Update the is_adult method to act the same as before.
# ------------------------

# A.

import datetime as dt


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

def is_adult(self) -> bool:
today = dt.date.today()
age = today.year - self.birthdate.year
birthday_this_year = dt.date(
today.year, self.birthdate.month, self.birthdate.day)
if today < birthday_this_year:
age -= 1
return age >= 18


imran = Person("Imran", dt.date(2008, 3, 22), "Ubuntu")
print(imran.is_adult())
43 changes: 43 additions & 0 deletions prep-exercises/type_checking.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# ------------------------
# Q. Do not run the following code.

# This code contains bugs related to types. They are bugs mypy can catch.

# Read this code to understand what it’s trying to do. Add type annotations to the method parameters and return types of this code. Run the code through mypy, and fix all of the bugs that show up. When you’re confident all of the type annotations are correct, and the bugs are fixed, run the code and check it works.
# ------------------------

def open_account(balances: dict[str, int], name: str, amount: int):
balances[name] = amount


def sum_balances(accounts: dict[str, 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):
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,
}

# Added missing argument for balances
# Converted float and string arguments to integers
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}")
Loading
Loading