5.5. String Recap
5.5.1. Assignments
# %% About
# - Name: Type Recap Splitlines
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2
# %% 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. Split `DATA` by lines
# 2. Run doctests - all must succeed
# %% Polish
# 1. Podziel `DATA` po liniach
# 2. Uruchom doctesty - wszystkie muszą się powieść
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> assert result is not Ellipsis, \
'Assign your result to variable `result`'
>>> assert len(result) == 3, \
'Variable `result` length should be 3'
>>> assert type(result) is list, \
'Variable `result` has invalid type, should be list'
>>> line = 'We choose to go to the Moon'
>>> assert line in result, f'Line "{line}" is not in the result'
>>> line = 'in this decade and do the other things.'
>>> assert line in result, f'Line "{line}" is not in the result'
>>> line = 'Not because they are easy, but because they are hard.'
>>> assert line in result, f'Line "{line}" is not in the result'
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`
# %% Imports
# %% Types
result: list[str]
# %% Data
DATA = """We choose to go to the Moon
in this decade and do the other things.
Not because they are easy, but because they are hard."""
# %% Result
result = ...
# %% About
# - Name: Type Recap Join
# - Difficulty: easy
# - Lines: 1
# - Minutes: 3
# %% 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. Join lines of text with newline (`\n`) character
# 2. Run doctests - all must succeed
# %% Polish
# 1. Połącz linie tekstu znakiem końca linii (`\n`)
# 2. Uruchom doctesty - wszystkie muszą się powieść
# %% Hints
# - `str.join()`
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> assert result is not Ellipsis, \
'Assign your result to variable `result`'
>>> assert type(result) is str, \
'Variable `result` has invalid type, should be str'
>>> assert result.count('\\n') == 2, \
'There should be only two newline characters in result'
>>> line = 'We choose to go to the Moon'
>>> assert line in result, f'Line "{line}" is not in the result'
>>> line = 'in this decade and do the other things.'
>>> assert line in result, f'Line "{line}" is not in the result'
>>> line = 'Not because they are easy, but because they are hard.'
>>> assert line in result, f'Line "{line}" is not in the result'
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`
# %% Imports
# %% Types
result: str
# %% Data
DATA = [
'We choose to go to the Moon',
'in this decade and do the other things.',
'Not because they are easy, but because they are hard.',
]
# %% Result
result = ...
# %% About
# - Name: Type Recap Normalization
# - Difficulty: easy
# - Lines: 8
# - 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. Use `str` methods to clean `DATA`
# Expected: 'Pana Twardowskiego III'
# 2. Run doctests - all must succeed
# %% Polish
# 1. Wykorzystaj metody `str` do oczyszczenia `DATA`
# Oczekiwane: 'Pana Twardowskiego III'
# 2. Uruchom doctesty - wszystkie muszą się powieść
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> assert example is not Ellipsis, \
'Assign your result to variable `example`'
>>> assert result_a is not Ellipsis, \
'Assign your result to variable `a`'
>>> assert result_b is not Ellipsis, \
'Assign your result to variable `b`'
>>> assert result_c is not Ellipsis, \
'Assign your result to variable `c`'
>>> assert result_d is not Ellipsis, \
'Assign your result to variable `d`'
>>> assert result_e is not Ellipsis, \
'Assign your result to variable `e`'
>>> assert result_f is not Ellipsis, \
'Assign your result to variable `f`'
>>> assert result_g is not Ellipsis, \
'Assign your result to variable `g`'
>>> assert type(example) is str, \
'Variable `example` has invalid type, should be str'
>>> assert type(result_a) is str, \
'Variable `result_a` has invalid type, should be str'
>>> assert type(result_b) is str, \
'Variable `result_b` has invalid type, should be str'
>>> assert type(result_c) is str, \
'Variable `result_c` has invalid type, should be str'
>>> assert type(result_d) is str, \
'Variable `result_d` has invalid type, should be str'
>>> assert type(result_e) is str, \
'Variable `result_e` has invalid type, should be str'
>>> assert type(result_f) is str, \
'Variable `result_f` has invalid type, should be str'
>>> assert type(result_g) is str, \
'Variable `result_g` has invalid type, should be str'
>>> example
'Pana Twardowskiego III'
>>> result_a
'Pana Twardowskiego III'
>>> result_b
'Pana Twardowskiego III'
>>> result_c
'Pana Twardowskiego III'
>>> result_d
'Pana Twardowskiego III'
>>> result_e
'Pana Twardowskiego III'
>>> result_f
'Pana Twardowskiego III'
>>> result_g
'Pana Twardowskiego III'
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`
# %% Imports
# %% Types
result_a: str
result_b: str
result_c: str
result_d: str
result_e: str
result_f: str
result_g: str
# %% Data
EXAMPLE = 'UL. Pana \tTWArdoWskIEGO 3'
A = 'ulica Pana Twardowskiego III'
B = 'ul Pana Twardowskiego III'
C = 'ul. Pana Twardowskiego III'
D = 'Pana Twardowskiego 3'
E = ' Pana Twardowskiego III\t'
F = 'Pana\t Twardowskiego III'
G = 'Pana Twardowskiego III\n'
example = (
EXAMPLE
.upper()
.replace('UL. ', '')
.replace('\t', '')
.strip()
.title()
.replace('3', 'III')
)
# %% Result
result_a = ...
result_b = ...
result_c = ...
result_d = ...
result_e = ...
result_f = ...
result_g = ...