-
-
Notifications
You must be signed in to change notification settings - Fork 105
London | 26 - JUL-SDC| Ping Wang |Sprint 5 |Spring 5 exercises-CYF 1155 #647
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
2d6dd40
20478fb
8a8c9ab
998fc6c
2caed1e
1be1836
5728b92
24c5264
3fee2a3
8149557
74bb1c7
6742d7b
9688c02
b1c18c3
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 |
|---|---|---|
| @@ -1,2 +1,7 @@ | ||
|
|
||
| node_modules | ||
| testoutput.txt | ||
|
|
||
| .venv/ | ||
| __pycache__/ | ||
| *.pyc |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| 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, | ||
| "George": 831, | ||
| } | ||
|
|
||
| open_account(balances,"Toby", 913) | ||
| open_account(balances,"Olive", 713) | ||
|
|
||
| total_pence = sum_balances(balances) | ||
| total_string = format_pence_as_string(total_pence) | ||
|
|
||
| print(f"The bank accounts total {total_string}") | ||
|
|
||
| # i amended three errors from original code from prep, line 28 function name is format_pence-as_string rather than format_pence_as_str, line 24 ad 25, supposed | ||
| # three arguments instead of 2, so i add balances when we call open_account and also call open_account amount should be integer rather than string or decimal |
| 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,address:str): | ||
| self.name = name | ||
| self.age = age | ||
| self.preferred_operating_system = preferred_operating_system | ||
| self.address=address | ||
|
|
||
| amy = Person("Amy", 22, "Ubuntu", "23 main road") | ||
| print(amy.name) | ||
|
|
||
| eliza = Person("Eliza", 34, "Arch Linux","32 divert way") | ||
| print(eliza.name) | ||
|
|
||
| def is_adult(person: Person) -> bool: | ||
| return person.age >= 18 | ||
|
|
||
| print(is_adult(amy)) | ||
|
|
||
| def home_address(person :Person): | ||
| return person.address | ||
|
|
||
| print(home_address(eliza)) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| 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) -> 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 | ||
|
|
||
| amy= Person("Amy", date(2011,11,23), "Ubuntu") | ||
| amy2= Person("Amy", date(2011,11,23), "Ubuntu") | ||
|
|
||
| print(amy) | ||
| print(amy2) | ||
| print(amy==amy2) | ||
| print(amy.is_adult()) | ||
|
|
||
|
|
||
|
|
| 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"] | ||
|
|
||
| france = Person(name="France", age=5, children=[]) | ||
| aisha = Person(name="Aisha", age= 1, children=[]) | ||
|
|
||
| amy = Person(name="Amy",age = 32, children=[france, 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(amy) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| class Parent: | ||
| def __init__(self, first_name: str, last_name: str): | ||
| self.first_name = first_name | ||
| self.last_name = last_name | ||
|
|
||
| def get_full_name(self) -> str: | ||
| return f"{self.first_name} {self.last_name}" | ||
|
|
||
| def change_last_name(self, last_name: str) -> None: | ||
| self.last_name = 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("Ella", "Alice") | ||
| print(person1.get_full_name()) | ||
| person1.change_last_name("Tina") | ||
| print(person1.get_full_name()) | ||
|
|
||
| person2 = Parent("Ella", "Alice") | ||
| print(person2.get_full_name()) | ||
| person2.change_last_name("Tina") | ||
| print(person2.get_full_name()) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| # Think of the advantages of using methods instead of free functions: | ||
| # Better organization, More readable code, Easier maintenance,Better editor support (autocomplete),Code reuse across all instances of the class | ||
| # Ease of documentation: Related data and behavior are grouped together, making the class easier to understand. | ||
| # Encapsulation: The implementation can change without affecting the rest of the program, as long as the method's interface stays the same. | ||
|
|
||
| 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): | ||
| 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 | ||
|
|
||
| amy = Person("Amy",date(2003,1,5), "Ubuntu") | ||
| eliza = Person("Eliza",date(2012,11,25), "Ubuntu") | ||
|
|
||
| print(amy.is_adult()) | ||
| print(eliza.is_adult()) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| from dataclasses import dataclass | ||
| from enum import Enum | ||
| 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 | ||
|
|
||
|
|
||
| laptops = [ | ||
| Laptop(1, "Dell", "XPS", 13, OperatingSystem.ARCH), | ||
| Laptop(2, "Dell", "XPS", 15, OperatingSystem.UBUNTU), | ||
| Laptop(3, "Dell", "XPS", 15, OperatingSystem.UBUNTU), | ||
| Laptop(4, "Apple", "MacBook", 13, OperatingSystem.MACOS), | ||
| ] | ||
|
|
||
|
|
||
| # Read name | ||
| name = input("Enter your name: ") | ||
|
|
||
| # Read and convert age | ||
| try: | ||
| age = int(input("Enter your age: ")) | ||
| except ValueError: | ||
| print("Error: age must be a whole number.", file=sys.stderr) | ||
| sys.exit(1) | ||
|
|
||
| # Read and convert operating system | ||
| print("Choose an operating system:") | ||
| print("- Ubuntu") | ||
| print("- Arch Linux") | ||
| print("- macOS") | ||
|
|
||
| while True: | ||
| os_input = input("Preferred operating system: ") | ||
|
|
||
| try: | ||
| preferred_os = OperatingSystem(os_input) | ||
| break | ||
| except ValueError: | ||
| print("Please choose Ubuntu, Arch Linux, or macOS.") | ||
|
|
||
| # Create the person | ||
| person = Person(name, age, preferred_os) | ||
|
|
||
| # Find matching laptops | ||
| matching = [ | ||
| laptop | ||
| for laptop in laptops | ||
| if laptop.operating_system == person.preferred_operating_system | ||
| ] | ||
|
|
||
| print( | ||
| f"\nThe library has {len(matching)} laptop(s) running " | ||
| f"{person.preferred_operating_system.value}." | ||
| ) | ||
|
|
||
| # Count laptops for each operating system | ||
| counts = {} | ||
|
|
||
| for laptop in laptops: | ||
| os = laptop.operating_system | ||
| counts[os] = counts.get(os, 0) + 1 | ||
|
|
||
| best_os = max(counts, key=counts.get) | ||
|
|
||
| if best_os != person.preferred_operating_system: | ||
| print( | ||
| f"If you're willing to accept {best_os.value}, " | ||
| f"there are {counts[best_os]} laptops available." | ||
| ) |
| 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_system: str | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class Laptop: | ||
| id: int | ||
| manufacturer: str | ||
| model: str | ||
| screen_size_in_inches: float | ||
| operating_system: List [str] | ||
|
|
||
|
|
||
| def find_possible_laptops(laptops: List[Laptop], person: Person) -> List[Laptop]: | ||
| possible_laptops = [] | ||
| for laptop in laptops: | ||
| if person.preferred_operating_system in laptop.operating_system: | ||
| possible_laptops.append(laptop) | ||
| return possible_laptops | ||
|
|
||
|
|
||
| people = [ | ||
| Person(name="Amy", age=22, preferred_operating_system="Ubuntu"), | ||
| Person(name="Eliza", age=34, preferred_operating_system="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) | ||
|
Contributor
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. When I run this, the suggested laptops are empty? Do you also see this?
Author
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. yes i do because operating_system is a list but person.preferred_operating_system is a string, so i amend person preferred operation system to list. thanks |
||
| print(f"Possible laptops for {person.name}: {possible_laptops}") | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| def double(number): | ||
| return number * 3 | ||
|
|
||
| print(double(10)) | ||
|
|
||
| # Because double conflicts with multiplying 3 so we can change function name to 'triple' or '*2' | ||
|
|
||
| def triple(number): | ||
| return number * 3 | ||
|
|
||
| print(triple(10)) | ||
|
|
||
|
|
||
| def double(number): | ||
| return number * 2 | ||
|
|
||
| print(double(10)) |
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 add explicit type annotations here?
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.
thanks i add type annotations when i define function