11.3. Loop For Unpack

11.3.1. Recap

>>> a, b = 1, 2
>>> a, b = (1, 2)
>>> k, v = (1, 2)
>>> key, value = (1, 2)
>>> role, name = ('commander', 'Melissa Lewis')

11.3.2. List of Pairs

>>> DATA = [
...     ('commander', 'Melissa Lewis'),
...     ('botanist', 'Mark Watney'),
...     ('chemist', 'Alex Vogel'),
... ]
>>>
>>> for role, name in DATA:
...     print(f'{role} -> {name}')
commander -> Melissa Lewis
botanist -> Mark Watney
chemist -> Alex Vogel

11.3.3. List of Tuples

>>> DATA = [
...     (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'),
... ]
>>>
>>> for sepal_length, sepal_width, petal_length, petal_width, species in DATA:
...     print(f'{species} -> {sepal_length}')
setosa -> 5.1
versicolor -> 5.7
virginica -> 6.3
>>> DATA = [
...     (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'),
... ]
>>>
>>> for sl, sw, pl, pw, s in DATA:
...     print(f'{s} -> {sl}')
setosa -> 5.1
versicolor -> 5.7
virginica -> 6.3

11.3.4. Unused Values

>>> DATA = [
...     (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'),
... ]
>>>
>>> for sepal_length, _, _, _, species in DATA:
...     print(f'{species} -> {sepal_length}')
setosa -> 5.1
versicolor -> 5.7
virginica -> 6.3
>>> DATA = [
...     (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'),
... ]
>>>
>>> for sepal_length, *_, species in DATA:
...     print(f'{species} -> {sepal_length}')
virginica -> 5.8
setosa -> 5.1
versicolor -> 5.7

11.3.5. Mixed

>>> DATA = [
...     (1, 2),
...     ('name', 'Mark Watney'),
...     ('agency', ['NASA', 'ESA', 'POLSA']),
...     ((1, 2), ['botanist', 'commander', 'pilot']),
...     (['Ares1', 'Are2', 'Ares3'], 1),
... ]
>>>
>>> for first, second in DATA:
...     print(f'{first} -> {second}')
1 -> 2
name -> Mark Watney
agency -> ['NASA', 'ESA', 'POLSA']
(1, 2) -> ['botanist', 'commander', 'pilot']
['Ares1', 'Are2', 'Ares3'] -> 1

11.3.6. Assignments

"""
* Assignment: Loop Unpack Months
* Type: class assignment
* Complexity: easy
* Lines of code: 3 lines
* Time: 5 min

English:
    1. Use `DATA: list[tuple]`
    2. Define `result: dict` with month number and name:
        a. Keys: month number
        b. Values: month name
    3. Use unpack syntax in a for loop
    4. Run doctests - all must succeed

Polish:
    1. Użyj `DATA: list[tuple]`
    2. Zdefiniuj `result: dict` z numerem miesiąca i nazwą:
        a. Klucz: numer miesiąca
        b. Wartość: nazwa miesiąca
    3. Użyj składni rozpakowania w pętli for
    4. Uruchom doctesty - wszystkie muszą się powieść

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

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

    >>> assert all(type(x) is int for x in result.keys())
    >>> assert all(type(x) is str for x in result.values())
    >>> assert all(x in result.keys() for x in range(1, 13))
    >>> assert all(v in result.values() for k,v in DATA)

    >>> 13 not in result
    True
    >>> 0 not in result
    True

    >>> pprint(result)
    {1: 'January',
     2: 'February',
     3: 'March',
     4: 'April',
     5: 'May',
     6: 'June',
     7: 'July',
     8: 'August',
     9: 'September',
     10: 'October',
     11: 'November',
     12: 'December'}
"""

DATA = [
    (1, 'January'),
    (2, 'February'),
    (3, 'March'),
    (4, 'April'),
    (5, 'May'),
    (6, 'June'),
    (7, 'July'),
    (8, 'August'),
    (9, 'September'),
    (10, 'October'),
    (11, 'November'),
    (12, 'December'),
]

# Define `result: dict` with month number and name:
# - Keys: month number
# - Values: month name
# Use unpack syntax in a for loop
# type: dict[int,str]
result = ...

"""
* Assignment: Loop Unpack Unique
* Type: class assignment
* Complexity: easy
* Lines of code: 3 lines
* Time: 5 min

English:
    1. Use `DATA: list[tuple]`
    2. Define `result: set` with unique species names from `DATA`
    3. Use unpack syntax in a for loop
    5. Run doctests - all must succeed

Polish:
    1. Użyj `DATA: list[tuple]`
    2. Zdefiniuj `result: set` z unikalnymi nazwami gatunków z `DATA`
    3. Użyj składni rozpakowania w pętli for
    4. Uruchom doctesty - wszystkie muszą się powieść

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

    >>> assert result is not Ellipsis, \
    'Assign your result to variable `result`'
    >>> assert type(result) is set, \
    'Result must be a set'
    >>> 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'

    >>> 'virginica' in result
    True
    >>> 'setosa' in result
    True
    >>> 'versicolor' in result
    True
"""

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

# Define `result: set` with unique species names from `DATA`
# Use unpack syntax in a for loop
# type: set[str]
result = ...

"""
* Assignment: Loop Unpack Startswith
* Type: class assignment
* Complexity: easy
* Lines of code: 4 lines
* Time: 5 min

English:
    1. Use `DATA: list[tuple]`
    2. Define `result: set` with:
       species names with prefix listed in `PREFIXES`
    3. Iterating over `DATA` unpack row to:
        a. `sl,sw,pl,pw` - all beside last
        b. `species` - last in the row
    3. Use unpack syntax in a for loop
    5. Run doctests - all must succeed

Polish:
    1. Użyj `DATA: list[tuple]`
    2. Zdefiniuj `result: set` z:
       nazwami gatunków z prefiksem wylistowanym w `PREFIXES`
    3. Iterując po `DATA` rozpakuj wiersz do:
        a. `sl,sw,pl,pw` - wszystkie oprócz ostatniego
        b. `species` - ostatni w wierszu
    4. Użyj składni rozpakowania w pętli for
    5. Uruchom doctesty - wszystkie muszą się powieść

Hints:
    * `str.endswith()`

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

    >>> assert result is not Ellipsis, \
    'Assign your result to variable `result`'
    >>> assert type(result) is set, \
    'Result must be a set'
    >>> 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'

    >>> 'virginica' in result
    True
    >>> 'setosa' in result
    False
    >>> 'versicolor' in result
    True
"""

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

PREFIXES = ('ver', 'vir')

# Define `result: set` with:
# species names with prefix listed in `PREFIXES`
# Iterating over `DATA` unpack row to:
# - `sl,sw,pl,pw` - all beside last
# - `species` - last in the row
# Use unpack syntax in a for loop
# type: set[str]
result = ...

"""
* Assignment: Loop Unpack Between
* Complexity: easy
* Lines of code: 4 lines
* Time: 5 min

English:
    1. Use `DATA: list[tuple]` variable
    2. Define `result: list[tuple]` with first names and last names
       of people whose age is in the range 18 to 65
    3. Use unpack syntax in a for loop
    5. Run doctests - all must succeed

Polish:
    1. Użyj zmiennej `DATA: list[tuple]`
    2. Zdefiniuj `result: list[tuple]` imionami i nazwiskami osób,
       których wiek jest w przedziale 18 do 65 lat
    3. Użyj składni rozpakowania w pętli for
    5. Uruchom doctesty - wszystkie muszą się powieść

Why:
    * Check if you can filter data
    * Check if you know string methods
    * Check if you know how to iterate over list[dict]

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 tuple for element in result), \
    'All elements in result must be a tuple'

    >>> pprint(result)
    [('Mark', 'Watney'),
     ('Melissa', 'Lewis'),
     ('Rick', 'Martinez'),
     ('Alex', 'Vogel'),
     ('Chris', 'Beck'),
     ('Beth', 'Johanssen')]
"""

DATA = [
    ('Mark', 'Watney', 41),
    ('Melissa', 'Lewis', 40),
    ('Rick', 'Martinez', 39),
    ('Alex', 'Vogel', 40),
    ('Chris', 'Beck', 36),
    ('Beth', 'Johanssen', 29),
    ('Pan', 'Twardowski', 529),
    ('Ivan', 'Ivanovich', 1),
]

# Define `result: list[tuple]` with first names and last names
# of people whose age is in the range 18 to 65
# Use unpack syntax in a for loop
# type: list[str]
result = ...


"""
* Assignment: Loop Unpack Endswith
* Complexity: easy
* Lines of code: 4 lines
* Time: 5 min

English:
    1. Use `DATA: list[tuple]`
    2. Define `result: list` with:
       email addresses from `DATA` with domain names listed in `DOMAINS`
    3. Use unpack syntax in a for loop
    4. Run doctests - all must succeed

Polish:
    1. Użyj `DATA: list[tuple]`
    2. Zdefiniuj `result: list` z:
       adresami email z `DATA` z domenami wymienionymi w `DOMAINS`
    3. Użyj składni rozpakowania w pętli for
    4. Uruchom doctesty - wszystkie muszą się powieść

Why:
    * Check if you can filter data
    * Check if you know string methods
    * Check if you know how to iterate over list[dict]

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'

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

DATA = [
    ('Mark', 'Watney', 'mwatney@nasa.gov'),
    ('Melissa', 'Lewis', 'mlewis@nasa.gov'),
    ('Rick', 'Martinez', 'rmartinez@nasa.gov'),
    ('Alex', 'Vogel', 'avogel@esa.int'),
    ('Chris', 'Beck', 'cbeck@nasa.gov'),
    ('Beth', 'Johanssen', 'bjohanssen@nasa.gov'),
    ('Pan', 'Twardowski', 'ptwardowski@polsa.gov.pl'),
    ('Ivan', 'Ivanovich', 'iivanovich@roscosmos.ru'),
]

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

# Define `result: list` with:
# email addresses from `DATA` with domain names listed in `DOMAINS`
# Use unpack syntax in a for loop
# type: list[str]
result = ...