1.3. Entry Test¶

1.3.1. Assignments¶

Code 1.13. Entry Test Assignment A
"""
* Complexity: easy
* Lines of code: 1 lines
* Time: 2 min

English:
1. Define variable result with value 1
2. Run doctests - all must succeed

Polish:
1. Zdefiniuj zmienną result z wartością 1
2. Uruchom doctesty - wszystkie muszą się powieść

Tests:
>>> import sys; sys.tracebacklimit = 0
>>> from pprint import pprint

>>> assert result is not Ellipsis, \
'Assign result to variable: result'
>>> assert type(result) is int, \
'Variable result has invalid type, should be int'

>>> pprint(result)
1
"""

# Define variable result with value 1
# type: int
result = ...


Code 1.14. Entry Test Assignment B
"""
* Complexity: easy
* Lines of code: 2 lines
* Time: 5 min

English:
1. Define result: list[dict]:
2. Convert DATA from list[tuple] to list[dict]
a. key - name from the header
b. value - numerical value or species name
3. Run doctests - all must succeed

Polish:
1. Zdefiniuj result: list[dict]:
2. Przekonwertuj DATA z list[tuple] do list[dict]
a. klucz - nazwa z nagłówka
b. wartość - wartość numeryczna lub nazwa gatunku
3. Uruchom doctesty - wszystkie muszą się powieść

Tests:
>>> import sys; sys.tracebacklimit = 0
>>> from pprint import pprint

>>> 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'

>>> pprint(result)
[{'PetalLength': 5.1,
'PetalWidth': 1.9,
'SepalLength': 5.8,
'SepalWidth': 2.7,
'Species': 'virginica'},
{'PetalLength': 1.4,
'PetalWidth': 0.2,
'SepalLength': 5.1,
'SepalWidth': 3.5,
'Species': 'setosa'},
{'PetalLength': 4.1,
'PetalWidth': 1.3,
'SepalLength': 5.7,
'SepalWidth': 2.8,
'Species': 'versicolor'},
{'PetalLength': 5.6,
'PetalWidth': 1.8,
'SepalLength': 6.3,
'SepalWidth': 2.9,
'Species': 'virginica'},
{'PetalLength': 4.5,
'PetalWidth': 1.5,
'SepalLength': 6.4,
'SepalWidth': 3.2,
'Species': 'versicolor'},
{'PetalLength': 1.3,
'PetalWidth': 0.2,
'SepalLength': 4.7,
'SepalWidth': 3.2,
'Species': 'setosa'}]
"""

DATA = [
('SepalLength', 'SepalWidth', 'PetalLength', 'PetalWidth', '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')]

# Convert DATA from list[tuple] to list[dict]
# type: list[dict[str,float|str]]
result = ...


Code 1.15. Entry Test Assignment C
"""
* Complexity: easy
* Lines of code: 5 lines
* Time: 5 min

English:
1. Load DATA from JSON format
2. Convert data to result: list[tuple]
4. Run doctests - all must succeed

Polish:
1. Wczytaj DATA z formatu JSON
2. Przekonwertuj dane do result: list[tuple]
3. Dodaj nagłówek jako pierwszą linię
4. Uruchom doctesty - wszystkie muszą się powieść

Tests:
>>> import sys; sys.tracebacklimit = 0
>>> from pprint import pprint

>>> 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]'

>>> pprint(result)
[('SepalLength', 'SepalWidth', 'PetalLength', 'PetalWidth', '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')]
"""

