-
-
Notifications
You must be signed in to change notification settings - Fork 90
London | 26-SDC-MAR | Jamal Laqdiem | Sprint 5 | Prep-exercises #507
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
jamallaqdiem
wants to merge
7
commits into
CodeYourFuture:main
Choose a base branch
from
jamallaqdiem: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.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8663043
feat: Added .venv to gitignore file, and finished the exercise one.
jamallaqdiem 82dae40
feat: Add the birthday logic to the method.
jamallaqdiem 9096891
feat: Used dataclass decorator instead of the manual _init_.
jamallaqdiem 6f365fc
fix:Added the age variable in Person class.
jamallaqdiem ff6cdec
Fix: fixing the mypy errors .
jamallaqdiem d3717ca
Predict and ply computer.
jamallaqdiem 1e84208
feat: iImplement the laptop exercise.
jamallaqdiem 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 |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| node_modules | ||
| .venv |
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 | ||
|
|
||
| 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" |
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,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()}") | ||
|
|
||
|
|
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,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.") |
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 =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) |
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,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. |
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 @@ | ||
| 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()}") | ||
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,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}") |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The explanations of methods vs functions is missing