-
-
Notifications
You must be signed in to change notification settings - Fork 21
London | 25-SDC-July | AYK | Sprint 4 | Prep exercises #45
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
base: main
Are you sure you want to change the base?
Changes from all commits
9a18513
f18bcf6
523289a
289488b
87645d3
6e1b00a
0c3a38d
adedd2e
cad3bb2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
|
|
||
| { | ||
| "python.linting.enabled": true, | ||
| "python.linting.mypyEnabled": true, | ||
| "python.linting.mypyArgs": ["--ignore-missing-imports"], | ||
| "python.analysis.typeCheckingMode": "basic" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| 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) | ||
|
|
||
| eliza = Person("Eliza", 34, "Arch Linux") | ||
| print(eliza.name) | ||
|
|
||
|
|
||
| def is_adult (person: Person) ->bool: | ||
| return person.age >=18 | ||
|
|
||
| print(is_adult(imran)) | ||
|
|
||
| def current_address (address : Person): | ||
| return address.location; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| from dataclasses import dataclass | ||
| from datetime import date | ||
|
|
||
| @dataclass | ||
| class Person: | ||
| name: str | ||
| dob: date | ||
| preferred_operating_system: str | ||
|
|
||
| def is_adult(self) -> bool: | ||
| today = date.today() | ||
| age = today.year - self.dob.year - ((today.month,today.day)<(self.dob.month,self.dob.day)) | ||
| return age >= 18 | ||
|
|
||
| # Example usage | ||
| person1 = Person(name="AYK", dob=date(1989, 7, 15), preferred_operating_system="mac") | ||
|
|
||
| print(person1.name) # AYK | ||
| print(person1.is_adult()) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| 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]: | ||
| return [laptop for laptop in laptops if laptop.operating_system == person.preferred_operating_system] | ||
|
|
||
|
|
||
| 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), | ||
| ] | ||
|
|
||
|
|
||
| name = input("Enter your name: ").strip() | ||
| age_input = input("Enter your age: ").strip() | ||
| age = int(age_input) | ||
| if age <= 0: | ||
| print("Age must be positive") | ||
| print("Available Operating Systems: macOS, Arch Linux, Ubuntu") | ||
| os_input = input("Enter your preferred operating system: ").strip().lower() | ||
|
|
||
|
|
||
| os_map = {os.value.lower(): os for os in OperatingSystem} | ||
| if os_input not in os_map: | ||
| print(f"Invalid operating system '{os_input}'") | ||
| preferred_os = os_map[os_input] | ||
|
|
||
|
|
||
| sys.exit(1) | ||
|
|
||
|
|
||
| person = Person(name=name, age=age, preferred_operating_system=preferred_os) | ||
|
|
||
|
|
||
| matching_laptops = find_possible_laptops(laptops, person) | ||
| print(f"\nHello {person.name}! There are {len(matching_laptops)} laptop(s) with {person.preferred_operating_system.value} available.") | ||
|
|
||
|
|
||
| os_counts = {os: 0 for os in OperatingSystem} | ||
| for laptop in laptops: | ||
| os_counts[laptop.operating_system] += 1 | ||
|
|
||
|
|
||
| max_os = None | ||
| max_count = 0 | ||
|
|
||
| for os, count in os_counts.items(): | ||
| if count > max_count: | ||
| max_os = os | ||
| max_count = count | ||
|
|
||
| if max_os != person.preferred_operating_system and max_count > len(matching_laptops): | ||
| print(f"Tip: There are more laptops available with {max_os.value}.") |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| from dataclasses import dataclass | ||
| from typing import List | ||
|
|
||
| @dataclass(frozen=True) | ||
| class Person: | ||
| name: str | ||
| children: List["Person"] | ||
| age : int | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you solve this in a way that it always prints the current age, rather than hard-coding a specific age? |
||
|
|
||
|
|
||
| fatma = Person(name="Fatma", children=[],age = 4) | ||
| aisha = Person(name="Aisha", children=[],age =2) | ||
|
|
||
| imran = Person(name="Imran", children=[fatma, aisha],age=33) | ||
|
|
||
| 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) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| 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: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is a benefit or drawback of writing the function this way? |
||
| today = date.today() | ||
| age = today.year - self.dob.year - ((today.month,today.day)<(self.dob.month,self.dob.day)) | ||
| return age >=18 | ||
|
|
||
| person1 = Person("AYK",date(1990,8,15),"mac") | ||
|
|
||
| print (person1.name + " born on " + str(person1.dob)+ " and like " + person1.preferred_operating_system) | ||
| print(person1.is_adult()) | ||
| 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 [os for os in person.preferred_operating_systems]: | ||
| possible_laptops.append(laptop) | ||
| return possible_laptops | ||
|
|
||
|
|
||
| people = [ | ||
| Person(name="Imran", age=22, preferred_operating_systems= ["Arch Linux","Ubuntu"]), | ||
| Person(name="Eliza", age=34, preferred_operating_systems=["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}") |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| def open_account(balances, name, amount): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Part of the bank account task involved making type annotations, can you try that? |
||
| balances[name] = amount | ||
|
|
||
| def sum_balances(accounts): | ||
| 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): | ||
| 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", 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}") | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| python_version = 3.9.6 | ||
| warn_return_any = True | ||
| warn_unused_configs = True | ||
| disallow_untyped_defs = True | ||
| ignore_missing_imports = True |
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.
Did you see what error this gives?