diff --git a/.gitignore b/.gitignore index 3c3629e64..28f1ba756 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules +.DS_Store \ No newline at end of file diff --git a/individual-shell-tools/ls/script-02.sh b/individual-shell-tools/ls/script-02.sh index d0a5a10f4..9fa05d88b 100755 --- a/individual-shell-tools/ls/script-02.sh +++ b/individual-shell-tools/ls/script-02.sh @@ -4,3 +4,4 @@ set -euo pipefail # TODO: Write a command which lists all of the files in the directory named child-directory. # The output should be a list of names: helper-1.txt, helper-2.txt, helper-3.txt. +ls child-directory \ No newline at end of file diff --git a/jq/.gitignore b/jq/.gitignore new file mode 100644 index 000000000..e43b0f988 --- /dev/null +++ b/jq/.gitignore @@ -0,0 +1 @@ +.DS_Store diff --git "a/prep/1_double(\"22\").py" "b/prep/1_double(\"22\").py" new file mode 100644 index 000000000..435054a79 --- /dev/null +++ "b/prep/1_double(\"22\").py" @@ -0,0 +1,10 @@ +# ✍️exercise +# Predict what double("22") will do. Then run the code and check. Did it do what you expected? +# Why did it return the value it did? + +def double(value): + return value * 2 + +#2222 + +print(double("22")) diff --git a/prep/1_double_bug.py b/prep/1_double_bug.py new file mode 100644 index 000000000..21b70b173 --- /dev/null +++ b/prep/1_double_bug.py @@ -0,0 +1,10 @@ +# ✍️exercise +# Read the above code and write down what the bug is. +# How would you fix it? + +def double(number): + return number * 3 + +print(double(10)) + +#Either call it tripple or * 2 diff --git a/prep/2_type_bugs.py b/prep/2_type_bugs.py new file mode 100644 index 000000000..3e5fa7a9a --- /dev/null +++ b/prep/2_type_bugs.py @@ -0,0 +1,43 @@ +# ✍️exercise +# Do not run the following code. + +# This code contains bugs related to types. They are bugs mypy can catch. + +# Read this code to understand what it’s trying to do. +# Add type annotations to the method parameters and return types of this code. +# Run the code through mypy, and fix all of the bugs that show up. +# When you’re confident all of the type annotations are correct, and the bugs are fixed, +# run the code and check it works. + +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}" +# File "C:\Users\kkazi\Desktop\cyf\Module-Tools\prep\3.py", line 27, in format_pence_as_string + # return f"£{pounds}.{pence:02d}" + #^^^^^^^^^^^ +#ValueError: Unknown format code 'd' for object of type 'float' +balances = { + "Sima": 700, + "Linn": 545, + "Georg": 831, +} + +open_account(balances, "Tobi", 913) # to solve the float int issue value passed in in pence3.py:35: error: Missing positional argument "amount" in call to "open_account" [call-arg] +open_account(balances, "Olya", 713) #3.py:36: error: Missing positional argument "amount" in call to "open_account" [call-arg] + +total_pence = sum_balances(balances) +total_string = format_pence_as_string(total_pence) # 3.py:39: error: Name "format_pence_as_str" is not defined [name-defined] +print(f"The bank accounts total {total_string}") diff --git a/prep/3_mypy_error_and_is_adult.py b/prep/3_mypy_error_and_is_adult.py new file mode 100644 index 000000000..e0996dddc --- /dev/null +++ b/prep/3_mypy_error_and_is_adult.py @@ -0,0 +1,33 @@ +# Exercise +# Add the is_adult code to the file you saved earlier. + +# Run it through mypy - notice that no errors are reported - mypy understands that Person has a property named age so is happy with the function. + +# Write a new function in the file that accepts a Person as a parameter and tries to access a property that doesn’t exist. +# Run it through mypy and check that it does report an error. + +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)) +#error means prop does not exist + +def get_eye_colour(person: Person): + return person.eye_colour + +print(get_eye_colour(imran)) +#error in mypy no attribute diff --git a/prep/4_Person_class_exercise.py b/prep/4_Person_class_exercise.py new file mode 100644 index 000000000..1bc569f3f --- /dev/null +++ b/prep/4_Person_class_exercise.py @@ -0,0 +1,27 @@ +# exercise +# Change the Person class to take a date of birth +# (using the standard library’s datetime.date class) and store it in a +# field instead of age. + +# Update the is_adult method to act the same as before. + +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): + today = date.today() + age = date.today().year - self.dob.year + if (today.month, today.day) < (self.dob.month, self.dob.day): + age -= 1 + return age >= 18 + +imran = Person("Imran", date(1988, 10, 10), "Ubuntu") +print(imran.is_adult()) +imran = Person("Jay", date(2015, 10, 10), "Ubuntu") +print(imran.is_adult()) diff --git a/prep/4_advantages_of_methods.py b/prep/4_advantages_of_methods.py new file mode 100644 index 000000000..8afa5b179 --- /dev/null +++ b/prep/4_advantages_of_methods.py @@ -0,0 +1,7 @@ +# exercise +# Think of the advantages of using methods instead of free functions. Write them down in your notebook. +1 makes data and logic more organised +2 makes it easier to find and reuse code +3 makes the code more specific and it is easier to understand what it is doing for + a junior/learner like me :) +4 reduces errors diff --git a/prep/5_Person_dataclass_exercise.py b/prep/5_Person_dataclass_exercise.py new file mode 100644 index 000000000..1f1cf855c --- /dev/null +++ b/prep/5_Person_dataclass_exercise.py @@ -0,0 +1,30 @@ +# ✍️exercise +# Write a Person class using @datatype which uses a datetime.date +# for date of birth, rather than an int for age. + +# Re-add the is_adult method to it. + +from dataclasses import dataclass +from datetime import date + +@dataclass +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 + name: str + dob: date + preferred_operating_system: str + + def is_adult(self): + today = date.today() + age = date.today().year - self.dob.year + if (today.month, today.day) < (self.dob.month, self.dob.day): + age -= 1 + return age >= 18 + +imran = Person("Imran", date(1988, 10, 10), "Ubuntu") +print(imran.is_adult()) +imran = Person("Jay", date(2015, 10, 10), "Ubuntu") +print(imran.is_adult()) diff --git a/prep/6_fix_code.py b/prep/6_fix_code.py new file mode 100644 index 000000000..c00bae199 --- /dev/null +++ b/prep/6_fix_code.py @@ -0,0 +1,25 @@ +# ✍️exercise +# Fix the above code so that it works. You must not change the print +# on line 17 - we do want to print the children’s ages. +# (Feel free to invent the ages of Imran’s children.) + +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=12, children=[]) +aisha = Person(name="Aisha", age=25, children=[]) + +imran = Person(name="Imran", age=67, 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) diff --git a/prep/7_type_values_fix.py b/prep/7_type_values_fix.py new file mode 100644 index 000000000..1184f82f1 --- /dev/null +++ b/prep/7_type_values_fix.py @@ -0,0 +1,61 @@ +# ✍️exercise +# Try changing the type annotation of Person.preferred_operating_system +# from str to List[str]. + +# Run mypy on the code. + +# It tells us different places that our code is now wrong, because we’re +# passing values of the wrong +# type. + +# We probably also want to rename our field - lists are plural. Rename +# the field to preferred_operating_systems. + +# Run mypy again. + +# Fix all of the places that mypy tells you need changing. Make sure the +# program works as you’d expect. + +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 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}") + \ No newline at end of file diff --git a/prep/8_laptop_program.py b/prep/8_laptop_program.py new file mode 100644 index 000000000..6c98403cc --- /dev/null +++ b/prep/8_laptop_program.py @@ -0,0 +1,115 @@ +# exercise +# Write a program which: + +# Already has a list of Laptops that a library has to lend out. +# Accepts user input to create a new Person - it should use the input +# function to read a person’s name, age, and preferred operating system. +# Tells the user how many laptops the library has that have that +# operating system. +# If there is an operating system that has more laptops available, +# tells the user that if they’re willing to accept that operating +# system they’re more likely to get a laptop. +# You should convert the age and preferred operating system input from +# the user into more constrained types as quickly as possible, and +# should output errors to stderr and terminate +# the program with a non-zero exit code if the user input bad values. + +from dataclasses import dataclass +from enum import Enum +from typing import List +from collections import Counter +import sys +from typing import Tuple + +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 create_a_new_person() -> Person: + add_name = input("What is your name? \n") + if not add_name or add_name == "" or len(add_name) > 50: + print("Enter a valid name.", file=sys.stderr) + sys.exit(1) + + str_add_age = input("What is your age? \n") + if not str_add_age.isdigit(): + print("Enter a number.", file=sys.stderr) + sys.exit(1) + add_age = int(str_add_age) + + if add_age <= 0 or add_age > 100: + print("Enter a valid age.", file=sys.stderr) + sys.exit(1) + + add_os = input("What is your preferred operating system? \n") + + if add_os not in [os.value for os in OperatingSystem]: + print("Enter a valid operating system.", file=sys.stderr) + sys.exit(1) + + return Person(name=add_name, age=add_age, preferred_operating_system=OperatingSystem(add_os)) + + +def find_possible_laptops(laptops: List[Laptop], person: Person) -> Tuple[List[Laptop], List[Laptop]]: + possible_laptops = [] + other_laptops = [] + for laptop in laptops: + if laptop.operating_system == person.preferred_operating_system: + possible_laptops.append(laptop) + else: + other_laptops.append(laptop) + return possible_laptops, other_laptops + +def compare_counts_of_poss_and_others(possible_laptops: List[Laptop], other_laptops: List[Laptop], person: Person) -> None: + if len(other_laptops) == 0: + print("The os you chose has the biggest availability.") + return + # counter to get obj + other_laptops_counted_as_obj = Counter(laptop.operating_system for laptop in other_laptops) + # and here turn to tuple os+count + most_common_laptop = other_laptops_counted_as_obj.most_common(1)[0] + + if len(possible_laptops) < most_common_laptop[1]: + print(f"There are more laptops available with {most_common_laptop[0].value} than your preferred OS {person.preferred_operating_system.value}. Consider accepting that OS to increase your chances of getting a laptop.") + else: + print("Your chosen os has the biggest availability.") + +people = [ + ] + +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), +] + +for person in people: + possible_laptops, other_laptops = find_possible_laptops(laptops, person) + print(f"Possible laptops for {person.name}: {possible_laptops}") + + +def execute(): + new_person = create_a_new_person() + possible_laptops, other_laptops = find_possible_laptops(laptops, new_person) + print(f"Number of laptops matching your preferred OS: {len(possible_laptops)}") + compare_counts_of_poss_and_others(possible_laptops, other_laptops, new_person) + + +execute() diff --git a/prep/9_play_computer.py b/prep/9_play_computer.py new file mode 100644 index 000000000..139cead21 --- /dev/null +++ b/prep/9_play_computer.py @@ -0,0 +1,40 @@ +# Play computer with this code. Predict what you expect each line will do. +# Then run the code and check your predictions. (If any lines cause +# errors, you may need to comment them out to check later lines). + +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()) # prints Elizaveta Alekseeva +print(person1.get_full_name()) # prints Elizaveta Alekseeva +person1.change_last_name("Tyurina") # no print +print(person1.get_name()) # prints Elizaveta Tyurina +print(person1.get_full_name()) # prints Elizaveta Tyurina (née Alekseeva) + +person2 = Parent("Elizaveta", "Alekseeva") +print(person2.get_name()) #print Elizaveta Alekseeva +print(person2.get_full_name()) #no full name method +person2.change_last_name("Tyurina") #no change last name method +print(person2.get_name()) # print Elizaveta Alekseeva +print(person2.get_full_name()) #no full name method so err