diff --git a/_coverpage.md b/_coverpage.md index f3e602e..2c3baaf 100644 --- a/_coverpage.md +++ b/_coverpage.md @@ -1,10 +1,9 @@ -# Welcome +# DevOps Digest ![logo](assets/images/logo.png) -## DevOps Digest [![GitHub stars](https://img.shields.io/github/stars/saranmahadev/devops-digest.svg)](https://GitHub.com/saranmahadev/devops-digest/stargazers/) [![GitHub Commit](https://img.shields.io/github/last-commit/saranmahadev/devops-digest)](https://GitHub.com/saranmahadev/devops-digest/commits/) [![GitHub license](https://img.shields.io/github/license/saranmahadev/devops-digest.svg)](https://github.com/saranmahadev/devops-digest/blob/master/LICENSE) [![GitHub Issues](https://img.shields.io/github/issues-closed-raw/saranmahadev/devops-digest)](https://github.com/saranmahadev/devops-digest/issues) -[GitHub](https://github.com/saranmahadev/devops-digest/) -[Get Started](./) \ No newline at end of file +[Get Started](./) + \ No newline at end of file diff --git a/_sidebar.md b/_sidebar.md index 3a4135a..fa3135f 100644 --- a/_sidebar.md +++ b/_sidebar.md @@ -6,4 +6,7 @@ - [📚 Golang](programming/go) - [📚 Ruby](programming/ruby) - [📚 Perl](programming/perl) - - [🗒 Exercises](programming/exercises) \ No newline at end of file + - [🗒 Exercises](programming/exercises) + - [⏮ Self Assessment - Page 1](programming/assessment-1) + - [⏮ Self Assessment - Page 2](programming/assessment-2) + diff --git a/assets/images/134_3_1146049_1642512581_AWS Course Completion Certificate.pdf b/assets/images/134_3_1146049_1642512581_AWS Course Completion Certificate.pdf new file mode 100644 index 0000000..e7d0454 Binary files /dev/null and b/assets/images/134_3_1146049_1642512581_AWS Course Completion Certificate.pdf differ diff --git a/index.html b/index.html index 13c1722..01be765 100644 --- a/index.html +++ b/index.html @@ -11,6 +11,10 @@ + + + +
@@ -56,6 +60,12 @@ + + + + + + diff --git a/os/index.md b/os/index.md new file mode 100644 index 0000000..e69de29 diff --git a/programming/assessment-1.md b/programming/assessment-1.md new file mode 100644 index 0000000..da0a87b --- /dev/null +++ b/programming/assessment-1.md @@ -0,0 +1,1564 @@ +# Self Assessment + > “The secret to getting ahead is getting started.” - Mark Twain + ++ Must Read + + + Read the Assessment *[or]* Try to Answer the Questions without Opening the Accordion. ++ What are some characteristics of the Python programming language? + + + - General Purpose Programming Language + - Interpreted + - Strongly Typed (i.e. type checking and type inference is enabled) + - High Readability + - Lot of libraries + - Everything is object oriented + ++ What built-in types Python has? + + + - Text Type: str + - Numeric Types: int, float, complex + - Sequence Types: list, tuple, range + - Mapping Type: dict + - Set Types: set, frozenset + - Boolean Type: bool + - Binary Types: bytes, bytearray, memoryview + ++ What are the Mutable and Immutable Datatypes? + + + 1. Mutable: List, Dictionary, Set + 2. Immutable: String, Integer, Float, Boolean, None + ++ What is Mutability? + + + - Mutability refers to the ability to change/modify the value of an object. + - Mutable objects can be changed. + - Immutable objects cannot be changed. + + +#### **Program** + +``` +1 > 2 +'b' > 'a' +1 == 'one' +2 > 'one' +``` + +#### **Output** +``` +False - One is not greater than two +True - Since 'b' is greater than 'a' in ASCII +False - 1 is a integer and 'one' is a string, since == checks for equality it does not throw an error +TypeError - Since 2 is a integer and 'one' is a string, since > checks for greater than it does not throw an error +``` + + ++ What is the result of `bool("")` and `bool(" ")` ? Explain + + + - `bool("")` is False + - `bool(" ")` is True + ++ Give Output: `[] is not []` + + + - `[] is not []` is True
+ `[]` is not `[]` because `[]` is not the same object as `[]` + ++ What is the result of running `True-True` ? + + + - `True-True` is `0` + ++ String is an immutable data type in Python - True or False? + + + True + ++ How to check if a string starts with a letter? + + + ``` + import re + if re.match("^[a-zA-Z]+.*", string): + ``` + + string built-in: + + ``` + if string and string[0].isalpha(): + ``` + + + ++ How to check if all characters in a given string are digits? + + + `string.isdigit` + + + ++ How to remove trailing slash ('/') from a string? + + + `string.rstrip('/')` + + + +#### **Program** +What is the result of of each of the following? +- "abc"*3 +- "abc"*2.5 +- "abc"*2.0 +- "abc"*True +- "abc"*False + +#### **Output** + +- abcabcabc +- TypeError +- TypeError +- "abc" +- "" + + + +#### **Program** +Improve the following code: + +``` +char = input("Insert a character: ") +if char == "a" or char == "o" or char == "e" or char =="u" or char == "i": + print("It's a vowel!") +``` +#### **Output** + +``` +char = input("Insert a character: ") # For readablity +if lower(char[0]) in "aieou": # Takes care of multiple characters and separate cases + print("It's a vowel!") +``` +*OR* +``` +if lower(input("Insert a character: ")[0]) in "aieou": # Takes care of multiple characters and small/Capital cases + print("It's a vowel!") +``` + + + ++ How to define a function with Python? + + + Using the `def` keyword. For Examples: + + ``` + def sum(a, b): + return (a + b) + ``` + + ++ In Python, functions are first-class objects. What does it mean? + + + In general, first class objects in programming languages are objects which can be assigned to variable, used as a return value and can be used as arguments or parameters.
+ In python you can treat functions this way. Let's say we have the following function + + ``` + def my_function(): + return 5 + ``` + + You can then assign a function to a variables like this `x = my_function` or you can return functions as return values like this `return my_function` + + + ++ Write a function to determine if a number is a Palindrome + + + - Code: + + ``` + from typing import Union + + def isNumberPalindrome(number: Union[int, str]) -> bool: + if isinstance(number, int): + number = str(number) + return number == number[::-1] + + print(isNumberPalindrome("12321")) + ``` + + - Using Python3.10 that accepts using bitwise operator '|'. + + ``` + def isNumberPalindrome(number: int | str) -> bool: + if isinstance(number, int): + number = str(number) + return number == number[::-1] + + print(isNumberPalindrome("12321")) + ``` + + Note: Using slicing to reverse a list could be slower than other options like `reversed` that return an iterator. + + - Result: + + ``` + True + ``` + + +#### **Program** + +What is the result of the following block of code? + +``` +x = ['a', 'b', 'c'] +for i in x: + if i == 'b': + x = ['z', 'y'] + print(i) +``` +#### **Output** + +``` +a +b +c +``` + + + ++ Explain inheritance and how to use it in Python + + + ``` + By definition inheritance is the mechanism where an object acts as a base of another object, retaining all its + properties. + + So if Class B inherits from Class A, every characteristics from class A will be also available in class B. + Class A would be the 'Base class' and B class would be the 'derived class'. + + This comes handy when you have several classes that share the same functionalities. + + The basic syntax is: + + class Base: pass + + class Derived(Base): pass + + A more forged example: + + class Animal: + def __init__(self): + print("and I'm alive!") + + def eat(self, food): + print("ñom ñom ñom", food) + + class Human(Animal): + def __init__(self, name): + print('My name is ', name) + super().__init__() + + def write_poem(self): + print('Foo bar bar foo foo bar!') + + class Dog(Animal): + def __init__(self, name): + print('My name is', name) + super().__init__() + + def bark(self): + print('woof woof') + + michael = Human('Michael') + michael.eat('Spam') + michael.write_poem() + + bruno = Dog('Bruno') + bruno.eat('bone') + bruno.bark() + + >>> My name is Michael + >>> and I'm alive! + >>> ñom ñom ñom Spam + >>> Foo bar bar foo foo bar! + >>> My name is Bruno + >>> and I'm alive! + >>> ñom ñom ñom bone + >>> woof woof + + Calling super() calls the Base method, thus, calling super().__init__() we called the Animal __init__. + + There is a more advanced python feature called MetaClasses that aid the programmer to directly control class creation. + ``` + + ++ Explain and demonstrate class attributes & instance attributes + + + In the following block of code `x` is a class attribute while `self.y` is a instance attribute + + ``` + class MyClass(object): + x = 1 + + def __init__(self, y): + self.y = y + ``` + ++ What is an error? What is an exception? What types of exceptions are you familiar with? + + + ``` + # Note that you generally don't need to know the compiling process but knowing where everything comes from + # and giving complete answers shows that you truly know what you are talking about. + + Generally, every compiling process have a two steps. + - Analysis + - Code Generation. + + Analysis can be broken into: + 1. Lexical analysis (Tokenizes source code) + 2. Syntactic analysis (Check whether the tokens are legal or not, tldr, if syntax is correct) + + for i in 'foo' + ^ + SyntaxError: invalid syntax + + We missed ':' + + 1. Semantic analysis (Contextual analysis, legal syntax can still trigger errors, did you try to divide by 0, + hash a mutable object or use an undeclared function?) + + 1/0 + ZeroDivisionError: division by zero + + These three analysis steps are the responsible for error handlings. + + The second step would be responsible for errors, mostly syntax errors, the most common error. + The third step would be responsible for Exceptions. + + As we have seen, Exceptions are semantic errors, there are many builtin Exceptions: + + ImportError + ValueError + KeyError + FileNotFoundError + IndentationError + IndexError + ... + + You can also have user defined Exceptions that have to inherit from the `Exception` class, directly or indirectly. + + Basic example: + + class DividedBy2Error(Exception): + def __init__(self, message): + self.message = message + + def division(dividend,divisor): + if divisor == 2: + raise DividedBy2Error('I dont want you to divide by 2!') + return dividend / divisor + + division(100, 2) + + >>> __main__.DividedBy2Error: I dont want you to divide by 2! + ``` + + + ++ Explain Exception Handling and how to use it in Python + + + **Exceptions:** Errors detected during execution are called Exceptions. + + **Handling Exception:** When an error occurs, or exception as we call it, Python will normally stop and generate an error message.
+ Exceptions can be handled using `try` and `except` statement in python. + + **Example:** Following example asks the user for input until a valid integer has been entered.
+ If user enter a non-integer value it will raise exception and using except it will catch that exception and ask the user to enter valid integer again. + ```py + while True: + try: + a = int(input("please enter an integer value: ")) + break + except ValueError: + print("Ops! Please enter a valid integer value.") + + ``` + For more details about errors and exceptions follow this [https://docs.python.org/3/tutorial/errors.html](https://docs.python.org/3/tutorial/errors.html) + + +#### **Program** +What is the result of running the following function? + +``` +def true_or_false(): + try: + return True + finally: + return False +``` +#### **Output** +False + + + + + +#### **Question** +- Explain the following built-in functions (their purpose + use case example): + * repr + * any + * all + +#### **Answer** + +- repr: + ``` + repr(object) -> string + Return a string containing a printable representation of an object. + ``` +- any: + ``` + any(iterable) -> bool + Return True if bool(x) is True for any x in the iterable. + ``` +- all: + ``` + all(iterable) -> bool + Return True if bool(x) is True for all values x in the iterable. + ``` + + + ++ What is the difference between repr function and str? + + + | repr | str | + | ---- | --- | + | Make Object Readable | Required code that reprdouces object | + | Generate Output to end user | Generate output for developer | + + **Example**: + ``` + >> import datetime + >> now = datetime.datetime.now() + >> str(now) + '2022-01-26 11:06:33.927791' + >> repr(now) + 'datetime.datetime(2022, 1, 26, 11, 6, 33, 927791)' + ``` + ++ What is the \_\_call\_\_ method? + + + It is used to emulate callable objects. It allows a class instance to be called as a function. + + - Example code: + + ``` + class Foo: + def __init__(self: object) -> None: + pass + def __call__(self: object) -> None: + print("Called!") + + f = Foo() + f() + ``` + + - Result: + + ``` + Called! + ``` + + + + ++ Do classes has the \_\_call\_\_ method as well? What for? + + + \_\_class\_\_ method is used to write classes where the instances behave like functions and can be called. + + - Example code: + **Program:** + ```py + class Foo: + def __init__(self: object) -> None: + pass + def __call__(self: object) -> None: + print("Called!") + f = Foo() + f() + ``` + **Output:** + ``` + Called! + ``` + ++ What _ is used for in Python? + + + 1. Translation lookup in i18n + 2. Hold the result of the last executed expression or statement in the interactive interpreter. + 3. As a general purpose "throwaway" variable name. For example: x, y, _ = get_data() (x and y are used but since we don't care about third variable, we "threw it away"). + ++ What is GIL? + + + Python Global Interpreter Lock (GIL) is a type of process lock which is used by python whenever it deals with processes. Generally, Python only uses only one thread to execute the set of written statements. This means that in python only one thread will be executed at a time + ++ What is Lambda? How is it used? + + + A ```lambda``` expression is an 'anonymous' function, the difference from a normal defined function using the keyword `def`` is the syntax and usage. + + The syntax is: + + ```lambda[parameters]: [expresion]``` + + **Examples:** + + * A lambda function add 10 with any argument passed. + + ```py + x = lambda a: a + 10 + print(x(10)) + ``` + + * An addition function + + ```py + addition = lambda x, y: x + y + print(addition(10, 20)) + ``` + + * Squaring function + + ```py + square = lambda x : x ** 2 + print(square(5)) + ``` + Generally it is considered a bad practice under PEP 8 to assign a lambda expresion, they are meant to be used as parameters and inside of other defined functions. + + ++ Are there private variables in Python? How would you make an attribute of a class, private? + + + Yes, there are private variables in Python. + + - Example code: + + ```py + class Foo: + def __init__(self: object) -> None: + self.__private_variable = "private" + self.public_variable = "public" + f = Foo() + print(f.__private_variable) + print(f.public_variable) + ``` + + - Result: + + ``` + private + public + ``` + ++ Explain the following: getter, setter, deleter + + - getter: Used to get the value of an attribute. + ``` + def get_name(self): + return self.__name + ``` + - setter: Used to set the value of an attribute. + ``` + def set_name(self, name): + self.__name = name + ``` + - deleter: Used to delete an attribute. + ``` + def del_name(self): + del self.__name + ``` + + In the above example, the getter and setter are used to get and set the value of the private variable. The deleter is used to delete the private variable. + + ++ Explain what is @property + + + @property is a built-in decorator for the propery() function which is used to give special functionality to the certain methods to make them as getter, setter and deleter. + + - Example code: + ``` + class Foo: + def __init__(self: object) -> None: + self.__name = "Foo" + @property + def name(self): + return self.__name + @name.setter + def name(self, name): + self.__name = name + @name.deleter + def name(self): + del self.__name + f = Foo() + print(f.name) + f.name = "Bar" + print(f.name) + del f.name + ``` + + ++ How do you swap values between two variables? + + + ``` + x, y = y, x + ``` + + + +#### **Question** +Explain the following object's magic variables: + - \_\_dict\_\_ + - \_\_doc\_\_ + - \_\_module\_\_ + - \_\_name\_\_ + - \_\_class\_\_ + - \_\_bases\_\_ + - \_\_mro\_\_ + +#### **Answer** + + 1. \_\_dict\_\_ is a dictionary or other mapping object used to store an object's (writable) attributes. + + 2. \_\_doc\_\_ is a string containing the object's docstring. + 3. \_\_module\_\_ is for retrieving the module where the function was defined. + 4. \_\_name\_\_ is the name of the current object. + 5. \_\_class\_\_ is the class of the current object. + 6. \_\_bases\_\_ is a tuple containing the base classes of the current object. + 7. \_\_mro\_\_ is a tuple containing the method resolution order of the current object. + + + + ++ Write a function to return the sum of one or more numbers. The user will decide how many numbers to use + + + First you ask the user for the amount of numbers that will be use. Use a while loop that runs until amount_of_numbers becomes 0 through subtracting amount_of_numbers by one each loop. In the while loop you want ask the user for a number which will be added a variable each time the loop runs. + + ``` + def return_sum(): + amount_of_numbers = int(input("How many numbers? ")) + total_sum = 0 + while amount_of_numbers != 0: + num = int(input("Input a number. ")) + total_sum += num + amount_of_numbers -= 1 + return total_sum + + ``` + + ++ Print the average of [2, 5, 6]. It should be rounded to 3 decimal places + + + ``` + li = [2, 5, 6] + print("{0:.3f}".format(sum(li)/len(li))) + ``` + ++ What is a tuple in Python? What is it used for? + + + A tuple is a built-in data type in Python. It's used for storing multiple items in a single variable. + + ++ List, like a tuple, is also used for storing multiple items. What is then, the difference between a tuple and a list? + + + List, as opposed to a tuple, is a mutable data type. It means we can modify it and at items to it. + + + ++ How to add the number 2 to the list ```x = [1, 2, 3]``` + + + `x.append(2)` + + + ++ How to check how many items a list contains? + + + `len(sone_list)` + + + ++ How to get the last element of a list? + + + `some_list[-1]` + + + ++ How to add the items of [1, 2, 3] to the list [4, 5, 6]? + + + x = [4, 5, 6] + x.extend([1, 2, 3]) + + Don't use `append` unless you would like the list as one item. + + + ++ How to remove the first 3 items from a list? + + + `my_list[0:3] = []` + + + ++ How do you get the maximum and minimum values from a list? + + + ``` + Maximum: max(some_list) + Minimum: min(some_list) + ``` + + + ++ How to get the top/biggest 3 items from a list? + + + ``` + sorted(some_list, reverse=True)[:3] + ``` + + Or + + ``` + some_list.sort(reverse=True) + some_list[:3] + ``` + ++ How to insert an item to the beginning of a list? What about two items? + + - One item: + + ``` + numbers = [1, 2, 3, 4, 5] + numbers.insert(0, 0) + print(numbers) + ``` + + - Multiple items or list: + + ``` + numbers_1 = [2, 3, 4, 5] + numbers_2 = [0, 1] + numbers_1 = numbers_2 + numbers_1 + print(numbers_1) + ``` + ++ How to sort list by the length of items? + + + ``` + sorted_li = sorted(li, key=len) + ``` + + Or without creating a new list: + + ``` + li.sort(key=len) + ``` + + + ++ Do you know what is the difference between list.sort() and sorted(list)? + + + * sorted(list) will return a new list (original list doesn't change) + * list.sort() will return None but the list is change in-place + + * sorted() works on any iterable (Dictionaries, Strings, ...) + * list.sort() is faster than sorted(list) in case of Lists + + ++ Convert every string to an integer: ```[['1', '2', '3'], ['4', '5', '6']``` + + + ``` + nested_li = [['1', '2', '3'], ['4', '5', '6']] + [[int(x) for x in li] for li in nested_li] + ``` + + + ++ How to merge two sorted lists into one sorted list? + + + ``` + sorted(li1 + li2) + ``` + + Another way: + + ``` + i, j = 0 + merged_li = [] + + while i < len(li1) and j < len(li2): + if li1[i] < li2[j]: + merged_li.append(li1[i]) + i += 1 + else: + merged_li.append(li2[j]) + j += 1 + + merged_li = merged_li + merged_li[i:] + merged_li[j:] + ``` + + + ++ How to check if all the elements in a given lists are unique? so [1, 2, 3] is unique but [1, 1, 2, 3] is not unique because 1 exists twice + + + There are many ways of solving this problem:
+ ```# Note: :list and -> bool are just python typings, they are not needed for the correct execution of the algorithm. ``` + + Taking advantage of sets and len: + + ``` + def is_unique(l:list) -> bool: + return len(set(l)) == len(l) + ``` + + This one is can be seen used in other programming languages. + + ``` + def is_unique2(l:list) -> bool: + seen = [] + + for i in l: + if i in seen: + return False + seen.append(i) + return True + ``` + + Here we just count and make sure every element is just repeated once. + + ``` + def is_unique3(l:list) -> bool: + for i in l: + if l.count(i) > 1: + return False + return True + ``` + + This one might look more convulated but hey, one liners. + + ``` + def is_unique4(l:list) -> bool: + return all(map(lambda x: l.count(x) < 2, l)) + ``` + + + You have the following function + + ``` + def my_func(li = []): + li.append("hmm") + print(li) + ``` + + If we call it 3 times, what would be the result each call? + + + + ``` + ['hmm'] + ['hmm', 'hmm'] + ['hmm', 'hmm', 'hmm'] + ``` + + ++ How to iterate over a list? + + + ``` + for item in some_list: + print(item) + ``` + + + ++ How to iterate over a list with indexes? + + + ``` + for i, item in enumerate(some_list): + print(i) + ``` + ++ How to start list iteration from 2nd index? + + + Using range like this + + ``` + for i in range(1, len(some_list)): + some_list[i] + ``` + + Another way is using slicing + + ``` + for i in some_list[1:]: + ``` + + + ++ How to iterate over a list in reverse order? + + + Method 1 + ``` + for i in reversed(li): + ... + ``` + + Method 2 + ``` + n = len(li) - 1 + while n > 0: + ... + n -= 1 + ``` + + ++ Sort a list of lists by the second item of each nested list + + + ``` + li = [[1, 4], [2, 1], [3, 9], [4, 2], [4, 5]] + + sorted(li, key=lambda l: l[1]) + ``` + + or + + ``` + li.sort(key=lambda l: l[1) + ``` + + + ++ Combine [1, 2, 3] and ['x', 'y', 'z'] so the result is [(1, 'x'), (2, 'y'), (3, 'z')] + + + ``` + nums = [1, 2, 3] + letters = ['x', 'y', 'z'] + + list(zip(nums, letters)) + ``` + + + ++ What is List Comprehension? Is it better than a typical loop? Why? Can you demonstrate how to use it? + + + From [Docs](https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions): "List comprehensions provide a concise way to create lists. Common applications are to make new lists where each element is the result of some operations applied to each member of another sequence or iterable, or to create a subsequence of those elements that satisfy a certain condition.". + + It's better because they're compact, faster and have better readability. + + - For loop: + + ``` + number_lists = [[1, 7, 3, 1], [13, 93, 23, 12], [123, 423, 456, 653, 124]] + odd_numbers = [] + for number_list in number_lists: + for number in number_list: + if number % 2 == 0: + odd_numbers.append(number) + print(odd_numbers) + ``` + + - List comprehesion: + + ``` + number_lists = [[1, 7, 3, 1], [13, 93, 23, 12], [123, 423, 456, 653, 124]] + odd_numbers = [number for number_list in number_lists for number in number_list if number % 2 == 0] + print(odd_numbers) + ``` + + + + You have the following list: ```[{'name': 'Mario', 'food': ['mushrooms', 'goombas']}, {'name': 'Luigi', 'food': ['mushrooms', 'turtles']}]```.Extract all type of foods. Final output should be: {'mushrooms', 'goombas', 'turtles'} + + + ``` + brothers_menu = \ + [{'name': 'Mario', 'food': ['mushrooms', 'goombas']}, {'name': 'Luigi', 'food': ['mushrooms', 'turtles']}] + + # "Classic" Way + def get_food(brothers_menu) -> set: + temp = [] + + for brother in brothers_menu: + for food in brother['food']: + temp.append(food) + + return set(temp) + + # One liner way (Using list comprehension) + set([food for bro in x for food in bro['food']]) + ``` + ++ How to create a dictionary? + + + my_dict = dict(x=1, y=2) + OR + my_dict = {'x': 1, 'y': 2} + OR + my_dict = dict([('x', 1), ('y', 2)]) + ++ How to remove a key from a dictionary? + + + del my_dict['some_key'] + you can also use `my_dict.pop('some_key')` which returns the value of the key. + ++ How to sort a dictionary by values? + + + ``` + {k: v for k, v in sorted(x.items(), key=lambda item: item[1])} + ``` + + + ++ How to sort a dictionary by keys? + + + ``` + dict(sorted(some_dictionary.items())) + ``` + ++ How to merge two dictionaries? + + + ``` + some_dict1.update(some_dict2) + ``` + + ++ Convert the string "a.b.c" to the dictionary ```{'a': {'b': {'c': 1}}}``` + + + ``` + output = {} + string = "a.b.c" + path = string.split('.') + target = reduce(lambda d, k: d.setdefault(k, {}), path[:-1], output) + target[path[-1]] = 1 + print(output) + ``` + + ++ Can you implement "binary search" in Python? + + + ```py + def binary_search(lst,item) -> int: + low = 0 + high = len(lst) - 1 + mid = (low + high) // 2 + + while low <= high: + if lst[mid] == item: + return mid + elif lst[mid] < item: + low = mid + 1 + else: + high = mid - 1 + mid = (low + high) // 2 + return -1 + print(binary_search([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 5)) + ``` + ++ How to write to a file? + + + ``` + with open('file.txt', 'w') as file: + file.write("My insightful comment") + ``` + + + ++ How to print the 12th line of a file? + + + ``` + with open('file.txt', 'r') as file: + for i, line in enumerate(file): + if i == 11: + print(line) + ``` + ++ How to reverse a file? + + + ``` + with open('file.txt', 'r') as file: + lines = file.readlines() + lines.reverse() + with open('file.txt', 'w') as file: + file.writelines(lines) + ``` + ++ Sum all the integers in a given file + + + ``` + with open('file.txt', 'r') as file: + lines = file.readlines() + sum = 0 + for line in lines: + sum += int(line) + ``` + ++ Print a random line of a given file + + + ``` + with open('file.txt', 'r') as file: + lines = file.readlines() + print(random.choice(lines)) + ``` + ++ Print every 3rd line of a given file + + + ``` + with open('file.txt', 'r') as file: + lines = file.readlines() + for i, line in enumerate(lines): + if i % 3 == 0: + print(line) + ``` + ++ Print the number of lines in a given file + + + ``` + with open('file.txt', 'r') as file: + lines = file.readlines() + print(len(lines)) + ``` + ++ Print the number of of words in a given file + + + ``` + with open('file.txt', 'r') as file: + lines = file.readlines() + words = [line.split() for line in lines] + print(len(words)) + ``` + + ++ Can you write a function which will print all the files in a given directory? including sub-directories + + + ``` + def print_files(path): + for root, dirs, files in os.walk(path): + for file in files: + print(os.path.join(root, file)) + ``` + ++ Write a dictionary (variable) to a file + + + ``` + import json + + with open('file.json', 'w') as f: + f.write(json.dumps(dict_var)) + ``` + ++ How to print current working directory? + + + ``` + import os + print(os.getcwd()) + ``` + + ++ Given the path ```/dir1/dir2/file1``` print the file name (file1) + + + ``` + import os + print(os.path.basename('/dir1/dir2/file1')) + # Another way + print(os.path.split('/dir1/dir2/file1')[1]) + ``` + + + +### **Question** + +Given the path ```/dir1/dir2/file1``` + + 1. Print the path without the file name (/dir1/dir2) + 2. Print the name of the directory where the file resides (dir2) + +### **Answer** + + ``` + import os + + ## Part 1. + # os.path.dirname gives path removing the end component + dirpath = os.path.dirname('/dir1/dir2/file1') + print(dirpath) + + ## Part 2. + print(os.path.basename(dirpath)) + ``` + + + + ++ How do you execute shell commands using Python? + + + ``` + import subprocess + subprocess.call(['ls', '-l']) + ``` + + ++ How do you join path components? for example ```/home``` and ```luig``` will result in ```/home/luigi``` + + + ``` + import os + print(os.path.join('/home', 'luigi')) + ``` + ++ How do you remove non-empty directory? + + + ``` + import os + os.rmdir('/home/luigi') + ``` + ++ How do you perform regular expressions related operations in Python? (match patterns, substitute strings, etc.) + + + - import re # Using re module + ++ How to substitute the string "green" with "blue"? + + + ``` + import re + string = "green is the color of the sky" + pattern = re.compile("green") + print(pattern.sub("blue", string)) + ``` + ++ How to find all the IP addresses in a variable? How to find them in a file? + + + ``` + import re + string = "My IP address is 192.168.24.89 and my other IP address is 192.168.42.98" + pattern = re.compile("\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}") + print(pattern.findall(string)) + ``` + ++ Find the first repeated character in a string + + + While you iterate through the characters, store them in a dictionary and check for every character whether it's already in the dictionary. + + ``` + def firstRepeatedCharacter(str): + chars = {} + for ch in str: + if ch in chars: + return ch + else: + chars[ch] = 0 + ``` + ++ How to extract the unique characters from a string? for example given the input "itssssssameeeemarioooooo" the output will be "mrtisaoe" + + + ``` + x = "itssssssameeeemarioooooo" + y = ''.join(set(x)) + ``` + ++ Find all the permutations of a given string + + + ``` + def permute_string(string): + + if len(string) == 1: + return [string] + + permutations = [] + for i in range(len(string)): + swaps = permute_string(string[:i] + string[(i+1):]) + for swap in swaps: + permutations.append(string[i] + swap) + + return permutations + + print(permute_string("abc")) + ``` + + Short way (but probably not acceptable in interviews): + + ``` + from itertools import permutations + + [''.join(p) for p in permutations("abc")] + ``` + + Detailed answer can be found here: http://codingshell.com/python-all-string-permutations + ++ How to check if a string contains a sub string? + + + ``` + def is_substring(string, sub_string): + return sub_string in string + ``` + ++ Find the frequency of each character in string + + + ``` + def frequency(string): + freq = {} + for ch in string: + if ch in freq: + freq[ch] += 1 + else: + freq[ch] = 1 + return freq + ``` + ++ Count the number of spaces in a string + + + ``` + def count_spaces(string): + return string.count(' ') + ``` + + ++ Given a string, find the N most repeated words + + + ``` + def most_repeated_words(string, n): + words = string.split() + freq = {} + for word in words: + if word in freq: + freq[word] += 1 + else: + freq[word] = 1 + return sorted(freq, key=freq.get, reverse=True)[:n] + ``` + ++ Given the string (which represents a matrix) "1 2 3\n4 5 6\n7 8 9" create rows and colums variables (should contain integers, not strings) + + + ``` + string = "1 2 3\n4 5 6\n7 8 9" + rows = string.split('\n') + cols = [row.split() for row in rows] + print(rows) + print(cols) + ``` + + + +#### **Question** + +What is the result of each of the following? + +``` +>> ', '.join(["One", "Two", "Three"]) +>> " ".join("welladsadgadoneadsadga".split("adsadga")[:2]) +>> "".join(["c", "t", "o", "a", "o", "q", "l"])[0::2] +``` + +#### **Answer** + +``` +>>> 'One, Two, Three' +>>> 'well done' +>>> 'cool' +``` + + + + ++ If ```x = "pizza"```, what would be the result of ```x[::-1]```? + + + It will reverse the string, so x would be equal to `azzip`. + + + ++ Reverse each word in a string (while keeping the order) + + + ``` + def reverse_words(string): + words = string.split() + return " ".join(reversed(words)) + ``` + + ++ What is the output of the following code: ```"".join(["a", "h", "m", "a", "h", "a", "n", "q", "r", "l", "o", "i", "f", "o", "o"])[2::3]``` + + + 'mario' + ++ What is an iterator? + + + Iterator is an object that can be iterated upon, i.e. you can call ```next()``` on it. + + **Example:** + ``` + >>> my_list = [1, 2, 3] + >>> my_iterator = iter(my_list) + >>> next(my_iterator) + 1 + >>> next(my_iterator) + 2 + ``` + ++ Explain data serialization and how do you perform it with Python + + + Pickle is a Python library that allows you to serialize and deserialize Python objects to and from a binary stream. + + ++ How do you handle argument parsing in Python? + + + In Python, you can use the ```argparse``` module to parse command line arguments. + + ++ What is a generator? Why using generators? + + + A generator is an object that produces one value at a time. + + **Example:** + ``` + def my_generator(): + yield 1 + yield 2 + yield 3 + ``` + + + +#### **Question:** + +What would be the output of the following block? + +``` +for i in range(3, 3): + print(i) +``` + +#### **Answer:** + +No output :) + + + + + ++ What is ```yield```? When would you use it? + + + yield is a keyword used to return from a function and save the state of the function. + + Usage: + ``` + def my_generator(): + yield 1 + yield 2 + yield 3 + ``` + when we want to iterate over the generator, we call ```next()``` on it and it will return the next value. + + ++ Explain the following types of methods and how to use them: Static method, Class method,Instance method + + + - Static Method is a method is defined inside a class and is called without creating an instance of the class. + + - Class method is a method is defined inside a class and is called without creating an instance of the class. + + - Instance method is a method is defined inside a class and is called by creating an instance of the class. + + **Example:** + ``` + class MyClass: + @staticmethod + def my_static_method(self): + print("I am a static method") + @classmethod + def my_class_method(cls): + print("I am a class method") + def my_instance_method(self): + print("I am an instance method") + ``` + ++ Difference between Static and Class Method + + + | Static | Class| + |--------|------| + | Does not take any specific parameter | Takes the class as a parameter | + | Can be called without creating an instance of the class | Can be called without creating an instance of the class | + | @staticmethod | @classmethod | + ++ How to reverse a list? + + + ``` + def reverse_list(list): + return list[::-1] + ``` + ++ How to combine list of strings into one string with spaces between the strings + + + ``` + def combine_list_of_strings(list): + return " ".join(list) + ``` + ++ You have the following list of nested lists: ```[['Mario', 90], ['Geralt', 82], ['Gordon', 88]]``` How to sort the list by the numbers in the nested lists?``` + + + One way is: + the_list.sort(key=lambda x: x[1]) + ++ Explain the following: zip(), map(), filter() + + + | zip() | map() | filter() | + |------|-------|----------| + | Creates an iterator of tuples | Creates an iterator of the results of calling a function on each item of a given iterable | Creates an iterator of the results of applying a function that you specify to each item of a given iterable | + ++ How do you debug Python code? + + + The module pdb defines a debugger that allows you to execute Python code step by step. + ++ How to check how much time it took to execute a certain script or block of code? + + + ``` + import time + start_time = time.time() + # do something + end_time = time.time() + print(end_time - start_time) + ``` + ++ What empty ```return``` returns? + + + Short answer is: It returns a None object. + + We could go a bit deeper and explain the difference between + + ``` + def a (): + return + + >>> None + ``` + + And + + ``` + def a (): + pass + + >>> None + ``` + Or we could be asked this as a following question, since they both give the same result. + + We could use the dis module to see what's going on: + + ``` + 2 0 LOAD_CONST 0 (", line 2>) + 2 LOAD_CONST 1 ('a') + 4 MAKE_FUNCTION 0 + 6 STORE_NAME 0 (a) + + 5 8 LOAD_CONST 2 (", line 5>) + 10 LOAD_CONST 3 ('b') + 12 MAKE_FUNCTION 0 + 14 STORE_NAME 1 (b) + 16 LOAD_CONST 4 (None) + 18 RETURN_VALUE + + Disassembly of ", line 2>: + 3 0 LOAD_CONST 0 (None) + 2 RETURN_VALUE + + Disassembly of ", line 5>: + 6 0 LOAD_CONST 0 (None) + 2 RETURN_VALUE + ``` + + An empty ``` return``` is exactly the same as ```return None``` and functions without any explicit return + will always return None regardless of the operations, therefore + + ``` + def sum(a, b): + global c + c = a + b + + >>> None + ``` + +[Continue Assessment ➡️](programming/assessment-2) \ No newline at end of file diff --git a/programming/assessment-2.md b/programming/assessment-2.md new file mode 100644 index 0000000..4bd9b9e --- /dev/null +++ b/programming/assessment-2.md @@ -0,0 +1,786 @@ + + + + +#### **Question:** + +How to improve the following block of code? + +``` +li = [] +for i in range(1, 10): + li.append(i) +``` +#### **Answer:** + +``` +[i for i in range(1, 10)] +``` + + + + +#### **Question:** + +Given the following function + +``` +def is_int(num): + if isinstance(num, int): + print('Yes') + else: + print('No') +``` + +What would be the result of is_int(2) and is_int(False)? + +#### **Answer:** + +``` +Yes +No +``` + + + ++ Can you implement a linked list in Python? + + + - The reason we need to implement in the first place, it's because a linked list isn't part of Python standard library. + - To implement a linked list, we have to implement two structures: The linked list itself and a node which is used by the linked list. + + Let's start with a node. A node has some value (the data it holds) and a pointer to the next node + + ``` + class Node(object): + def __init__(self, data): + self.data = data + self.next = None + ``` + + Now the linked list. An empty linked list has nothing but an empty head. + + ``` + class LinkedList(object): + def __init__(self): + self.head = None + ``` + + Now we can start using the linked list + + ``` + ll = Linkedlist() + ll.head = Node(1) + ll.head.next = Node(2) + ll.head.next.next = Node(3) + ``` + + What we have is: + + ---- ----- ---- + | 1 | -> | 2 | -> | 3 | + ---- ----- ----- + + Usually, more methods are implemented, like a push_head() method where you insert a node at the beginning of the linked list + + ``` + def push_head(self, value): + new_node = Node(value) + new_node.next = self.head + self.head = new_node + ``` + + + ++ Add a method to the Linked List class to traverse (print every node's data) the linked list + + + def print_list(self): + node = self.head + while(node): + print(node.data) + node = node.next + + + ++ Write a method to that will return a boolean based on whether there is a loop in a linked list or not + + + Let's use the Floyd's Cycle-Finding algorithm: + + ``` + def loop_exists(self): + one_step_p = self.head + two_steps_p = self.head + while(one_step_p and two_steps_p and two_steps_p.next): + one_step_p = self.head.next + two_step_p = self.head.next.next + if (one_step_p == two_steps_p): + return True + return False + ``` + ++ Implement Stack in Python + + + ``` + stack = [] + + # Push + stack.append(1) + stack.append(2) + stack.append(3) + + # Pop + stack.pop() + stack.pop() + + # Peek + stack[-1] + ``` + ++ Write a Test to check whether the function returns string in Python? + + + ``` + def test_is_string(): + assert is_string('hello') == True + ``` + ++ What is PEP8? Give an example of 3 style guidelines + + + PEP8 is a list of coding conventions and style guidelines for Python + + 5 style guidelines: + + 1. Limit all lines to a maximum of 79 characters. + 2. Surround top-level function and class definitions with two blank lines. + 3. Use commas when making a tuple of one element + 4. Use spaces (and not tabs) for indentation + 5. Use 4 spaces per indentation level + + ++ How to test if an exception was raised? + + + ``` + try: + raise Exception('This is an exception') + except Exception as e: + print(e) + ``` + ++ What ```assert``` does in Python? + + + ```assert``` is a built-in function in Python that checks if a condition is true. If it's not, it raises an exception. + + Syntax: + ``` + assert expression, message + ``` + ++ Explain mocks + + + ``` + import unittest + from unittest.mock import Mock + + class TestClass(unittest.TestCase): + def test_method(self): + mock_method = Mock() + mock_method.return_value = 'hello' + assert mock_method() == 'hello' + ``` + Here the mock_method is a mock object. It's a mock object because it doesn't have a real implementation. + ++ How do you measure execution time of small code snippets? + + + ``` + import time + start = time.time() + # do something + end = time.time() + print(end - start) + ``` + + ++ Why one shouldn't use ```assert``` in non-test/production code? + + + ```assert``` is a built-in function in Python that checks if a condition is true. If it's not, it raises an exception. + In non-test/production code, we should not use ```assert``` because it's a bad practice where we can't debug the code. + ++ Can you describe what is Django/Flask and how you have used it? Why Flask and not Django? (or vice versa) + + + + - Django and Flask are both web frameworks which are used to build web applications. + - Prefer Flask because it's more lightweight and easier to maintain. + - Prefer Django because it's more powerful and easier to maintain. + ++ What is a route? + + + As every web framework, Flask provides a route functionality that lets you serve a content of a given URL. + + There are multiple ways to map a URL with a function in Python. + + - Decorator: you can use python decorators. In this case we're using `app`. This `app` decorator is the instance of the `Flask` class. And route it's a method of this class. + + ``` + @app.route('/') + def home(): + return 'main website' + ``` + + - `add_url_rule` method: This is a method of the Flask class. We can also use it for map the URL with a function. + + ``` + def home(): + return 'main website' + + app.add_url_rule('/', view_func=home) + ``` + ++ What is a blueprint in Flask? + + + A blueprint is a way to organize your application. It's a way to group your views and templates. + + ``` + from flask import Blueprint + + main = Blueprint('main', __name__) + ``` + + - `main` is the name of the blueprint. + - `__name__` is the name of the module. + + ++ What is a template? + + + A template is a file that contains HTML code. + + ``` + + + My title + + +

My title

+ + + ``` + + - `` is the root tag. + - `` is the head tag. + - `` is the title tag. + - `<body>` is the body tag. + - `<h1>` is the h1 tag. + + ++ Given ```x = [1, 2, 3]```, what is the result of list(zip(x))? + + + ``` + [(1,), (2,), (3,)] + ``` + +<!-- tabs:start --> +#### **Question** + +What is the result of each of the following: + +1. list(zip(range(5), range(50), range(50))) +2. list(zip(range(5), range(50), range(-2))) + +#### **Answers** + +1. [(0, 0, 0), (1, 1, 1), (2, 2, 2), (3, 3, 3), (4, 4, 4)] +2. [] + +<!-- tabs:end --> + + ++ Explain Descriptors + + + Descriptors let objects customize attribute lookup, storage, and deletion. + + Example: + ``` + class MyClass: + def __init__(self): + self.x = 'x' + self.y = 'y' + self.z = 'z' + + def __getattr__(self, name): + if name == 'a': + return 'a' + else: + raise AttributeError(name) + + def __setattr__(self, name, value): + if name == 'a': + raise AttributeError(name) + else: + super().__setattr__(name, value) + + def __delattr__(self, name): + if name == 'a': + raise AttributeError(name) + else: + super().__delattr__(name) + ``` + + - `__getattr__` is a method that is called when an attribute is not found. + - `__setattr__` is a method that is called when an attribute is set. + - `__delattr__` is a method that is called when an attribute is deleted. + + To use the desciptor given above, we can use the following code: + ``` + >>>my_class = MyClass() + >>>my_class.x + 'x' + >>>my_class.y + 'y' + ``` + + +<!-- tabs:start --> + +#### **Question** + +What would be the result of running ```a.num2``` assuming the following code +``` +class B: + def __get__(self, obj, objtype=None): + reuturn 10 + +class A: + num1 = 2 + num2 = Five() +``` +#### **Answers** +```10``` +<!-- tabs:end --> + + +<!-- tabs:start --> + +#### **Question** + +What would be the result of running ```some_car = Car("Red", 4)``` assuming the following code +``` +class Print: + + def __get__(self, obj, objtype=None): + value = obj._color + print("Color was set to {}".format(valie)) + return value + + def __set__(self, obj, value): + print("The color of the car is {}".format(value)) + obj._color = value + +class Car: + + color = Print() + + def __ini__(self, color, age): + self.color = color + self.age = age +``` + +#### **Answer** +An instance of Car class will be created and the following will be printed: "The color of the car is Red" + +<!-- tabs:end --> + + ++ How can you spawn multiple processes with Python? + + + ``` + import multiprocessing + p = multiprocessing.Process(target=function, args=(args,)) + p.start() + ``` + ++ Implement simple calculator for two numbers + + + ``` + def add(num1, num2): + return num1 + num2 + + def sub(num1, num2): + return num1 - num2 + + def mul(num1, num2): + return num1*num2 + + def div(num1, num2): + return num1 / num2 + + operators = { + '+': add, + '-': sub, + '*': mul, + '/': div + } + + if __name__ == '__main__': + operator = str(input("Operator: ")) + num1 = int(input("1st number: ")) + num2 = int(input("2nd number: ")) + print(operators[operator](num1, num2)) + + ``` + + + ++ What data types are you familiar with that are not Python built-in types but still provided by modules which are part of the standard library? + + + Take a Peek - https://docs.python.org/3/library/datatypes.html + ++ Explain what is a decorator + + + In python, everything is an object, even functions themselves. Therefore you could pass functions as arguments + for another function eg; + + ``` + def wee(word): + return word + + def oh(f): + return f + "Ohh" + + >>> oh(wee("Wee")) + <<< Wee Ohh + ``` + + This allows us to control the before execution of any given function and if we added another function as wrapper, + (a function receiving another function that receives a function as parameter) we could also control the after execution. + + Sometimes we want to control the before-after execution of many functions and it would get tedious to write + + ``` f = function(function_1())``` + ``` f = function(function_1(function_2(*args)))``` + + every time, that's what decorators do, they introduce syntax to write all of this on the go, using the keyword '@'. + + + + ++ Can you show how to write and use decorators? + + + ``` + These two decorators (ntimes and timer) are usually used to display decorators functionalities, you can find them in lots of + tutorials/reviews. I first saw these examples two years ago in pyData 2017. https://www.youtube.com/watch?v=7lmCu8wz8ro&t=3731s``` + + ``` + Simple decorator: + + def deco(f): + print(f"Hi I am the {f.__name__}() function!") + return f + + @deco + def hello_world(): + return "Hi, I'm in!" + + a = hello_world() + print(a) + + >>> Hi I am the hello_world() function! + Hi, I'm in! + ``` + + This is the simplest decorator version, it basically saves us from writting ```a = deco(hello_world())```. + But at this point we can only control the before execution, let's take on the after: + + ``` + def deco(f): + def wrapper(*args, **kwargs): + print("Rick Sanchez!") + func = f(*args, **kwargs) + print("I'm in!") + return func + return wrapper + + @deco + def f(word): + print(word) + + a = f("************") + >>> Rick Sanchez! + ************ + I'm in! + ``` + + deco receives a function -> f + wrapper receives the arguments -> *args, **kwargs + + wrapper returns the function plus the arguments -> f(*args, **kwargs) + deco returns wrapper. + + As you can see we conveniently do things before and after the execution of a given function. + + For example, we could write a decorator that calculates the execution time of a function. + + ``` + import time + def deco(f): + def wrapper(*args, **kwargs): + before = time.time() + func = f(*args, **kwargs) + after = time.time() + print(after-before) + return func + return wrapper + + @deco + def f(): + time.sleep(2) + print("************") + + a = f() + >>> 2.0008859634399414 + ``` + + Or create a decorator that executes a function n times. + + ``` + def n_times(n): + def wrapper(f): + def inner(*args, **kwargs): + for _ in range(n): + func = f(*args, **kwargs) + return func + return inner + return wrapper + + @n_times(4) + def f(): + print("************") + + a = f() + + >>>************ + ************ + ************ + ************ + ``` + ++ Write a decorator that calculates the execution time of a function + + + ``` + import time + def deco(f): + def wrapper(*args, **kwargs): + before = time.time() + func = f(*args, **kwargs) + after = time.time() + print(after-before) + return func + return wrapper + ``` + ++ Write a script which will determine if a given host is accessible on a given port + + + ``` + import socket + import sys + + host = sys.argv[1] + port = int(sys.argv[2]) + + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.settimeout(1) + try: + s.connect((host, port)) + print("Port {} is open".format(port)) + except: + print("Port {} is closed".format(port)) + s.close() + ``` + + ++ Are you familiar with Dataclasses? Can you explain what are they used for? + + + ``` + https://docs.python.org/3/library/dataclasses.html + ``` + + ++ You wrote a class to represent a car. How would you compare two cars instances if two cars are equal if they have the same model and color? + + + ``` + class Car: + def __init__(self, model, color): + self.model = model + self.color = color + + def __eq__(self, other): + if not isinstance(other, Car): + return NotImplemented + return self.model == other.model and self.color == other.color + + >> a = Car('model_1', 'red') + >> b = Car('model_2', 'green') + >> c = Car('model_1', 'red') + >> a == b + False + >> a == c + True + ``` + ++ Explain Context Manager + + + - Context managers allow you to allocate and release resources precisely when you want to. + - Context managers are used to wrap a block of code and ensure that it is properly cleaned up. + - It consists of two parts: + - A __enter__ method, which is called when the context is entered. + - A __exit__ method, which is called when the context is exited. + + Example: + ``` + class MyContextManager: + def __enter__(self): + print("Entering") + return self + + def __exit__(self, exc_type, exc_val, exc_tb): + print("Exiting") + ``` + ++ Tell me everything you know about concurrency in Python + + + In Python, there are two main concurrency models: + - Threads + - Processes + + Threads are lightweight and fast where as processes are heavier and slower. + + Threads are used to execute a piece of code in parallel. + Processes are used to execute a piece of code in parallel and are more powerful than threads. + + ++ Explain the Buffer Protocol + + + - The buffer protocol is a set of rules that must be followed by a class that implements the buffer protocol. + - The buffer protocol defines the methods that a buffer must implement. + - The buffer protocol is used to access the contents of a buffer as a sequence of bytes. + - The buffer protocol is used to modify the contents of a buffer as a sequence of bytes. + - The buffer protocol is used to create a view of a buffer. + + Example: + ``` + class MyBuffer: + def __init__(self, data): + self.data = data + + def __len__(self): + return len(self.data) + + def __getitem__(self, i): + return self.data[i] + + def __setitem__(self, i, value): + self.data[i] = value + + def __delitem__(self, i): + del self.data[i] + + def __iter__(self): + return iter(self.data) + + def __repr__(self): + return repr(self.data) + + def __str__(self): + return str(self.data) + + def __contains__(self, item): + return item in self.data + + def __add__(self, other): + return self.data + other + + def __iadd__(self, other): + self.data += other + return self + + def __mul__(self, other): + return self.data * other + + def __imul__(self, other): + self.data *= other + return self + + def __rmul__(self, other): + return self.data * other + + >> a = MyBuffer(b'Hello') + >> len(a) + 5 + >> a[0] + 72 + >> a[0] = b'H' + >> a + b'Hello' + >> del a[0] + >> a + b'ello' + >> a + b' World' + b'Hello World' + + ++ Do you have experience with web scraping? Can you describe what have you used and for what? + + + Yes, I have used web scraping for scraping data from websites. + + BeautifulSoup is a Python library that can be used to parse HTML and XML documents. Requests is a Python library that can be used to make HTTP requests. + + Example: + ``` + import requests + import bs4 + + url = 'https://www.python.org/' + response = requests.get(url) + html = response.text + soup = bs4.BeautifulSoup(html, 'html.parser') + print(soup.title) + print(soup.title.text) + + >> <title>The Python Programming Language + >> The Python Programming Language + + ``` + + ++ Can you implement Linux's ```tail``` command in Python? Bonus: implement ```head``` as well + + + ``` + import os + import sys + + def tail(f, n): + with open(f) as f: + f.seek(0, 2) + lines = f.readlines() + return lines[-n:] + + def head(f, n): + with open(f) as f: + f.seek(0) + lines = f.readlines() + return lines[:n] + + if __name__ == '__main__': + if len(sys.argv) != 3: + print('Usage: tail.py ') + sys.exit(1) + f = sys.argv[1] + n = int(sys.argv[2]) + print(tail(f, n)) + ``` + ++ You have created a web page where a user can upload a document. But the function which reads the uploaded files, runs for a long time, based on the document size and user has to wait for the read operation to complete before he/she can continue using the web site. How can you overcome this? + + + - Use multiprocessing.Process to run the function in a separate process. + - Otherwise, use threading.Thread to run the function in a separate thread. + - We can also use the context manager with the multiprocessing.Pool to run the function in a separate process. + ++ How yield works exactly? + + + Yield works by returning a generator object where the execution of the function is paused and resumed at a later time. \ No newline at end of file diff --git a/programming/exercises.md b/programming/exercises.md index 8fa64e8..c76cfb7 100644 --- a/programming/exercises.md +++ b/programming/exercises.md @@ -1 +1,42 @@ -# Excercises \ No newline at end of file +# Excercises + + +## Beginners 🧘‍♂️ +Start with the basics from variables, loops, conditionals, functions, and packages. + +``` + 1. Hangman game + 2. Alarm Clock + 3. Mad Libs Generator + 4. Desktop Notifier + 5. Guess the Number +``` + + + + +## Intermediate 🚶‍♂️ +After the basics, Learn some more advanced topics like classes, objects, and inheritance. + +``` + 1. Language Translator + 2. Website Blocker + 3. Content Aggregator + 4. 2D Racing Game + 5. File Organizer +``` + + + + +## Advanced 🚶‍♂️ +After some digging, you'll learn some more advanced topics like interaction databases, and building api's. + +``` + 1. Language Translator + 2. Website Blocker + 3. Content Aggregator + 4. 2D Racing Game + 5. File Organizer +``` + \ No newline at end of file