-
-
Notifications
You must be signed in to change notification settings - Fork 92
Glasgow | 26-SDC-Mar | Mohammed Abdoon | Sprint 5 | Prep exercises #525
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
M-Abdoon
wants to merge
5
commits into
CodeYourFuture:main
Choose a base branch
from
M-Abdoon: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
5 commits
Select commit
Hold shift + click to select a range
cddb646
exercises double, double2 and types checking with mypy
M-Abdoon 91bdeda
Person exercises
M-Abdoon 8ddccac
other exercises
M-Abdoon 370025e
dataclasses and laptopAllocation exercises
M-Abdoon d691338
enums and inheritance exercises
M-Abdoon 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,5 @@ | ||
| Methods make the code more organised because everything stays inside the class. | ||
| It also looks cleaner and easier to read. | ||
| imran.is_adult() looks better than is_adult(imran) | ||
| Its easier to edit later because you only change the code in one place. | ||
| Methods also make more sense because the function belongs to the object itself. |
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,25 @@ | ||
| 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) | ||
|
|
||
| # ------------- | ||
|
|
||
| def is_adult(person: Person) -> bool: | ||
| return person.age >= 18 | ||
|
|
||
| print(is_adult(imran)) | ||
|
|
||
| # ------------- | ||
|
|
||
| def birthday(person: Person) -> str: | ||
| return person.birthday |
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,38 @@ | ||
| from datetime import date | ||
|
|
||
|
|
||
| class Person: | ||
| def __init__( | ||
| self, | ||
| name: str, | ||
| date_of_birth: date, | ||
| preferred_operating_system: str, | ||
| ): | ||
| self.name = name | ||
| self.date_of_birth = date_of_birth | ||
| self.preferred_operating_system = preferred_operating_system | ||
|
|
||
| def is_adult(self) -> bool: | ||
| today = 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", | ||
| date(2002, 5, 10), | ||
| "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,25 @@ | ||
| from dataclasses import dataclass | ||
| from datetime import date | ||
|
|
||
|
|
||
| @dataclass | ||
| class Person: | ||
| name: str | ||
| date_of_birth: date | ||
| preferred_operating_system: str | ||
|
|
||
| def is_adult(self): | ||
| today = 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", date(2002, 5, 10), "Ubuntu") | ||
|
|
||
| print(imran) | ||
| 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,14 @@ | ||
| def half(value): | ||
| return value / 2 | ||
|
|
||
| def double(value): | ||
| return value * 2 | ||
|
|
||
| def second(value): | ||
| return value[1] | ||
|
|
||
| print(double("22")) | ||
|
|
||
| # My prediction is that double("22") will return 44 | ||
| # When running the code, it did not return what I expected. instead, the output is '2222' | ||
| # It returned '2222' because the input ( 22 ) is a text not a number |
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,8 @@ | ||
| def double(number): | ||
| return number * 3 | ||
|
|
||
| print(double(10)) | ||
|
|
||
| # The but in this code is that the parameter type of the variable number is not | ||
| # specified, the function takes whatever passed and multiplies it by 3, so if we | ||
| # pass a string it will concatenate the string 3 times instead of multiplying the number by 3. |
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,41 @@ | ||
| from enum import Enum | ||
| from dataclasses import dataclass | ||
|
|
||
|
|
||
| class OS(Enum): | ||
| UBUNTU = "Ubuntu" | ||
| MAC = "Mac" | ||
| WINDOWS = "Windows" | ||
|
|
||
|
|
||
| @dataclass | ||
| class Laptop: | ||
| model: str | ||
| os: OS | ||
|
|
||
|
|
||
| @dataclass | ||
| class Person: | ||
| name: str | ||
| age: int | ||
| preferred_os: OS | ||
|
|
||
|
|
||
| laptops = [ | ||
| Laptop("Dell", OS.UBUNTU), | ||
| Laptop("MacBook", OS.MAC), | ||
| Laptop("HP", OS.WINDOWS), | ||
| ] | ||
|
|
||
| person = Person("Ali", 22, OS.UBUNTU) | ||
|
|
||
|
|
||
| def match(laptops, person): | ||
| result = [] | ||
| for l in laptops: | ||
| if l.os == person.preferred_os: | ||
| result.append(l) | ||
| return result | ||
|
|
||
|
|
||
| print(match(laptops, person)) |
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,23 @@ | ||
| class Animal: | ||
| def __init__(self, name): | ||
| self.name = name | ||
|
|
||
| def speak(self): | ||
| return "some sound" | ||
|
|
||
|
|
||
| class Dog(Animal): | ||
| def speak(self): | ||
| return "Woof!" | ||
|
|
||
|
|
||
| class Cat(Animal): | ||
| def speak(self): | ||
| return "Meow!" | ||
|
|
||
|
|
||
| dog = Dog("Rex") | ||
| cat = Cat("Milo") | ||
|
|
||
| print(dog.name, dog.speak()) | ||
| print(cat.name, cat.speak()) |
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 | ||
| from enum import Enum | ||
|
|
||
|
|
||
| class OperatingSystem(Enum): | ||
| UBUNTU = "Ubuntu" | ||
| MAC = "Mac" | ||
| WINDOWS = "Windows" | ||
|
|
||
|
|
||
| @dataclass | ||
| class Person: | ||
| name: str | ||
| age: int | ||
| preferred_os: OperatingSystem | ||
|
|
||
|
|
||
| @dataclass | ||
| class Laptop: | ||
| model: str | ||
| os: OperatingSystem | ||
|
|
||
|
|
||
| def get_matching_laptops(laptops: List[Laptop], person: Person) -> List[Laptop]: | ||
| matches = [] | ||
|
|
||
| for laptop in laptops: | ||
| if laptop.os == person.preferred_os: | ||
| matches.append(laptop) | ||
|
|
||
| return matches | ||
|
|
||
|
|
||
| laptops = [ | ||
| Laptop("Dell", OperatingSystem.UBUNTU), | ||
| Laptop("MacBook", OperatingSystem.MAC), | ||
| Laptop("HP", OperatingSystem.WINDOWS), | ||
| ] | ||
|
|
||
| person = Person("Ali", 22, OperatingSystem.UBUNTU) | ||
|
|
||
| print(get_matching_laptops(laptops, person)) | ||
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, 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 = { | ||
| "Sima": 700, | ||
| "Linn": 545, | ||
| "Georg": 831, | ||
| } | ||
|
|
||
| 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}") |
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.
Can you consider a fallback, that way they still get an "imperfect" match if there are no preferred laptops left, or notify them somehow?