9.4. Unpack Recap

9.4.1. Assignments

# %% About
# - Name: Unpack Recap Train/Test
# - Difficulty: easy
# - Lines: 3
# - Minutes: 8

# %% 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. Separate header from rows
# 2. Define `train` with first 75% of rows
# 3. Define `test` with last 25% of rows
# 4. Use slice, i.e.: `list[start:stop:step]`
# 5. Run doctests - all must succeed

# %% Polish
# 1. Oddziel nagłówek od wierszy
# 2. Zdefiniuj `train` z pierwszymi 75% wierszami
# 3. Zdefiniuj `test` z ostatnimi 25% wierszami
# 4. Użyj slice, tj. `list[start:stop:step]`
# 5. Uruchom doctesty - wszystkie muszą się powieść

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> from pprint import pprint

>>> assert split is not Ellipsis, \
'Assign your result to variable `split`'
>>> assert train is not Ellipsis, \
'Assign your result to variable `train`'
>>> assert test is not Ellipsis, \
'Assign your result to variable `test`'
>>> assert type(split) is int, \
'Variable `split` has invalid type, should be int'
>>> assert type(train) is list, \
'Variable `train` has invalid type, should be list'
>>> assert type(train) is list, \
'Variable `train` has invalid type, should be list'
>>> assert type(test) is list, \
'Variable `test` has invalid type, should be list'
>>> assert all(type(x) is tuple for x in train), \
'All elements in `train` should be tuple'
>>> assert all(type(x) is tuple for x in test), \
'All elements in `test` should be tuple'

>>> pprint(split)
4

>>> pprint(train, width=30)
[('Mark', 'Watney', 41),
 ('Melissa', 'Lewis', 40),
 ('Rick', 'Martinez', 39),
 ('Alex', 'Vogel', 40)]

>>> pprint(test, width=30)
[('Chris', 'Beck', 36),
 ('Beth', 'Johanssen', 29)]
"""

# %% 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
train: list[tuple[str,str,int]]
test: list[tuple[str,str,int]]

# %% Data
RATIO = 0.75  # 75%

DATA = [
    ('firstname', 'lastname', 'age'),
    ('Mark', 'Watney', 41),
    ('Melissa', 'Lewis', 40),
    ('Rick', 'Martinez', 39),
    ('Alex', 'Vogel', 40),
    ('Chris', 'Beck', 36),
    ('Beth', 'Johanssen', 29),
]

header = DATA[0]
rows = DATA[1:]

# %% Result
train = ...
test = ...