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
8 changes: 8 additions & 0 deletions Perp-Exercise/exercise1.1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
def double(value):
return value * 2
#Question
#Predict what double("22") will do. Then run the code and check. Did it do what you expected? Why did it return the value it did?

#Answer
# it will return "2222"
#In python, the string*number shows the number of times of the string.
12 changes: 12 additions & 0 deletions Perp-Exercise/exercise1.2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def double(number):
return number * 3

print(double(10))

#Question
#Read the above code and write down what the bug is. How would you fix it?

#Answer

#The code is correct but the function name as double but the code is calculated for Triple .
#I can fix with two method. 1. change to function name to triple 2. change return number *3 to number *2
33 changes: 33 additions & 0 deletions Perp-Exercise/exercise2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# adding type annotation

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: dict[str,int] = {
"Sima": 700,
"Linn": 545,
"Georg": 831,
}

#changing float to int (9.13 to 913)
open_account(balances,"Tobi", 913)
open_account(balances,"Olya", 713)

total_pence = sum_balances(balances)
total_string = format_pence_as_string(total_pence) #correct the function name

print(f"The bank accounts total {total_string}")
28 changes: 28 additions & 0 deletions Perp-Exercise/exercise3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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.age)

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

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

print(is_adult(imran))

#new function
ayk = Person("AYK",36,"Linux")

def get_info(person: Person):
print(person.address)



result = get_info(ayk)
25 changes: 25 additions & 0 deletions Perp-Exercise/exercise4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import datetime as dt

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

def get_age(self) -> int :
today = dt.date.today()
return today.year - self.dob.year

imran = Person("Imran", dt.date(1990,8,22), "Ubuntu")
print(imran.name)
print(imran.dob)
print(imran.get_age())


Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As part of task 4, can you Explain advantages / disadvantages between class methods vs free functions








26 changes: 26 additions & 0 deletions Perp-Exercise/exercise5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import datetime as dt
from dataclasses import dataclass

@dataclass
class Person:
name: str
dob: dt.date
preferred_os:str

def get_age(self) -> int :
today = dt.date.today()
return today.year - self.dob.year

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the year the only part you need to consider for this to work?


imran = Person("Imran", dt.date(1990,8,22), "Ubuntu")
print(imran.name)
print(imran.dob)
print(imran.get_age())









20 changes: 20 additions & 0 deletions Perp-Exercise/exercise6.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
children: List["Person"]
age: int

fatma = Person(name="Fatma", children=[],age=5)
aisha = Person(name="Aisha", children=[],age=7)

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

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

print_family_tree(imran)
42 changes: 42 additions & 0 deletions Perp-Exercise/exercise7.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 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=["Ubuntu","macOS"]),
]

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