1.4. About Entry Test

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. Define `result: list[str]` with all email addresses having domain name in `DOMAINS`
# 2. Search for emails only in `DATA` -> `rows`
# 3. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj `result: list[str]` z wszystkimi adresami email mającymi domenę z `DOMAINS`
# 2. Szukaj adresów email tylko w `DATA` -> `rows`
# 3. Uruchom doctesty - wszystkie muszą się powieść

# %% Example
# DATA = {
#     'database': 'myproject',
#     'table': 'users',
#     'rows': [
#         {'pk': 1, 'firstname': 'Melissa', 'lastname': 'Lewis', 'email': 'mlewis@nasa.gov'},
#         {'pk': 2, 'firstname': 'Rick', 'lastname': 'Martinez', 'email': 'rmartinez@nasa.gov'},
#         {'pk': 3, 'firstname': 'Alex', 'lastname': 'Vogel', 'email': 'avogel@esa.int'},
#         {'pk': 4, 'firstname': 'Pan', 'lastname': 'Twardowski', 'email': 'ptwardowski@polsa.gov.pl'},
#         {'pk': 5, 'firstname': 'Chris', 'lastname': 'Beck', 'email': 'cbeck@nasa.gov'},
#         {'pk': 6, 'firstname': 'Beth', 'lastname': 'Johanssen', 'email': 'bjohanssen@nasa.gov'},
#         {'pk': 7, 'firstname': 'Mark', 'lastname': 'Watney', 'email': 'mwatney@nasa.gov'},
#         {'pk': 8, 'firstname': 'Ivan', 'lastname': 'Ivanovich', 'email': 'iivanovich@roscosmos.ru'},
#     ]
#  }
#
# 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': 'myproject',
    'table': 'users',
    'rows': [
        {'firstname': 'Alice', 'lastname': 'Apricot', 'email': 'alice@example.com'},
        {'firstname': 'Bob', 'lastname': 'Banana', 'email': 'bob@example.com'},
        {'firstname': 'Carol', 'lastname': 'Corn', 'email': 'carol@example.com'},
        {'firstname': 'Dave', 'lastname': 'Durian', 'email': 'dave@example.org'},
        {'firstname': 'Eve', 'lastname': 'Elderberry', 'email': 'eve@example.org'},
        {'firstname': 'Mallory', 'lastname': 'Melon', '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
# 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ę
# 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. Define `result: list[dict]`, where each dict has keys:
#    - ip: str
#    - hosts: list[str]
# 2. Iterate over lines in `DATA` skipping comments (`#`) and empty lines
# 3. Extract from each line: `ip` and `hosts`
# 4. Add `ip` and `hosts` to `result` as a dict, example:
#    {'ip': '10.13.37.1', 'hosts': ['nasa.gov', 'esa.int']}
# 5. Each line must be a separate dict
# 6. Mind, that for 127.0.0.1 there will be two separate entries
# 7. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj `result: list[dict]`, gdzie każdy dict ma klucze:
#    - ip: str
#    - hosts: list[str]
# 2. Iteruj po liniach w `DATA` pomijając komentarze (`#`) i puste linie
# 3. Wyciągnij z każdej linii: `ip` i `hosts`
# 4. Dodaj `ip` i `hosts` do `result` jako słownik, przykład:
#    {'ip': '10.13.37.1', 'hosts': ['nasa.gov', 'esa.int']}
# 5. Każda linia ma być osobnym słownikiem
# 6. Zwróć uwagę, że dla 127.0.0.1 będą dwa osobne wiersze
# 7. 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, 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 = ...