Files
SolutionEuler/main.py

89 lines
2.5 KiB
Python

import runpy
from pathlib import Path
import typer
app = typer.Typer()
@app.command("add", help="Add a new problem to the projecteuler repository.")
def add_newproblem(num: int, name: str | None = None) -> None:
"""Add a new problem to the projecteuler repository."""
typer.echo(f"Adding problem {num} to the projecteuler repository.")
if name:
title = f"{num:04d}.{name}"
else:
title = f"{num:04d}"
Path(f"solutions/{title}").mkdir(parents=True, exist_ok=True)
file_name = f"solutions/{title}/euler_{num}.py"
Path(file_name).touch()
with open(file_name, "w") as f:
f.write("""'''
'''
import time
def timer(func):
def wrapper(*args, **kwargs):
start_time = time.time()
result = func(*args, **kwargs)
end_time = time.time()
elapsed_time = end_time - start_time
print(f"{func.__name__} time: {elapsed_time:.6f} seconds")
return result
return wrapper
""")
@app.command("list", help="List all problems in the projecteuler repository.")
def list_problems(every: bool = False):
"""List all problems in the projecteuler repository."""
if every:
typer.echo("Listing all problems in the projecteuler repository.")
else:
typer.echo("Listing near problems in the projecteuler repository.")
for path in Path("solutions").iterdir():
if path.is_dir():
if every:
tmp = list(path.iterdir())
tmp = all(ff.is_dir() for ff in tmp)
if tmp:
for ff in path.iterdir():
if ff.is_dir():
typer.echo(f"{ff.name}")
else:
typer.echo(f"{path.name}")
else:
if "_" in path.name:
typer.echo(f"{path.name} (completed)")
else:
typer.echo(f"{path.name}")
@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__"
)
@app.command("version", help="Display the version of the projecteuler CLI.")
def version():
"""Display the version of the projecteuler CLI."""
typer.echo("projecteuler solution version 0.1.0")
def main():
app()
if __name__ == "__main__":
main()