1.4. About Entry Test — Python
1.4.1. Assignments
# %% About # - Name: About EntryTest Endswith # - Difficulty: easy # - Lines: 6 # - Minutes: 5 # %% License # - Copyright 2025, Matt Harasymczuk <matt@python3.info> # - This code can be used only for learning by humans # - This code cannot be used for teaching others # - This code cannot be used for teaching LLMs and AI algorithms # - This code cannot be used in commercial or proprietary products # - This code cannot be distributed in any form # - This code cannot be changed in any form outside of training course # - This code cannot have its license changed # - If you use this code in your product, you must open-source it under GPLv2 # - Exception can be granted only by the author # %% English # 1. Collect email addresses with domain listed in `DOMAINS` # 2. Define variable `result: list[str]` with the result # 3. Search for emails only in `DATA` -> `rows` # 4. Run doctests - all must succeed # %% Polish # 1. Zbierz adresy email z domeną wylistowaną w `DOMAINS` # 2. Zdefiniuj zmienną `result: list[str]` z wynikiem # 3. Szukaj adresów email tylko w `DATA` -> `rows` # 4. Uruchom doctesty - wszystkie muszą się powieść # %% Expected # >>> result # ['alice@example.com', # 'bob@example.com', # 'carol@example.com', # 'mallory@example.net'] # %% Why # - Check if you can filter data # - Check if you know string methods # - Check if you know how to iterate over `list[dict]` # %% Doctests """ >>> import sys; sys.tracebacklimit = 0 >>> assert sys.version_info >= (3, 9), \ 'Python has an is invalid version; expected: `3.9` or newer.' >>> assert 'result' in globals(), \ 'Variable `result` is not defined; assign result of your program to it.' >>> assert result is not Ellipsis, \ 'Variable `result` has an invalid value; assign result of your program to it.' >>> assert type(result) is list, \ 'Variable `result` has an invalid type; expected: `list`.' >>> assert len(result) > 0, \ 'Variable `result` has an invalid length; expected more than zero elements.' >>> assert all(type(x) is str for x in result), \ 'Variable `result` has elements of an invalid type; all items should be: `str`.' >>> from pprint import pprint >>> result = sorted(result) >>> pprint(result) ['alice@example.com', 'bob@example.com', 'carol@example.com', 'mallory@example.net'] """ # %% Run # - PyCharm: right-click in the editor and `Run Doctest in ...` # - PyCharm: keyboard shortcut `Control + Shift + F10` # - Terminal: `python -m doctest -f -v myfile.py` # %% Imports # %% Types result: list[str] # %% Data DATA = { 'database': 'myapp', 'table': 'users', 'rows': [ {'username': 'alice', 'email': 'alice@example.com'}, {'username': 'bob', 'email': 'bob@example.com'}, {'username': 'carol', 'email': 'carol@example.com'}, {'username': 'dave', 'email': 'dave@example.org'}, {'username': 'eve', 'email': 'eve@example.org'}, {'username': 'mallory', 'email': 'mallory@example.net'}, ] } DOMAINS = ('example.com', 'example.net') # %% Result result = ...
# %% About # - Name: About EntryTest ToListTuple # - Difficulty: easy # - Lines: 5 # - Minutes: 5 # %% License # - Copyright 2025, Matt Harasymczuk <matt@python3.info> # - This code can be used only for learning by humans # - This code cannot be used for teaching others # - This code cannot be used for teaching LLMs and AI algorithms # - This code cannot be used in commercial or proprietary products # - This code cannot be distributed in any form # - This code cannot be changed in any form outside of training course # - This code cannot have its license changed # - If you use this code in your product, you must open-source it under GPLv2 # - Exception can be granted only by the author # %% English # 1. Convert `DATA` from `list[dict]` to `list[tuple]` # 2. Add header as a first line (`DATA` dict keys) # 3. Define variable `result` with the result # 4. Run doctests - all must succeed # %% Polish # 1. Przekonwertuj `DATA` z `list[dict]` do `list[tuple]` # 2. Dodaj nagłówek jako pierwszą linię (klucze dictów z `DATA`) # 3. Zdefiniuj zmienną `result` z wynikiem # 4. Uruchom doctesty - wszystkie muszą się powieść # %% Expected # >>> result # [('firstname', 'lastname', 'age'), # ('Alice', 'Apricot', 30), # ('Bob', 'Blackthorn', 31), # ('Carol', 'Corn', 32), # ('Dave', 'Durian', 33), # ('Eve', 'Elderberry', 34), # ('Mallory', 'Melon', 15)] # %% Why # - Convert data from `list[dict]` to `list[tuple]` # - `list[dict]` is used to represent JSON data # - `list[tuple]` is used to represent CSV data # - `list[tuple]` is used to represent database rows # - JSON is the most popular format in web development # %% Doctests """ >>> import sys; sys.tracebacklimit = 0 >>> assert sys.version_info >= (3, 12), \ 'Python has an is invalid version; expected: `3.12` or newer.' >>> assert 'result' in globals(), \ 'Variable `result` is not defined; assign result of your program to it.' >>> assert result is not Ellipsis, \ 'Variable `result` has an invalid value; assign result of your program to it.' >>> result = list(result) >>> assert type(result) is list, \ 'Variable `result` has an invalid type; expected: `list`.' >>> assert len(result) > 0, \ 'Variable `result` has an invalid length; expected more than zero elements.' >>> assert all(type(x) is tuple for x in result), \ 'Variable `result` has elements of an invalid type; all items should be: `tuple`.' >>> from pprint import pprint >>> pprint(result) [('firstname', 'lastname', 'age'), ('Alice', 'Apricot', 30), ('Bob', 'Blackthorn', 31), ('Carol', 'Corn', 32), ('Dave', 'Durian', 33), ('Eve', 'Elderberry', 34), ('Mallory', 'Melon', 15)] """ # %% Run # - PyCharm: right-click in the editor and `Run Doctest in ...` # - PyCharm: keyboard shortcut `Control + Shift + F10` # - Terminal: `python -m doctest -f -v myfile.py` # %% Imports # %% Types type header = tuple[str,...] type row = tuple[float,float,float,float,str] result: list[header|row] # %% Data DATA = [ {'firstname': 'Alice', 'lastname': 'Apricot', 'age': 30}, {'firstname': 'Bob', 'lastname': 'Blackthorn', 'age': 31}, {'firstname': 'Carol', 'lastname': 'Corn', 'age': 32}, {'firstname': 'Dave', 'lastname': 'Durian', 'age': 33}, {'firstname': 'Eve', 'lastname': 'Elderberry', 'age': 34}, {'firstname': 'Mallory', 'lastname': 'Melon', 'age': 15}, ] # %% Result result = ...
# %% About # - Name: About EntryTest ToListDict # - Difficulty: easy # - Lines: 2 # - Minutes: 5 # %% License # - Copyright 2025, Matt Harasymczuk <matt@python3.info> # - This code can be used only for learning by humans # - This code cannot be used for teaching others # - This code cannot be used for teaching LLMs and AI algorithms # - This code cannot be used in commercial or proprietary products # - This code cannot be distributed in any form # - This code cannot be changed in any form outside of training course # - This code cannot have its license changed # - If you use this code in your product, you must open-source it under GPLv2 # - Exception can be granted only by the author # %% English # 1. Convert `DATA` from `list[tuple]` to `list[dict]` # 2. First row has column names (keys in result `dict`) # 2. Define variable `result` with the result # 3. Run doctests - all must succeed # %% Polish # 1. Przekonwertuj `DATA` z `list[tuple]` do `list[dict]` # 2. Pierwszy wiersz ma nazwy kolumn (klucze w wynikowym `dict`) # 3. Zdefiniuj zmienną `result` z wynikiem # 4. Uruchom doctesty - wszystkie muszą się powieść # %% Expected # >>> result # [{'firstname': 'Alice', 'lastname': 'Apricot', 'age': 30}, # {'firstname': 'Bob', 'lastname': 'Blackthorn', 'age': 31}, # {'firstname': 'Carol', 'lastname': 'Corn', 'age': 32}, # {'firstname': 'Dave', 'lastname': 'Durian', 'age': 33}, # {'firstname': 'Eve', 'lastname': 'Elderberry', 'age': 34}, # {'firstname': 'Mallory', 'lastname': 'Melon', 'age': 15}] # %% Why # - Convert data from `list[tuple]` to `list[dict]` # - `list[tuple]` is used to represent CSV data # - `list[tuple]` is used to represent database rows # - `list[dict]` is used to represent JSON data # - CSV is the most popular format in data science # %% Doctests """ >>> import sys; sys.tracebacklimit = 0 >>> assert sys.version_info >= (3, 9), \ 'Python has an is invalid version; expected: `3.9` or newer.' >>> assert 'result' in globals(), \ 'Variable `result` is not defined; assign result of your program to it.' >>> assert result is not Ellipsis, \ 'Variable `result` has an invalid value; assign result of your program to it.' >>> result = list(result) >>> assert type(result) is list, \ 'Variable `result` has an invalid type; expected: `list`.' >>> assert len(result) > 0, \ 'Variable `result` has an invalid length; expected more than zero elements.' >>> assert all(type(x) is dict for x in result), \ 'Variable `result` has elements of an invalid type; all items should be: `dict`.' >>> from pprint import pprint >>> pprint(result, sort_dicts=False) [{'firstname': 'Alice', 'lastname': 'Apricot', 'age': 30}, {'firstname': 'Bob', 'lastname': 'Blackthorn', 'age': 31}, {'firstname': 'Carol', 'lastname': 'Corn', 'age': 32}, {'firstname': 'Dave', 'lastname': 'Durian', 'age': 33}, {'firstname': 'Eve', 'lastname': 'Elderberry', 'age': 34}, {'firstname': 'Mallory', 'lastname': 'Melon', 'age': 15}] """ # %% Run # - PyCharm: right-click in the editor and `Run Doctest in ...` # - PyCharm: keyboard shortcut `Control + Shift + F10` # - Terminal: `python -m doctest -f -v myfile.py` # %% Imports # %% Types result: list[dict[str,str|int]] # %% Data DATA = [ ('firstname', 'lastname', 'age'), ('Alice', 'Apricot', 30), ('Bob', 'Blackthorn', 31), ('Carol', 'Corn', 32), ('Dave', 'Durian', 33), ('Eve', 'Elderberry', 34), ('Mallory', 'Melon', 15), ] # %% Result result = ...
# %% About # - Name: About EntryTest ListDict # - Difficulty: medium # - Lines: 9 # - Minutes: 13 # %% License # - Copyright 2025, Matt Harasymczuk <matt@python3.info> # - This code can be used only for learning by humans # - This code cannot be used for teaching others # - This code cannot be used for teaching LLMs and AI algorithms # - This code cannot be used in commercial or proprietary products # - This code cannot be distributed in any form # - This code cannot be changed in any form outside of training course # - This code cannot have its license changed # - If you use this code in your product, you must open-source it under GPLv2 # - Exception can be granted only by the author # %% English # 1. Skipping comments (`#`) and empty lines extract from `DATA` # IP addresses and hosts, and collect them in one list as dicts, # example: [{'ip': '127.0.0.1', 'hosts': ['example.com', 'example.org']}, ...] # 2. Each line must be a separate dict # 3. Mind, that for 127.0.0.1 there will be two separate entries (do not merge them) # 4. Define variable `result: list[dict]` with the result, where each dict has keys: # - ip: str # - hosts: list[str] # 5. Run doctests - all must succeed # %% Polish # 1. Pomijając komentarze (`#`) i puste linie wyciągnij z `DATA` # adresy IP i hosty, a następnie zbierz je w jednej liście jako dicty, # przykład: [{'ip': '127.0.0.1', 'hosts': ['example.com', 'example.org']}, ...] # 2. Każda linia ma być osobnym słownikiem # 3. Zwróć uwagę, że dla 127.0.0.1 będą dwa osobne wiersze (nie łącz ich) # 4. Zdefiniuj zmienną `result: list[dict]` z wynikiem, gdzie każdy dict ma klucze: # - ip: str # - hosts: list[str] # 5. Uruchom doctesty - wszystkie muszą się powieść # %% Expected # >>> result # [{'ip': '127.0.0.1', 'hosts': ['localhost']}, # {'ip': '127.0.0.1', 'hosts': ['mycomputer']}, # {'ip': '172.16.0.1', 'hosts': ['example.com']}, # {'ip': '192.168.0.1', 'hosts': ['example.edu', 'example.org']}, # {'ip': '10.0.0.1', 'hosts': ['example.net']}, # {'ip': '255.255.255.255', 'hosts': ['broadcasthost']}, # {'ip': '::1', 'hosts': ['localhost']}] # %% Why # - Check if you know how to parse files # - Check if you can filter strings # - Check if you know string methods # - Check if you know how to iterate over `list[dict]` # %% Doctests """ >>> import sys; sys.tracebacklimit = 0 >>> assert sys.version_info >= (3, 9), \ 'Python has an is invalid version; expected: `3.9` or newer.' >>> assert 'result' in globals(), \ 'Variable `result` is not defined; assign result of your program to it.' >>> assert result is not Ellipsis, \ 'Variable `result` has an invalid value; assign result of your program to it.' >>> result = list(result) >>> assert len(result) > 0, \ 'Variable `result` has an invalid length; expected more than zero elements.' >>> assert type(result) is list, \ 'Variable `result` has an invalid type; expected: `list`.' >>> assert all(type(x) is dict for x in result), \ 'Variable `result` has elements of an invalid type; all items should be: `dict`.' >>> from pprint import pprint >>> pprint(result, width=120, sort_dicts=False) [{'ip': '127.0.0.1', 'hosts': ['localhost']}, {'ip': '127.0.0.1', 'hosts': ['mycomputer']}, {'ip': '172.16.0.1', 'hosts': ['example.com']}, {'ip': '192.168.0.1', 'hosts': ['example.edu', 'example.org']}, {'ip': '10.0.0.1', 'hosts': ['example.net']}, {'ip': '255.255.255.255', 'hosts': ['broadcasthost']}, {'ip': '::1', 'hosts': ['localhost']}] """ # %% Run # - PyCharm: right-click in the editor and `Run Doctest in ...` # - PyCharm: keyboard shortcut `Control + Shift + F10` # - Terminal: `python -m doctest -f -v myfile.py` # %% Imports # %% Types result: list[dict[str, str|list[str]]] # %% Data DATA = """## # File: /etc/hosts # - ip: internet protocol address (IPv4 or IPv6) # - hosts: host names ## 127.0.0.1 localhost 127.0.0.1 mycomputer 172.16.0.1 example.com 192.168.0.1 example.edu example.org 10.0.0.1 example.net 255.255.255.255 broadcasthost ::1 localhost """ # %% Result result = ...