feat(cli): 支持多重目录结构与特殊解法运行,新增解法列表选项
This commit is contained in:
65
main.py
65
main.py
@@ -62,16 +62,61 @@ def list_problems(every: bool = False):
|
||||
|
||||
|
||||
@app.command("solution", help="Run a solution for a given problem.")
|
||||
def run_solution(num: int) -> None:
|
||||
folders = list(Path("solutions").iterdir())
|
||||
folders = [
|
||||
folder
|
||||
for folder in folders
|
||||
if folder.is_dir() and folder.name.startswith(f"{num:04d}")
|
||||
]
|
||||
runpy.run_path(
|
||||
(folders[0] / f"euler_{num}.py").resolve().as_posix(), run_name="__main__"
|
||||
)
|
||||
def run_solution(
|
||||
num: int,
|
||||
special: None | str = None,
|
||||
list_solutions: bool = False,
|
||||
) -> None:
|
||||
# Find target folders that match the problem number
|
||||
target_folders = []
|
||||
for folder in Path("solutions").iterdir():
|
||||
if not folder.is_dir():
|
||||
continue
|
||||
# Check for exact match (e.g., "0001")
|
||||
if folder.name.startswith(f"{num:04d}"):
|
||||
target_folders.append(folder)
|
||||
break
|
||||
# Check for range folders (e.g., "0001_0050")
|
||||
if "_" in folder.name:
|
||||
try:
|
||||
start_str, end_str = folder.name.split("_")
|
||||
start_num = int(start_str)
|
||||
end_num = int(end_str)
|
||||
if start_num <= num <= end_num:
|
||||
for subfolder in folder.iterdir():
|
||||
if subfolder.is_dir() and subfolder.name.startswith(
|
||||
f"{num:04d}"
|
||||
):
|
||||
target_folders.append(subfolder)
|
||||
break
|
||||
except (ValueError, IndexError):
|
||||
continue
|
||||
|
||||
if not target_folders:
|
||||
typer.echo(f"No folder found for problem {num}")
|
||||
return
|
||||
|
||||
target_folder = target_folders[0]
|
||||
|
||||
if list_solutions:
|
||||
# List all Python files in the target folder
|
||||
for file in target_folder.iterdir():
|
||||
if file.is_file() and file.suffix == ".py":
|
||||
typer.echo(f"{file.name}")
|
||||
return
|
||||
|
||||
# Determine the filename to run
|
||||
filename = f"euler_{num}.py"
|
||||
if special:
|
||||
filename = f"euler_{num}_{special}.py"
|
||||
|
||||
file_path = target_folder / filename
|
||||
if not file_path.exists():
|
||||
typer.echo(f"Solution file not found: {file_path}")
|
||||
return
|
||||
|
||||
# Run the solution
|
||||
runpy.run_path(file_path.resolve().as_posix(), run_name="__main__")
|
||||
|
||||
|
||||
@app.command("version", help="Display the version of the projecteuler CLI.")
|
||||
|
||||
Reference in New Issue
Block a user