7.6. Mapping Recap
7.6.1. Assignments
# %% About
# - Name: Mapping Dict Switch
# - Difficulty: medium
# - Lines: 2
# - 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. Use `DATA: dict` - with pilot's phonetic alphabet
# 2. Ask user to input letter
# 3. Define `result: str` with phonetic pronunciation of a letter
# 4. If the character does not exist in the alphabet, assign: 'Missing'
# 5. Non-functional requirements:
# - User will always put only one capitalized letter
# - User will not input any invalid characters or numbers
# - Do not use `if`, `try`, and `except`
# - `Mock` will simulate inputting `W` letter by a user
# - Use `input()` function as normal
# 6. Run doctests - all must succeed
# %% Polish
# 1. Użyj `DATA: dict` - alfabet fonetyczny pilotów
# 2. Poproś użytkownika o wprowadzenie litery
# 3. Zdefiniuj `result: str` z fonetyczną wymową litery
# 4. Jeżeli znak nie istnieje w alfabecie, przypisz: 'Missing'
# 5. Wymagania niefunkcjonalne:
# - Użytkownik zawsze wpisze tylko jedną wielką literę lub cyfrę
# - Nie używaj `if`, `try`, i `except`
# - `Mock` zasymuluje wpisanie litery `W` przez użytkownika
# - Skorzystaj z funkcji `input()` tak jak normalnie
# 6. Uruchom doctesty - wszystkie muszą się powieść
# %% Hints
# - `input(prompt)`
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> from pprint import pprint
>>> import string
>>> assert letter is not Ellipsis, \
'Ask user to input letter and assign it to: `letter`'
>>> assert result is not Ellipsis, \
'Assign your result to variable `result`'
>>> assert type(result) is str, \
'Variable `result` has invalid type, should be str'
>>> pprint(result)
'Whisky'
"""
# %% 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
from unittest.mock import Mock
# %% Types
result: str
# %% Data
DATA = {
'A': 'Alfa',
'B': 'Bravo',
'C': 'Charlie',
'D': 'Delta',
'E': 'Echo',
'F': 'Foxtrot',
'G': 'Golf',
'H': 'Hotel',
'I': 'India',
'J': 'Juliet',
'K': 'Kilo',
'L': 'Lima',
'M': 'Mike',
'N': 'November',
'O': 'Oscar',
'P': 'Papa',
'Q': 'Quebec',
'R': 'Romeo',
'S': 'Sierra',
'T': 'Tango',
'U': 'Uniform',
'V': 'Victor',
'W': 'Whisky',
'X': 'X-Ray',
'Z': 'Zulu',
}
input = Mock(side_effect=['W'])
letter = input('Type letter: ')
# %% Result
result = ...
# %% About
# - Name: Mapping Dict SwitchMatches
# - Difficulty: medium
# - Lines: 2
# - 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. Use `PL: dict` with Polish diacritic characters
# 2. Ask user to input single letter
# 3. Convert letter to lowercase
# 4. If letter is in `PL` then use conversion value for the letter
# 5. Non-functional requirements:
# - User will always put only one capitalized letter
# - User will not input any invalid characters or numbers
# - Do not use `if`, `try`, and `except`
# - Use `input()` function as normal
# - `Mock` will simulate inputting `Ó` letter by a user
# - Note that letter `Ó` is in `PL`
# 6. Run doctests - all must succeed
# %% Polish
# 1. Użyj `PL: dict` z polskimi znakami diakrytycznymi
# 2. Poproś użytkownika o wprowadzenie pojedynczej litery
# 3. Zamień literę na małą
# 4. Jeżeli litera jest w `PL` to użyj wartości konwersji dla litery
# 5. Wymagania niefunkcjonalne:
# - Użytkownik zawsze wpisze tylko jedną wielką literę
# - Użytkownik nie wpisze żadnych nieprawidłowych znaków lub cyfr
# - Nie używaj `if`, `try`, i `except`
# - Skorzystaj z funkcji `input()` tak jak normalnie
# - `Mock` zasymuluje wpisanie litery `Ó` przez użytkownika
# - Zwróć uwagę, że litera `Ó` jest w `PL`
# 6. Uruchom doctesty - wszystkie muszą się powieść
# %% Example
# | Input | Output |
# |-------|--------|
# | a | a |
# | A | a |
# | ą | a |
# | Ą | a |
# | b | b |
# | B | B |
# | c | c |
# | C | c |
# | ć | c |
# | Ć | c |
# | ... | ... |
# %% Hints
# - `input()`
# - `str.lower()`
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> from pprint import pprint
>>> import string
>>> assert letter is not Ellipsis, \
'Ask user to input letter and assign it to: `letter`'
>>> 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 not in PL.keys(), \
'Result should not be in PL dict'
>>> assert result in string.ascii_letters, \
'Result should be an ASCII letter'
>>> pprint(result)
'o'
"""
# %% 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
from unittest.mock import Mock
# %% Types
result: str
# %% Data
PL = {
'ą': 'a',
'ć': 'c',
'ę': 'e',
'ł': 'l',
'ń': 'n',
'ó': 'o',
'ś': 's',
'ż': 'z',
'ź': 'z',
}
input = Mock(side_effect=['Ó'])
letter = input('Type letter: ').lower()
# %% Result
result = ...
# %% About
# - Name: Mapping Dict SwitchMissing
# - Difficulty: medium
# - Lines: 2
# - 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. Use `PL: dict` with Polish diacritic characters
# 2. Ask user to input single letter
# 3. Convert letter to lowercase
# 4. If letter is in `PL` then use conversion value for the letter
# 5. Non-functional requirements:
# - User will always put only one capitalized letter
# - User will not input any invalid characters or numbers
# - Do not use `if`, `try`, and `except`
# - Use `input()` function as normal
# - `Mock` will simulate inputting `O` letter by a user
# - Note that letter `O` is not in `PL`
# 6. Run doctests - all must succeed
# %% Polish
# 1. Użyj `PL: dict` z polskimi znakami diakrytycznymi
# 2. Poproś użytkownika o wprowadzenie pojedynczej litery
# 3. Zamień literę na małą
# 4. Jeżeli litera jest w `PL` to użyj wartości konwersji dla litery
# 5. Wymagania niefunkcjonalne:
# - Użytkownik zawsze wpisze tylko jedną wielką literę
# - Użytkownik nie wpisze żadnych nieprawidłowych znaków lub cyfr
# - Nie używaj `if`, `try`, i `except`
# - Skorzystaj z funkcji `input()` tak jak normalnie
# - `Mock` zasymuluje wpisanie litery `O` przez użytkownika
# - Zwróć uwagę, że litera `O` nie jest w `PL`
# 6. Uruchom doctesty - wszystkie muszą się powieść
# %% Example
# | Input | Output |
# |-------|--------|
# | a | a |
# | A | a |
# | ą | a |
# | Ą | a |
# | b | b |
# | B | B |
# | c | c |
# | C | c |
# | ć | c |
# | Ć | c |
# | ... | ... |
# %% Hints
# - `input()`
# - `str.lower()`
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> from pprint import pprint
>>> import string
>>> assert letter is not Ellipsis, \
'Ask user to input letter and assign it to: `letter`'
>>> 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 not in PL.keys(), \
'Result should not be in PL dict'
>>> assert result in string.ascii_letters, \
'Result should be an ASCII letter'
>>> pprint(result)
'o'
"""
# %% 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
from unittest.mock import Mock
# %% Types
result: str
# %% Data
PL = {
'ą': 'a',
'ć': 'c',
'ę': 'e',
'ł': 'l',
'ń': 'n',
'ó': 'o',
'ś': 's',
'ż': 'z',
'ź': 'z',
}
input = Mock(side_effect=['O'])
letter = input('Type letter: ').lower()
# %% Result
result = ...