diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..c1cb9df --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,7 @@ + + { + "python.linting.enabled": true, + "python.linting.mypyEnabled": true, + "python.linting.mypyArgs": ["--ignore-missing-imports"], + "python.analysis.typeCheckingMode": "basic" + } diff --git a/AYK/class&object.py b/AYK/class&object.py new file mode 100644 index 0000000..c774bba --- /dev/null +++ b/AYK/class&object.py @@ -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; + diff --git a/AYK/dataclass.py b/AYK/dataclass.py new file mode 100644 index 0000000..d74f2d6 --- /dev/null +++ b/AYK/dataclass.py @@ -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()) \ No newline at end of file diff --git a/AYK/enum.py b/AYK/enum.py new file mode 100644 index 0000000..d72587e --- /dev/null +++ b/AYK/enum.py @@ -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}.") \ No newline at end of file diff --git a/AYK/generic.py b/AYK/generic.py new file mode 100644 index 0000000..893702a --- /dev/null +++ b/AYK/generic.py @@ -0,0 +1,21 @@ +from dataclasses import dataclass +from typing import List + +@dataclass(frozen=True) +class Person: + name: str + children: List["Person"] + age : int + + +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) \ No newline at end of file diff --git a/AYK/method.py b/AYK/method.py new file mode 100644 index 0000000..aa89f0d --- /dev/null +++ b/AYK/method.py @@ -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: + 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()) \ No newline at end of file diff --git a/AYK/refactorings.py b/AYK/refactorings.py new file mode 100644 index 0000000..ab715b5 --- /dev/null +++ b/AYK/refactorings.py @@ -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}") \ No newline at end of file diff --git a/AYK/typechecking_mypy.py b/AYK/typechecking_mypy.py new file mode 100644 index 0000000..c407442 --- /dev/null +++ b/AYK/typechecking_mypy.py @@ -0,0 +1,30 @@ +def open_account(balances, name, amount): + 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}") \ No newline at end of file diff --git a/mypy.ini b/mypy.ini new file mode 100644 index 0000000..323ce15 --- /dev/null +++ b/mypy.ini @@ -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 \ No newline at end of file