1.4. Entry Test

1.5. Assignments

Code 1.48. Entry Test Assignment A
"""
* Assignment: Entry Test List of Dict
* Complexity: easy
* Lines of code: 6 lines
* Time: 8 min

English:
    1. Use data from "Given" section (see below)
    2. Define `result: list[dict]`:
    3. Convert `DATA` from `list[tuple]` to `list[dict]`
        a. key - name from the header
        b. value - numerical value or species name
    4. Compare result with "Tests" section (see below)

Polish:
    1. Użyj danych z sekcji "Given" (patrz poniżej)
    2. Zdefiniuj `result: list[dict]`:
        a. klucz - nazwa z nagłówka
        b. wartość - wartość numeryczna lub nazwa gatunku
    3. Przekonwertuj `DATA` z `list[tuple]` do `list[dict]`
    4. Porównaj wyniki z sekcją "Tests" (patrz poniżej)

Tests:
    >>> assert type(result) is list
    >>> assert all(type(row) is dict for row in result)
    >>> result  # doctest: +NORMALIZE_WHITESPACE
    [{'Sepal length': 5.8, 'Sepal width': 2.7, 'Petal length': 5.1, 'Petal width': 1.9, 'Species': 'virginica'},
     {'Sepal length': 5.1, 'Sepal width': 3.5, 'Petal length': 1.4, 'Petal width': 0.2, 'Species': 'setosa'},
     {'Sepal length': 5.7, 'Sepal width': 2.8, 'Petal length': 4.1, 'Petal width': 1.3, 'Species': 'versicolor'},
     {'Sepal length': 6.3, 'Sepal width': 2.9, 'Petal length': 5.6, 'Petal width': 1.8, 'Species': 'virginica'},
     {'Sepal length': 6.4, 'Sepal width': 3.2, 'Petal length': 4.5, 'Petal width': 1.5, 'Species': 'versicolor'},
     {'Sepal length': 4.7, 'Sepal width': 3.2, 'Petal length': 1.3, 'Petal width': 0.2, 'Species': 'setosa'}]
"""


# Given
DATA = [('Sepal length', 'Sepal width', 'Petal length', 'Petal width', 'Species'),
        (5.8, 2.7, 5.1, 1.9, 'virginica'),
        (5.1, 3.5, 1.4, 0.2, 'setosa'),
        (5.7, 2.8, 4.1, 1.3, 'versicolor'),
        (6.3, 2.9, 5.6, 1.8, 'virginica'),
        (6.4, 3.2, 4.5, 1.5, 'versicolor'),
        (4.7, 3.2, 1.3, 0.2, 'setosa')]


result: list


Code 1.49. Entry Test Assignment B
"""
* Assignment: Entry Test File
* Complexity: hard
* Lines of code: 15 lines
* Time: 13 min

English:
    1. Use data from "Given" section (see below)
    2. Skip comments (`#`) and empty lines
    3. Extract from each line: ip, host and protocol and add to `result: list[dict]`
    4. Each line must be a separate dict
    5. Merge host names with the same IP
    6. IPv4 protocol address is when dot (`.`) is in ip address
    7. `result` must be list of dicts (`list[dict]`)
    8. Compare result with "Tests" section (see below)

Polish:
    1. Użyj danych z sekcji "Given" (patrz poniżej)
    2. Pomiń komentarze (`#`) i puste linie
    3. Wyciągnij z każdej linii: ip, host i protokół i dodaj do `result: list[dict]`
    4. Każda linia ma być osobnym słownikiem
    5. Protokół IPv4 jest gdy kropka (`.`) znajduje się w adresie
    6. Scal nazwy hostów dla tego samego IP
    7. Porównaj wyniki z sekcją "Tests" (patrz poniżej)

Tests:
    >>> type(result)
    <class 'list'>
    >>> len(result) > 0
    True
    >>> all(type(row) is dict
    ...     for row in result)
    True
    >>> result  # doctest: +NORMALIZE_WHITESPACE
    [{'ip': '127.0.0.1', 'hosts': ['localhost', 'astromatt'], 'protocol': 'ipv4'},
     {'ip': '10.13.37.1', 'hosts': ['nasa.gov', 'esa.int', 'roscosmos.ru'], 'protocol': 'ipv4'},
     {'ip': '255.255.255.255', 'hosts': ['broadcasthost'], 'protocol': 'ipv4'},
     {'ip': '::1', 'hosts': ['localhost'], 'protocol': 'ipv6'}]
"""


# Given
DATA = """
##
# `/etc/hosts` structure:
#   - IPv4 or IPv6
#   - Hostnames
 ##

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

result: list


Code 1.50. Entry Test Assignment C
"""
* Assignment: Entry Test Endswith
* Complexity: medium
* Lines of code: 4 lines
* Time: 13 min

English:
    1. Use data from "Given" section (see below)
    2. Define `result: set[str]`
    3. Iterate over `DATA`
    4. Append to `result` species with endings in `SUFFIXES`
    5. Compare result with "Tests" section (see below)

Polish:
    1. Użyj danych z sekcji "Given" (patrz poniżej)
    2. Zdefiniuj `result: set[str]`
    3. Iterując po `DATA`
    4. Dodaj do `result` nazwy gatunków z końcówkami w `SUFFIXES`
    5. Porównaj wyniki z sekcją "Tests" (patrz poniżej)

Tests:
    >>> type(result)
    <class 'set'>
    >>> len(result) > 0
    True
    >>> 'virginica' in result
    True
    >>> 'setosa' in result
    True
    >>> 'versicolor' in result
    False
"""


# Given
DATA = [('Sepal length', 'Sepal width', 'Petal length', 'Petal width', 'Species'),
        (5.8, 2.7, 5.1, 1.9, {'virginica'}),
        (5.1, 3.5, 1.4, 0.2, {'setosa'}),
        (5.7, 2.8, 4.1, 1.3, {'versicolor'}),
        (6.3, 2.9, 5.6, 1.8, {'virginica'}),
        (6.4, 3.2, 4.5, 1.5, {'versicolor'}),
        (4.7, 3.2, 1.3, 0.2, {'setosa'}),
        (7.0, 3.2, 4.7, 1.4, {'versicolor'}),
        (7.6, 3.0, 6.6, 2.1, {'virginica'}),
        (4.6, 3.1, 1.5, 0.2, {'setosa'})]

SUFFIXES = ('ca', 'osa')

result: set