Python 3.x | No third-party dependencies | Windows / macOS / Linux
Assignment-4/
│
├── README.md
├── Screenshots/ ← proof of working output
│ ├── Q1-Execute-Commands-1.png ← demo mode (--demo flag)
│ ├── Q1-Execute-Commands-2.png ← custom commands + duplicate skip
│ └── Q2-Euler-Problems.png ← both Euler answers
│
├── Q1-Execute-Commands/
│ ├── run_commands.py ← CLI entry point
│ └── command_runner/
│ ├── __init__.py
│ └── executor.py ← core execution logic
│
└── Q2-Euler-Problems/
├── run_all.py ← CLI entry point
└── euler_problems/
├── __init__.py
├── problem_065.py ← Convergents of e
└── problem_315.py ← Digital root clocks
Write a Python program that executes a given list of OS commands and returns the results as a list of dictionaries with the following rules:
- Duplicates — each command is run only once (order preserved)
- Failures — if a command fails, execution continues for remaining commands
- Output format — a JSON list of
{ command: { output, error, status } }dicts
| Module | Role |
|---|---|
command_runner/executor.py |
Runs each command via subprocess, captures stdout/stderr, handles timeouts and OS errors |
command_runner/__init__.py |
Package exports |
run_commands.py |
CLI — supports positional args, --file, --demo, and --timeout flags |
Navigate to the folder:
cd "c:\Users\rehan.farooque\Downloads\Assignment-4\Q1-Execute-Commands"Demo mode (built-in commands including a duplicate and a failing command):
python run_commands.py --demoPass commands directly:
python run_commands.py "echo hello" "python --version" "no-such-cmd"With custom timeout (seconds):
python run_commands.py --demo --timeout 10[
{
"python --version": {
"output": "Python 3.x.x",
"error": "",
"status": "success"
}
},
{
"echo Hello from the command runner": {
"output": "Hello from the command runner",
"error": "",
"status": "success"
}
},
{
"no-such-command-xyz": {
"output": "",
"error": "'no-such-command-xyz' is not recognized as an internal or external command,\noperable program or batch file.",
"status": "Failed"
}
}
]Note: The duplicate
echo Hello from the command runnerin the demo list is silently skipped — only one entry appears in the output.
Problem: The continued fraction expansion of e is:
e = [2; 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8, ...]
The convergents
Find the sum of digits in the numerator of the 100th convergent of e.
Answer: 272
Problem: Sam and Max each build a digital-root clock. When fed a number, the clock shows the number, then each digit-sum step, until a single digit remains, then goes blank.
Example: 137 → 11 → 2 → (blank)
7-segment display — each digit uses some of 7 segments (top, top-right, bottom-right, bottom, bottom-left, top-left, middle). Energy is consumed only when segments switch on or off.
| Clock | Behaviour | Cost |
|---|---|---|
| Sam's | Turns the entire panel OFF, then ON for each number | 2 × segments per number |
| Max's | Only toggles segments that actually change | XOR of consecutive segment states |
Worked example (n = 137):
| Step | Sam's cost | Max's cost |
|---|---|---|
137 on/off |
(2+5+4) × 2 = 22 |
11 (on) + 7 (→11 XOR) |
11 on/off |
(2+2) × 2 = 8 |
0 (already set) + 3 (→2 XOR) |
2 on/off |
5 × 2 = 10 |
4 (on) + 5 (off) |
| Total | 40 | 30 |
Difference for 137: 10
The program sums this difference over all primes between 10,000,000 and 20,000,000.
Answer: 13625242
| Module | Role |
|---|---|
euler_problems/problem_065.py |
Continued fraction recurrence for e, digit sum of numerator |
euler_problems/problem_315.py |
Sieve of Eratosthenes, 7-segment encoding, Sam vs Max cost calculation |
run_all.py |
CLI runner with timing for both problems |
Navigate to the folder:
cd "c:\Users\rehan.farooque\Downloads\Assignment-4\Q2-Euler-Problems"Run both problems:
python run_all.pyRun a single problem:
python run_all.py 65
python run_all.py 315--- Project Euler Problem 65 ---
Sum of digits in numerator of 100th convergent of e
Answer : 272
Elapsed: 0.00s
--- Project Euler Problem 315 ---
Sam - Max segment switches for primes in [10^7, 2*10^7)
Answer : 13625242
Elapsed: 5.72s
Note: Problem 315 sieves primes up to 20 million — expect ~5-10 seconds runtime.
Demo mode (python run_commands.py --demo) — shows python --version (success), echo (success), duplicate skipped, and no-such-command-xyz (Failed):
Custom commands (python run_commands.py "echo hello" "pwd" "echo hello" "no-such-cmd") — demonstrates duplicate skipping (echo hello appears once) and graceful failure handling:
Problem 65 (python run_all.py 65) — Answer: 272 (instantaneous)
Problem 315 (python run_all.py 315) — Answer: 13625242 (~5 seconds):
- Python 3.10+ (uses
bit_count()method andmatchpatterns) - No external packages required — standard library only (
subprocess,functools,argparse,json,time)


