EasyPython

Extract Function

PythonClean CodeFunctionsRefactoring

The function below does too many things at once — it validates, transforms and collects all in one loop.

# dirty
def solve(users):
    result = []
    for u in users:
        if u['age'] >= 18 and '@' in u['email'] and u['name'].strip() != '':
            result.append({'name': u['name'].strip(), 'email': u['email'].lower(), 'adult': True})
    return result

Refactor `solve(users)` by extracting helper functions so each function does exactly one thing. The output must be identical.

Your extracted version should have at minimum: is_valid(user), format_user(user), and the main solve(users) that composes them.

Sample tests

Test #1Valid adult user passes through
Input: [[{"age":25,"name":"Alice","email":"alice@example.com"}]]
Output: [{"name":"Alice","adult":true,"email":"alice@example.com"}]
Test #2Name is stripped and email is lowercased
Input: [[{"age":30,"name":" Bob ","email":"BOB@EXAMPLE.COM"}]]
Output: [{"name":"Bob","adult":true,"email":"bob@example.com"}]
Test #3Under 18 is filtered out
Input: [[{"age":16,"name":"Teen","email":"teen@x.com"}]]
Output: []