generated from CodeYourFuture/Module-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 7
London | Elhadj Abdoul Diallo | Module-Decomposition | WEEK4 - prep-exercises #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
eediallo
wants to merge
21
commits into
CodeYourFuture:main
Choose a base branch
from
eediallo:prep-exercises
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+294
−0
Open
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
43fd3cc
predict what double("22") will do
eediallo 65c6ee1
add function at limits of type checking section
eediallo 04eb798
change function name to reflect what it does
eediallo edd621c
add mypy to requirements for type checking
eediallo 58b2ea1
add functions for account management and balance formatting
eediallo 10c2406
add .gitignore to exclude virtual environment directory
eediallo 1c20088
add type annotations to functions and fix account opening parameters
eediallo d6fc6e4
refactor: update type annotations for account functions and adjust ba…
eediallo bfb8234
read and understand error
eediallo d534963
add method to check if a person is an adult
eediallo e92f78f
Write a function that accepts a Person as a parameter and tries to ac…
eediallo eab6400
change Person class to take a date of birth and update is_adult method
eediallo 30f8a0f
fix: correct age calculation in is_adult method to account for month …
eediallo 079fa07
update is_adult method to use datetime
eediallo 7ad2748
add ages to Person instances
eediallo 2ea6159
Update Person.preferred_operating_system to use a
eediallo e787cf2
Add Laptop and Person classes with operating system selection
eediallo e5cfb61
predict what is going to happen when code is run.
eediallo 07825d2
comments out non existing parent method
eediallo e9d929f
Refine type annotations for previous_last_names and change_last_name …
eediallo 51bda20
Add node_modules to .gitignore
eediallo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.venv. | ||
node_modules |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
def is_adult(self) -> bool: | ||
return self.age >= 18 | ||
|
||
imran = Person("Imran", 22, "Ubuntu") | ||
print(imran.name) | ||
# print(imran.address) # address is not an attribute of the Person class | ||
print(imran.is_adult()) | ||
|
||
eliza = Person("Eliza", 34, "Arch Linux") | ||
print(eliza.name) | ||
# print(eliza.address)# address is not an attribute of the Person class | ||
|
||
def get_address(person: Person) -> str: | ||
return person.address | ||
|
||
print(get_address(imran)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
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() | ||
age = current_date.year - self.date_of_birth.year - ( | ||
(current_date.month, current_date.day) < (self.date_of_birth.month, self.date_of_birth.day) | ||
) | ||
return age >= 18 | ||
|
||
# Example usage | ||
imran = Person("Imran", date(2001, 5, 15), "Ubuntu") | ||
print(imran) # Prints Person(name='Imran', date_of_birth=datetime.date(2001, 5, 15), preferred_operating_system='Ubuntu') | ||
print(imran.is_adult()) # Prints True or False depending on the current date |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
from dataclasses import dataclass | ||
from enum import Enum | ||
|
||
class OperatingSystem(Enum): | ||
MACOS = "macOS" | ||
UBUNTU = "Ubuntu" | ||
ARCH = "Arch Linux" | ||
|
||
@dataclass | ||
class Laptop: | ||
id: int | ||
manufacturer: str | ||
model: str | ||
screen_size_in_inches: float | ||
operating_system: OperatingSystem | ||
|
||
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), | ||
] | ||
|
||
@dataclass | ||
class Person: | ||
name: str | ||
age: int | ||
preferred_operating_system: OperatingSystem | ||
|
||
|
||
name = input('Please enter your name: ') | ||
|
||
age_input = input('Please enter your age: ') | ||
try: | ||
age = int(age_input) | ||
except ValueError: | ||
print(f"Age must be a number") | ||
exit() | ||
|
||
preferred_os_input = input('Please enter your preferred operating system: ') | ||
try: | ||
preferred_operating_system = OperatingSystem(preferred_os_input) | ||
except ValueError: | ||
print(f'We are not offering {preferred_os_input}. Please choose from the following list: {[os.value for os in OperatingSystem]}') | ||
exit() | ||
|
||
|
||
|
||
person = Person(name=name, age=age, preferred_operating_system=preferred_operating_system) | ||
|
||
|
||
number_of_available_laptops = sum( | ||
1 for laptop in laptops if laptop.operating_system == person.preferred_operating_system | ||
) | ||
|
||
def offer_laptop_to_user()->None: | ||
offer = input('Would you like a laptop with this OS (yes / no) ? ') | ||
if offer.lower() == "y" or offer.lower() == "yes": | ||
print(f'Great! Please come on monday next week to collect your laptop') | ||
else: | ||
print('No problem. See you later.') | ||
|
||
if number_of_available_laptops == 1: | ||
print(f'There is {number_of_available_laptops} laptop available with your preferred operating system.') | ||
offer_laptop_to_user() | ||
elif number_of_available_laptops > 1: | ||
print(f'There are {number_of_available_laptops} laptops available with your preferred operating system.') | ||
offer_laptop_to_user() | ||
else: | ||
print('There are no laptops available with your preferred operating system.') | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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=25, children=[]) | ||
aisha = Person(name="Aisha",age=30, 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
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") | ||
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()) # will thrown an error since get_full_name does not exist in parent class | ||
# person2.change_last_name("Tyurina") # will thrown an error since change_last_name method does not exist in parent class | ||
print(person2.get_name()) | ||
# print(person2.get_full_name()) # will thrown an error since get_full_name does not exist in parent class |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from datetime import date | ||
|
||
class Person: | ||
def __init__(self, name: str, DoB: date, preferred_operating_system: str): | ||
self.name = name | ||
self.DoB = DoB | ||
self.preferred_operating_system = preferred_operating_system | ||
|
||
def is_adult(self)-> bool: | ||
current_date = date.today() | ||
age = current_date.year - self.DoB.year - ( | ||
(current_date.month, current_date.day) < (self.DoB.month, self.DoB.day) | ||
) | ||
return age >= 18 | ||
|
||
imran = Person("Imran", date(1998, 1, 6), "Ubuntu") | ||
print(imran.is_adult()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mypy |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
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", "macOS"]), | ||
Person(name="Eliza", age=34, preferred_operating_systems=["Arch Linux", "macOS", "Ubuntu" ]), | ||
] | ||
|
||
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}") | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
def open_account(balances:dict[str, float], name:str, amount:float)-> None: | ||
balances[name] = amount | ||
|
||
def sum_balances(accounts:dict[str, float])->float: | ||
total = 0.00 | ||
for name, pence in accounts.items(): | ||
print(f"{name} had balance {pence}") | ||
total += pence | ||
return total | ||
|
||
def format_pence_as_string(total_pence:float) -> 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": 7.00, | ||
"Linn": 5.45, | ||
"Georg": 8.31, | ||
} | ||
|
||
open_account(balances, "Tobi", 9.13) | ||
open_account(balances, "Olya", 7.13) | ||
|
||
total_pence = sum_balances(balances) | ||
total_string = format_pence_as_string(total_pence) | ||
|
||
print(f"The bank accounts total {total_string}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
def half(value): | ||
return value / 2 | ||
|
||
def double(value): | ||
return value * 2 | ||
|
||
def second(value): | ||
return value[1] | ||
|
||
|
||
|
||
# print(half(22)) | ||
# print(half("hello")) | ||
# print(half("22")) | ||
|
||
# print(double(22)) | ||
# print(double("hello")) | ||
# print(double("22")) | ||
|
||
# print(second(22)) | ||
# print(second(0x16)) | ||
# print(second("hello")) | ||
# print(second("22")) | ||
|
||
print(double("22")) # will print 2222 | ||
eediallo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def triple(number): | ||
return number * 3 | ||
|
||
print(triple(10)) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.