/projeuler.py

code solutions for Project Euler problems

Primary LanguagePythonMIT LicenseMIT

projeuler.py

Check

GitHub License GitHub top language GitHub repo file count (file extension)

Code solutions for Project Euler problems

Usage

$ python3 projeuler.py [COMMAND] [OPTIONS ...] [PROBLEMS ...]

Command list

Show problems solved, show full problem content with argument -f or --full.

Command run

Run specified problem solutions. Check answer, if a non-none value is given, with argument -c or --check.

Command create

Create a new problem solution template.

Solution structure

Each problem solution is a python module in problems directory, and filename MUST BE format like pXXXX.py. Module level variable PID is legacy used to identify the problem, and no longer used.

Solver method functions MUST BE defined at less one, whose function name is solve or starts with solve_. All solver method will be run to evaluate, and at least one solver method function returns correct answer in timeout limit makes the framework treats this problem is solved correctly.

If ANSWER is None or not presented, the solution will not be checked.

#!/usr/bin/env python3
# coding: utf-8


ANSWER = 42


def solve():
    return 42


def solve_method_1():
    return 42


def solve_method_2():
    return 42

The following module level configure variables are supported:

  • PID: Problem ID, legacy used to identify the problem, and no longer used.
  • ANSWER: Answer of the problem, optional.
  • TIMEOUT_EXT: Extra timeout (in milliseconds) for the problem, optional.

External data loading

In some problems, like problem 22, external data is required to solve the problem. The framework provides an automatic way to load external data, with the following steps:

  1. Download external data from Project Euler website, and save it to data directory.
  2. Write data loading module, store in data directory, with the filename exactly the same as the filename of problem solution file. A method load() MUST BE implemented in the module, and return the load data. See example in data of problem 22 and data loader of problem 22.
    #!/usr/bin/env python3
    # coding: utf-8
    # data/example.py
    
    
    def load():
        result = []
        with open("data/example.txt", "r") as fd:
            for line in fd:
                result.append(int(line))
    
        return result
  3. In the solution module of problem, import the data loading module, and call data.load() method to load data. See example in solution of problem 22.
    #!/usr/bin/env python3
    # coding: utf-8
    # problems/example.py
    
    
    from data import load
    
    
    ANSWER = 42
    
    
    def solve():
        data = load()
        return sum(data)