# %% About
# - Name: Generator Function Passwd
# - Difficulty: medium
# - Lines: 10
# - Minutes: 5
# %% 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 and then by colon `:`
# 2. Extract system accounts (users with UID [third field] is less than 1000)
# 3. Return list of system account logins
# 4. Implement solution using function
# 5. Implement solution using generator and `yield` keyword
# 6. Run doctests - all must succeed
# %% Polish
# 1. Podziel `DATA` po liniach a następnie po dwukropku `:`
# 2. Wyciągnij konta systemowe (użytkownicy z UID (trzecie pole) mniejszym niż 1000)
# 3. Zwróć listę loginów użytkowników systemowych
# 4. Zaimplementuj rozwiązanie wykorzystując funkcję
# 5. Zaimplementuj rozwiązanie wykorzystując generator i słowo kluczowe `yield`
# 6. Uruchom doctesty - wszystkie muszą się powieść
# %% Expected
# >>> list(function(DATA))
# ['root', 'daemon', 'bin', 'sys']
#
# >>> list(generator(DATA))
# ['root', 'daemon', 'bin', 'sys']
# %% Hints
# - `str.splitlines()`
# - `str.split()`
# - unpacking expression
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'
>>> from inspect import isfunction, isgeneratorfunction
>>> assert isfunction(function)
>>> assert isgeneratorfunction(generator)
>>> list(function(DATA))
['root', 'daemon', 'bin', 'sys']
>>> list(generator(DATA))
['root', 'daemon', 'bin', 'sys']
"""
# %% 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
from typing import Callable, Generator
function: Callable[[str], list[str]]
generator: Callable[[str], Generator[str, None, None]]
# %% Data
DATA = """# File: /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
alice:x:1000:1000:Alice:/home/alice:/bin/bash
bob:x:1001:1001:Bob:/home/bob:/bin/bash
carol:x:1002:1002:Carol:/home/carol:/bin/bash
dave:x:1003:1003:Dave:/home/dave:/bin/bash
eve:x:1004:1004:Eve:/home/eve:/bin/bash
mallory:x:1005:1005:Mallory:/home/mallory:/bin/bash
"""
# %% Result
def function(data: str):
...
def generator(data: str):
...