11.4. FP Apply Composition
11.4.1. Assignments
# %% 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
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: Functional Composition Sum
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2
# %% English
# 1. Define `result: int` with a sum of elements in `DATA`
# 2. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj `result: int` z sumą elementów w `DATA`
# 2. Uruchom doctesty - wszystkie muszą się powieść
# %% Hints
# - `filter()`
# - `sum()`
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> from inspect import isfunction
>>> assert isfunction(even), \
'Object `even` must be a function'
>>> assert result is not Ellipsis, \
'Assign result to variable: `result`'
>>> assert type(result) is int, \
'Variable `result` has invalid type, should be int'
>>> result
6
"""
def even(x):
return x % 2 == 0
NUMBERS = (1, 2, 3, 4)
DATA = filter(even, NUMBERS)
# Define `result: int` with a sum of elements in `DATA`
# type: int
result = ...
# %% 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
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: Functional Composition Len
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2
# %% English
# 1. Define `result: int` with a number of elements in `DATA`
# 2. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj `result: int` ze liczbą elementów w `DATA`
# 2. Uruchom doctesty - wszystkie muszą się powieść
# %% Hints
# - `tuple()`
# - `len()`
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> from inspect import isfunction
>>> assert isfunction(even), \
'Object `even` must be a function'
>>> assert result is not Ellipsis, \
'Assign result to variable: `result`'
>>> assert type(result) is int, \
'Variable `result` has invalid type, should be int'
>>> result
2
"""
def even(x):
return x % 2 == 0
NUMBERS = (1, 2, 3, 4)
DATA = filter(even, NUMBERS)
# Define `result: int` with a number of elements in `DATA`
# type: int
result = ...
# %% 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
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: Functional Composition Mean
# - Difficulty: easy
# - Lines: 2
# - Minutes: 2
# %% English
# 1. Define `result: int` with a arithmetic mean of elements in `DATA`
# 2. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj `result: int` ze średnią arytmetyczną elementów w `DATA`
# 2. Uruchom doctesty - wszystkie muszą się powieść
# %% Hints
# - `tuple()`
# - `sum() / len()`
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> from inspect import isfunction
>>> assert isfunction(even), \
'Object `even` must be a function'
>>> assert result is not Ellipsis, \
'Assign result to variable: `result`'
>>> assert type(result) is float, \
'Variable `result` has invalid type, should be float'
>>> result
3.0
"""
def even(x):
return x % 2 == 0
NUMBERS = (1, 2, 3, 4)
DATA = filter(even, NUMBERS)
# Define `result: int` with a arithmetic mean of elements in `DATA`
# type: float
result = ...
# %% 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
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: Functional Composition FilterMap
# - Difficulty: easy
# - Lines: 3
# - Minutes: 3
# %% English
# 1. Define `result: list[dict]`, where each dict has keys:
# - ip: str
# - hosts: list[str]
# 2. Skip comments (`#`) and empty lines
# 3. Extract from each line: `ip` and `hosts`
# 4. Add `ip` and `hosts` to `result` as a dict
# example: {'ip': '127.0.0.1', 'hosts': ['localhost', 'astromatt']}
# 5. Each line must be a separate dict
# 4. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj `result: list[dict]`, gdzie każdy dict ma klucze:
# - ip: str
# - hosts: list[str]
# 2. Pomiń komentarze (`#`) i puste linie
# 3. Wyciągnij z każdej linii: `ip` i `hosts`
# 4. Dodaj `ip` i `hosts` do `result` jako słownik
# przykład: {'ip': '127.0.0.1', 'hosts': ['localhost', 'astromatt']}
# 5. Każda linia ma być osobnym słownikiem
# 4. Uruchom doctesty - wszystkie muszą się powieść
# %% Hints
# - `filter()`
# - `map()`
# - `len()`
# - `str.split()`
# - `str.startswith()`
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> from inspect import isfunction
>>> assert isfunction(is_valid), \
'Object `is_valid` must be a function'
>>> assert result is not Ellipsis, \
'Assign result to variable: `result`'
>>> result = list(result)
>>> assert type(result) is list, \
'Evaluated `result` has invalid type, should be list'
>>> assert all(type(x) is dict for x in result), \
'All rows in `result` should be dict'
>>> assert all(type(key) is str
... for row in result
... for key in row.keys()), \
'All rows in `result` should be dict'
>>> list(result) # doctest: +NORMALIZE_WHITESPACE
[{'ip': '127.0.0.1', 'hosts': ['localhost', 'astromatt']},
{'ip': '10.13.37.1', 'hosts': ['nasa.gov', 'esa.int']},
{'ip': '255.255.255.255', 'hosts': ['broadcasthost']},
{'ip': '::1', 'hosts': ['localhost']}]
"""
DATA = """##
# `/etc/hosts` structure:
# - ip: internet protocol address (IPv4 or IPv6)
# - hosts: host names
##
127.0.0.1 localhost astromatt
10.13.37.1 nasa.gov esa.int
255.255.255.255 broadcasthost
::1 localhost"""
def is_valid(line):
if len(line) == 0:
return False
if line.startswith('#'):
return False
return True
def transform(line):
ip, *hosts = line.split()
return {'ip': ip, 'hosts': hosts}
# Define `result: list[dict]`, where each dict has keys:
# - ip: str
# - hosts: list[str]
# Skip comments (`#`) and empty lines
# Extract from each line: `ip` and `hosts`
# Add `ip` and `hosts` to `result` as a dict
# example: {'ip': '127.0.0.1', 'hosts': ['localhost', 'astromatt']}
# Each line must be a separate dict
# type: list[dict]
result = ...
# %% 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
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: Functional Composition FilterMap
# - Difficulty: easy
# - Lines: 4
# - Minutes: 3
# %% English
# 0. Note, this assignment differs from previous by one character in `DATA`
# 1. Filter-out lines from `DATA` when:
# - line is empty
# - line has only spaces
# - starts with # (comment)
# 2. Use `filter()` to apply function `is_valid()` to DATA
# 3. Define `result: map` with result
# 4. Run doctests - all must succeed
# %% Polish
# 0. Zauważ, że to zadanie od poprzedniego różni się jednym znakiem w `DATA`
# 1. Odfiltruj linie z `DATA` gdy:
# - linia jest pusta
# - linia ma tylko spacje
# - zaczyna się od # (komentarz)
# 2. Użyj `filter()` aby zaaplikować funkcję `is_valid()` do DATA
# 3. Zdefiniuj `result: map` z wynikiem
# 4. Uruchom doctesty - wszystkie muszą się powieść
# %% Hints
# - `filter()`
# - `map()`
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> from inspect import isfunction
>>> assert isfunction(is_valid), \
'Object `is_valid` must be a function'
>>> assert isfunction(transform), \
'Object `transform` must be a function'
>>> assert result is not Ellipsis, \
'Assign result to variable: `result`'
>>> assert type(result) is map, \
'Variable `result` has invalid type, should be map'
>>> result = list(result)
>>> assert type(result) is list, \
'Evaluated `result` has invalid type, should be list'
>>> assert all(type(x) is dict for x in result), \
'All rows in `result` should be dict'
>>> list(result) # doctest: +NORMALIZE_WHITESPACE
[{'ip': '127.0.0.1', 'hosts': ['localhost']},
{'ip': '127.0.0.1', 'hosts': ['astromatt']},
{'ip': '10.13.37.1', 'hosts': ['nasa.gov', 'esa.int']},
{'ip': '255.255.255.255', 'hosts': ['broadcasthost']},
{'ip': '::1', 'hosts': ['localhost']}]
"""
DATA = """##
# `/etc/hosts` structure:
# - ip: internet protocol address (IPv4 or IPv6)
# - hosts: host names
##
127.0.0.1 localhost
127.0.0.1 astromatt
10.13.37.1 nasa.gov esa.int
255.255.255.255 broadcasthost
::1 localhost"""
def is_valid(line):
if len(line) == 0:
return False
if line.startswith('#'):
return False
return True
def transform(line):
ip, *hosts = line.split()
return {'ip': ip, 'hosts': hosts}
# Note, this assignment differs from previous by one character in `DATA`
# Filter-out lines from `DATA` when:
# - line is empty
# - line has only spaces
# - starts with # (comment)
# Use `filter()` to apply function `is_valid()` to DATA
# type: map
result = ...