easy +8 pts

Safe Command Executor

Simulate a simple command runner safely without using subprocess or network.

Write a function `emulate_run(command: list) -> dict` that takes a list of strings representing a command (e.g. `['echo', 'hello']`) and simulates running it without using `subprocess`, `os.system`, or any network/process imports. The function simulates a subset of common commands: - If the first element is `'echo'`, join the remaining elements with spaces, append a newline, set `stdout` to that string, `stderr` to `''`, and `returncode` to `0`. - If the first element is `'python'` and the rest of the arguments contain exactly `-c` followed by a code string, simulate running Python code that only prints to stdout/stderr and exits with code 0 (unless it calls `exit(n)` at the end). The code may contain `print('...')` and `print('...', file=__import__('sys').stderr)` and `exit(n)`. The simulation captures all printed lines in order, with each print adding a newline. If `exit(n)` is called, the returncode becomes n (default 0 if no exit call). If the code has any other constructs (loops, conditionals, variable assignments, etc.), return `returncode: -1`, `stdout: ''`, and `stderr: 'Unsupported code'`. - If the first element is `'cat'` and there are no arguments, set `stdout` to `'hello\nworld\n'` and `returncode` to `0`. If there are arguments, return `returncode` to `1`, `stdout` to `''`, and `stderr` to `'cat: file not found'`. - For any other first element, return `{'returncode': -1, 'stdout': '', 'stderr': '[Errno 2] No such file or directory: "<first>"'}` where `<first>` is the first argument. The function must never raise an exception; it always returns a dictionary with keys `'returncode'` (int), `'stdout'` (str), and `'stderr'` (str).

Constraints

- `command` is a list of strings, non-empty. - Only simple `print` statements and `exit(n)` (with n an integer) are supported in the Python simulation. - The simulation is deterministic and does not require external resources.

Example

```python
>>> emulate_run(['echo', 'hello'])
{'returncode': 0, 'stdout': 'hello\n', 'stderr': ''}
>>> emulate_run(['nonexistent_command'])
{'returncode': -1, 'stdout': '', 'stderr': '[Errno 2] No such file or directory: \'nonexistent_command\''}
>>> emulate_run(['python', '-c', "print('out')"])
{'returncode': 0, 'stdout': 'out\n', 'stderr': ''}
>>> emulate_run(['python', '-c', "print('err', file=__import__('sys').stderr); exit(3)"])
{'returncode': 3, 'stdout': '', 'stderr': 'err\n'}
```
8 points ~10 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Start by examining command[0] to dispatch to specific handlers.
For echo, join the rest of the list with spaces and add a newline.
For python, check for '-c' and parse the code string; support only print and exit.
Handle unknown commands by returning error dictionary with str of first element.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.