DATA = [
{'SepalLength': 5.8, 'SepalWidth': 2.7, 'PetalLength': 5.1, 'PetalWidth': 1.9, 'Species': 'virginica'},
{'SepalLength': 5.1, 'SepalWidth': 3.5, 'PetalLength': 1.4, 'PetalWidth': 0.2, 'Species': 'setosa'},
{'SepalLength': 5.7, 'SepalWidth': 2.8, 'PetalLength': 4.1, 'PetalWidth': 1.3, 'Species': 'versicolor'},
{'SepalLength': 6.3, 'SepalWidth': 2.9, 'PetalLength': 5.6, 'PetalWidth': 1.8, 'Species': 'virginica'},
{'SepalLength': 6.4, 'SepalWidth': 3.2, 'PetalLength': 4.5, 'PetalWidth': 1.5, 'Species': 'versicolor'},
{'SepalLength': 4.7, 'SepalWidth': 3.2, 'PetalLength': 1.3, 'PetalWidth': 0.2, 'Species': 'setosa'}]

# Convert DATA from list[dict] to list[tuple]
# type: row = tuple[float,float,float,float,str]
result = ...


Code 1.16. Entry Test Assignment D
"""
* Complexity: easy
* Lines of code: 5 lines
* Time: 5 min

English:
1. Define result: list[str]
2. Collect in result all email addresses from DATA -> crew
with domain names mentioned in DOMAINS
3. Run doctests - all must succeed

Polish:
1. Zdefiniuj result: list[str]
2. Zbierz w result wszystkie adresy email z DATA -> crew
z nazwami domenowymi wymienionymi w DOMAINS
3. Uruchom doctesty - wszystkie muszą się powieść

Tests:
>>> import sys; sys.tracebacklimit = 0
>>> from pprint import pprint

>>> 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'

>>> pprint(result)
['mlewis@nasa.gov',
'rmartinez@nasa.gov',
'avogel@esa.int',
'cbeck@nasa.gov',
'bjohanssen@nasa.gov',
'mwatney@nasa.gov']
"""

DATA = {
'mission': 'Ares 3',
'launch': '2035-06-29',
'landing': '2035-11-07',
'destination': 'Mars',
'location': 'Acidalia Planitia',
'crew': [{'name': 'Melissa Lewis', 'email': 'mlewis@nasa.gov'},
{'name': 'Rick Martinez', 'email': 'rmartinez@nasa.gov'},
{'name': 'Alex Vogel', 'email': 'avogel@esa.int'},
{'name': 'Chris Beck', 'email': 'cbeck@nasa.gov'},
{'name': 'Beth Johanssen', 'email': 'bjohanssen@nasa.gov'},
{'name': 'Mark Watney', 'email': 'mwatney@nasa.gov'},
{'name': 'Pan Twardowski', 'email': 'ptwardowski@polsa.gov.pl'},
{'name': 'Ivan Ivanovich', 'email': 'iivanovich@roscosmos.ru'}]}

DOMAINS = ('esa.int', 'nasa.gov')

# Email addresses with top-level domain in DOMAINS
# type: list[str]
result = ...


Code 1.17. Entry Test Assignment E
"""
* Complexity: hard
* Lines of code: 15 lines
* Time: 13 min

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': '127.0.0.1', 'hosts': ['localhost', 'astromatt']}
5. Each line must be a separate dict
6. Merge host names with the same IP (important!)
7. Run doctests - all must succeed

Polish:
1. Zdefiniuj result: list[dict], gdzie każdy dict ma klucze:
* ip: str
* hosts: list[str]
2. Iteruje 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': '127.0.0.1', 'hosts': ['localhost', 'astromatt']}
5. Każda linia ma być osobnym słownikiem
6. Scal nazwy hostów dla tego samego IP (ważne)
7. Uruchom doctesty - wszystkie muszą się powieść

Tests:
>>> import sys; sys.tracebacklimit = 0
>>> from pprint import pprint

>>> 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'
>>> assert [x['ip'] for x in result].count('127.0.0.1') == 1, \
'You did not merge hostnames for the same ip (127.0.0.1)'

>>> pprint(result, width=120, sort_dicts=False)
[{'ip': '127.0.0.1', 'hosts': ['localhost', 'astromatt']},
{'ip': '10.13.37.1', 'hosts': ['nasa.gov', 'esa.int']},
{'ip': '::1', 'hosts': ['localhost']}]
"""

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