1.6. About Entry Test

1.6.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ść

# %% Example
# 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'},
#     ]
# }
#
# result = [
#   'alice@example.com',
#   'bob@example.com',
#   'carol@example.com',
#   'dave@example.org']

# %% 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 3.9+ required'

>>> assert result is not Ellipsis, \
'Assign result to variable: `result`'
>>> assert type(result) is list, \
'Result must be a list'
>>> assert len(result) > 0, \
'Result cannot be empty'
>>> assert all(type(element) is str for element in result), \
'All elements in result must be a 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 -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ść

# %% Example
# DATA = [
#     {'firstname': 'Alice', 'lastname': 'Apricot', 'age': 30},
#     {'firstname': 'Bob', 'lastname': 'Banana', '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 = [
#     ('firstname', 'lastname', 'age'),
#     ('Alice', 'Apricot', 30),
#     ('Bob', 'Banana', 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 3.12+ required'

>>> assert result is not Ellipsis, \
'Assign result to variable: `result`'
>>> result = list(result)
>>> assert type(result) is list, \
'Variable `result` has invalid type, should be list'
>>> assert len(result) > 0, \
'Variable `result` should not be empty'
>>> assert all(type(row) is tuple for row in result), \
'Variable `result` should be a list[tuple]'

>>> from pprint import pprint
>>> pprint(result)
[('firstname', 'lastname', 'age'),
 ('Alice', 'Apricot', 30),
 ('Bob', 'Banana', 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 -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': 'Banana', '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ść

# %% Example
# DATA = [
#     ('firstname', 'lastname', 'age'),
#     ('Alice', 'Apricot', 30),
#     ('Bob', 'Banana', 31),
#     ('Carol', 'Corn', 32),
#     ('Dave', 'Durian', 33),
#     ('Eve', 'Elderberry', 34),
#     ('Mallory', 'Melon', 15)]
#
# result = [
#     {'firstname': 'Alice', 'lastname': 'Apricot', 'age': 30},
#     {'firstname': 'Bob', 'lastname': 'Banana', '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 3.9+ required'

>>> assert result is not Ellipsis, \
'Assign result to variable: `result`'
>>> result = list(result)
>>> assert type(result) is list, \
'Result must be a list'
>>> assert len(result) > 0, \
'Result cannot be empty'
>>> assert all(type(element) is dict for element in result), \
'All elements in result must be a dict'

>>> from pprint import pprint
>>> pprint(result, sort_dicts=False)
[{'firstname': 'Alice', 'lastname': 'Apricot', 'age': 30},
 {'firstname': 'Bob', 'lastname': 'Banana', '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 -v myfile.py`

# %% Imports

# %% Types
result: list[dict[str,str|int]]

# %% Data
DATA = [
    ('firstname', 'lastname', 'age'),
    ('Alice', 'Apricot', 30),
    ('Bob', 'Banana', 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
# 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
# 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ść

# %% Example
# DATA = """##
#     # `/etc/hosts` structure:
#     #    - ip: internet protocol address (IPv4 or IPv6)
#     #    - hosts: host names
#      ##
#
#     127.0.0.1       localhost
#     127.0.0.1       astromatt
#     10.13.37.1      nasa.gov esa.int
#     255.255.255.255 broadcasthost
#     ::1             localhost"""
#
# RESULT = [
#     {'ip': '127.0.0.1', 'hosts': ['localhost']},
#     {'ip': '127.0.0.1', 'hosts': ['astromatt']},
#     {'ip': '10.13.37.1', 'hosts': ['nasa.gov', 'esa.int']},
#     {'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 3.9+ required'

>>> assert result is not Ellipsis, \
'Assign result to variable: `result`'
>>> result = list(result)
>>> assert len(result) > 0, \
'Result cannot be empty'
>>> assert type(result) is list, \
'Variable `result` has invalid type, should be list'
>>> assert all(type(x) is dict for x in result), \
'All keys in `result` 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': ['astromatt']},
 {'ip': '10.13.37.1', 'hosts': ['nasa.gov', 'esa.int']},
 {'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 -v myfile.py`

# %% Imports

# %% Types
result: list[dict[str, str|list[str]]]

# %% Data
DATA = """##
# `/etc/hosts` structure:
#    - ip: internet protocol address (IPv4 or IPv6)
#    - hosts: host names
 ##

127.0.0.1       localhost
127.0.0.1       astromatt
10.13.37.1      nasa.gov esa.int
255.255.255.255 broadcasthost
::1             localhost"""

# %% Result
result = ...