6.9. String Recap

6.9.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ść

# %% Expected
# >>> result
# ['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.']

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0

>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'

>>> assert 'result' in globals(), \
'Variable `result` is not defined; assign result of your program to it.'

>>> assert result is not Ellipsis, \
'Variable `result` has an invalid value; assign result of your program to it.'

>>> assert len(result) == 3, \
'Variable `result` has an invalid length; expected: `3`.'

>>> assert type(result) is list, \
'Variable `result` has an invalid type; expected: `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ść

# %% Expected
# >>> result
# 'We choose to go to the Moon\nin this decade and do the other things.\nNot because they are easy, but because they are hard.'

# %% Hints
# - `str.join()`

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0

>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'

>>> assert 'result' in globals(), \
'Variable `result` is not defined; assign result of your program to it.'

>>> assert result is not Ellipsis, \
'Variable `result` has an invalid value; assign result of your program to it.'

>>> assert type(result) is str, \
'Variable `result` has an invalid type; expected: `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 Str Normalize
# - Difficulty: easy
# - Lines: 4
# - 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. 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ść

# %% Expected
# >>> result
# 'Pana Twardowskiego III'

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0

>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'

>>> from pprint import pprint

>>> assert 'result' in globals(), \
'Variable `result` is not defined; assign result of your program to it.'

>>> assert result is not Ellipsis, \
'Variable `result` has an invalid value; assign result of your program to it.'

>>> assert type(result) is str, \
'Variable `result` has an invalid type; expected: `str`.'

>>> pprint(result)
'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: str

# %% Data
DATA = 'UL. pana \tTWArdoWskIEGO 3'

# %% 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ść

# %% Expected
# >>> result1
# 'Pana Twardowskiego III'
#
# >>> result2
# 'Pana Twardowskiego III'
#
# >>> result3
# 'Pana Twardowskiego III'
#
# >>> result4
# 'Pana Twardowskiego III'
#
# >>> result5
# 'Pana Twardowskiego III'
#
# >>> result6
# 'Pana Twardowskiego III'
#
# >>> result7
# 'Pana Twardowskiego III'

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0

>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'

>>> assert example is not Ellipsis, \
'Variable `example` has an invalid value; assign result of your program to it.'

>>> assert 'result1' in globals(), \
'Variable `result1` is not defined; assign result of your program to it.'

>>> assert result1 is not Ellipsis, \
'Variable `result1` has an invalid value; assign result of your program to it.'

>>> assert 'result2' in globals(), \
'Variable `result2` is not defined; assign result of your program to it.'

>>> assert result2 is not Ellipsis, \
'Variable `result2` has an invalid value; assign result of your program to it.'

>>> assert 'result3' in globals(), \
'Variable `result3` is not defined; assign result of your program to it.'

>>> assert result3 is not Ellipsis, \
'Variable `result3` has an invalid value; assign result of your program to it.'

>>> assert 'result4' in globals(), \
'Variable `result4` is not defined; assign result of your program to it.'

>>> assert result4 is not Ellipsis, \
'Variable `result4` has an invalid value; assign result of your program to it.'

>>> assert 'result5' in globals(), \
'Variable `result5` is not defined; assign result of your program to it.'

>>> assert result5 is not Ellipsis, \
'Variable `result5` has an invalid value; assign result of your program to it.'

>>> assert 'result6' in globals(), \
'Variable `result6` is not defined; assign result of your program to it.'

>>> assert result6 is not Ellipsis, \
'Variable `result6` has an invalid value; assign result of your program to it.'

>>> assert 'result7' in globals(), \
'Variable `result7` is not defined; assign result of your program to it.'

>>> assert result7 is not Ellipsis, \
'Variable `result7` has an invalid value; assign result of your program to it.'

>>> assert type(example) is str, \
'Variable `example` has an invalid type; expected: `str`.'

>>> assert type(result1) is str, \
'Variable `result1` has an invalid type; expected: `str`.'

>>> assert type(result2) is str, \
'Variable `result2` has an invalid type; expected: `str`.'

>>> assert type(result3) is str, \
'Variable `result3` has an invalid type; expected: `str`.'

>>> assert type(result4) is str, \
'Variable `result4` has an invalid type; expected: `str`.'

>>> assert type(result5) is str, \
'Variable `result5` has an invalid type; expected: `str`.'

>>> assert type(result6) is str, \
'Variable `result6` has an invalid type; expected: `str`.'

>>> assert type(result7) is str, \
'Variable `result7` has an invalid type; expected: `str`.'

>>> example
'Pana Twardowskiego III'

>>> result1
'Pana Twardowskiego III'

>>> result2
'Pana Twardowskiego III'

>>> result3
'Pana Twardowskiego III'

>>> result4
'Pana Twardowskiego III'

>>> result5
'Pana Twardowskiego III'

>>> result6
'Pana Twardowskiego III'

>>> result7
'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
result1: str
result2: str
result3: str
result4: str
result5: str
result6: str
result7: str

# %% Data
EXAMPLE = 'UL. Pana \tTWArdoWskIEGO 3'

DATA1 = 'ulica Pana Twardowskiego III'
DATA2 = 'ul Pana Twardowskiego III'
DATA3 = 'ul. Pana Twardowskiego III'
DATA4 = 'Pana Twardowskiego 3'
DATA5 = ' Pana Twardowskiego III\t'
DATA6 = 'Pana\t Twardowskiego III'
DATA7 = 'Pana Twardowskiego III\n'

example = (
    EXAMPLE
    .upper()
    .replace('UL. ', '')
    .replace('\t', '')
    .strip()
    .title()
    .replace('3', 'III')
)

# %% Result
result1 = ...
result2 = ...
result3 = ...
result4 = ...
result5 = ...
result6 = ...
result7 = ...