<commit_before># -*- coding: utf-8 -*-

import os
import sys
import linecache
import subprocess

import click

from EulerPy.timing import clock, format_time

def get_filename(problem, width=3):
    """Returns filename padded with leading zeros"""
    return '{0:0{w}d}.py'.format(problem, w=width)


def get_solution(problem):
    """Returns the answer to a given problem"""
    solutionsFile = os.path.join(os.path.dirname(__file__), 'solutions.txt')
    line = linecache.getline(solutionsFile, problem)

    try:
        answer = line.split('. ')[1].strip()
    except IndexError:
        answer = None

    if answer:
        return answer
    else:
        msg = 'Answer for problem %i not found in solutions.txt.' % problem
        click.secho(msg, fg='red')
        click.echo('If you have an answer, consider submitting a pull request '
                   'to EulerPy at https://github.com/iKevinY/EulerPy.')
        sys.exit(1)


def get_problem(problem):
    """Parses problems.txt and returns problem text"""
    def problem_iter(problem, problemFile):
        with open(problemFile) as file:
            problemText = False
            lastLine = ''

            for line in file:
                if line.strip() == 'Problem %i' % problem:
                    problemText = True

                if problemText:
                    if line == lastLine == '\n':
                        break
                    else:
                        yield line[:-1]
                        lastLine = line

    problemFile = os.path.join(os.path.dirname(__file__), 'problems.txt')
    problemLines = [line for line in problem_iter(problem, problemFile)]

    if problemLines:
        # First three lines are the problem number, the divider line,
        # and a newline, so don't include them in the returned string
        return '\n'.join(problemLines[3:])
    else:
        msg = 'Problem %i not found in problems.txt.' % problem
        click.secho(msg, fg='red')
        click.echo('If this problem exists on Project Euler, consider '
                   'submitting a pull request to EulerPy at '
                   'https://github.com/iKevinY/EulerPy.')
        sys.exit(1)


def determine_largest_problem():
    """Determines largest problem file in the current directory"""
    # Arbitrary value that is larger than all Project Euler problems
    for problem in reversed(range(600)):
        if os.path.isfile(get_filename(problem)):
            return problem
    else:
        return False


# --cheat / -c
def cheat(problem):
    """View the answer to a problem."""
    # get_solution() will exit here if the solution does not exist
    solution = click.style(get_solution(problem), bold=True)
    click.confirm("View answer to problem %i?" % problem, abort=True)
    click.echo("The answer to problem %i is {0}.".format(solution) % problem)


# --generate / -g
def generate(problem, prompt_default=True):
    """Generates Python file for a problem."""

    msg = "Generate file for problem %i?" % problem
    click.confirm(msg, default=prompt_default, abort=True)
    problemText = get_problem(problem)

    filename = get_filename(problem)

    if os.path.isfile(filename):
        msg = '"{0}" already exists. Overwrite?'.format(filename)
        click.confirm(click.style(msg, fg='red'), abort=True)

    problemHeader = 'Project Euler Problem %i\n' % problem
    problemHeader += '=' * len(problemHeader.strip()) + '\n\n'

    with open(filename, 'w') as file:
        file.write('"""\n')
        file.write(problemHeader)
        file.write(problemText)
        file.write('"""\n\n\n')

    click.secho('Successfully created "{0}".'.format(filename), fg='green')


# --preview / -p
def preview(problem):
    """Prints the text of a problem."""
    # Define problemText instead of echoing it right away
    # in case the problem does not exist in problems.txt
    problemText = get_problem(problem)[:-1] # strip newline from text
    click.secho("Project Euler Problem %i" % problem, bold=True)
    click.echo(problemText)


# --skip / -s
def skip(problem):
    """Generates Python file for the next problem."""
    click.echo("Current problem is problem %i." % problem)
    generate(problem + 1, prompt_default=False)


# --verify / -v
def verify(problem):
    """Verifies the solution to a problem."""
    filename = get_filename(problem)

    if not os.path.isfile(filename):
        click.secho('Error: "{0}" not found.'.format(filename), fg='red')
        sys.exit(1)

    # get_solution() will exit here if the solution does not exist
    solution = get_solution(problem)

    click.echo('Checking "{0}" against solution: '.format(filename), nl=False)

    cmd = [sys.executable or 'python', filename]
    start = clock()
    proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
    stdout, stderr = proc.communicate()
    end = clock()
    time_info = format_time(start, end)

    # Python 3 returns bytes; use a valid encoding like ASCII as the output
    # will fall in that range
    if isinstance(stdout, bytes):
        output = stdout.decode('ascii')

    # Return value of anything other than 0 indicates an error
    if proc.poll() != 0:
        click.secho('Error calling "{0}".'.format(filename), fg='red')
        click.secho(time_info, fg='cyan')
        sys.exit(1)

    # Split output lines into array; make empty output more readable
    output_lines = output.splitlines() if output else ['[no output]']

    # If output is multi-lined, print the first line of the output on a
    # separate line from the "checking against solution" message, and
    # skip the solution check (multi-line solution won't be correct)
    if len(output_lines) > 1:
        is_correct = False
        click.echo('') # force output to start on next line
        for line in output_lines:
            click.secho(line, bold=True, fg='red')
    else:
        is_correct = output_lines[0] == solution
        fg_colour = 'green' if is_correct else 'red'
        click.secho(output_lines[0], bold=True, fg=fg_colour)

    click.secho(time_info, fg='cyan')

    # Exit here if answer was incorrect<commit_msg>Changed format of printed filenames<commit_after># -*- coding: utf-8 -*-

import os
import sys
import linecache
import subprocess

import click

from EulerPy.timing import clock, format_time

def get_filename(problem, width=3):
    """Returns filename padded with leading zeros"""
    return '{0:0{w}d}.py'.format(problem, w=width)


def get_solution(problem):
    """Returns the answer to a given problem"""
    solutionsFile = os.path.join(os.path.dirname(__file__), 'solutions.txt')
    line = linecache.getline(solutionsFile, problem)

    try:
        answer = line.split('. ')[1].strip()
    except IndexError:
        answer = None

    if answer:
        return answer
    else:
        msg = 'Answer for problem %i not found in solutions.txt.' % problem
        click.secho(msg, fg='red')
        click.echo('If you have an answer, consider submitting a pull request '
                   'to EulerPy at https://github.com/iKevinY/EulerPy.')
        sys.exit(1)


def get_problem(problem):
    """Parses problems.txt and returns problem text"""
    def problem_iter(problem, problemFile):
        with open(problemFile) as file:
            problemText = False
            lastLine = ''

            for line in file:
                if line.strip() == 'Problem %i' % problem:
                    problemText = True

                if problemText:
                    if line == lastLine == '\n':
                        break
                    else:
                        yield line[:-1]
                        lastLine = line

    problemFile = os.path.join(os.path.dirname(__file__), 'problems.txt')
    problemLines = [line for line in problem_iter(problem, problemFile)]

    if problemLines:
        # First three lines are the problem number, the divider line,
        # and a newline, so don't include them in the returned string
        return '\n'.join(problemLines[3:])
    else:
        msg = 'Problem %i not found in problems.txt.' % problem
        click.secho(msg, fg='red')
        click.echo('If this problem exists on Project Euler, consider '
                   'submitting a pull request to EulerPy at '
                   'https://github.com/iKevinY/EulerPy.')
        sys.exit(1)


def determine_largest_problem():
    """Determines largest problem file in the current directory"""
    # Arbitrary value that is larger than all Project Euler problems
    for problem in reversed(range(600)):
        if os.path.isfile(get_filename(problem)):
            return problem
    else:
        return False


# --cheat / -c
def cheat(problem):
    """View the answer to a problem."""
    # get_solution() will exit here if the solution does not exist
    solution = click.style(get_solution(problem), bold=True)
    click.confirm("View answer to problem %i?" % problem, abort=True)
    click.echo("The answer to problem %i is {0}.".format(solution) % problem)


# --generate / -g
def generate(problem, prompt_default=True):
    """Generates Python file for a problem."""

    msg = "Generate file for problem %i?" % problem
    click.confirm(msg, default=prompt_default, abort=True)
    problemText = get_problem(problem)

    filename = get_filename(problem)
    boldname = click.style(filename, bold=True)

    if os.path.isfile(filename):
        msg = '{0} already exists. Overwrite?'.format(boldname)
        click.confirm(click.style(msg, fg='red'), abort=True)

    problemHeader = 'Project Euler Problem %i\n' % problem
    problemHeader += '=' * len(problemHeader.strip()) + '\n\n'

    with open(filename, 'w') as file:
        file.write('"""\n')
        file.write(problemHeader)
        file.write(problemText)
        file.write('"""\n\n\n')

    click.secho('Successfully created {0}.'.format(boldname), fg='green')


# --preview / -p
def preview(problem):
    """Prints the text of a problem."""
    # Define problemText instead of echoing it right away
    # in case the problem does not exist in problems.txt
    problemText = get_problem(problem)[:-1] # strip newline from text
    click.secho("Project Euler Problem %i" % problem, bold=True)
    click.echo(problemText)


# --skip / -s
def skip(problem):
    """Generates Python file for the next problem."""
    click.echo("Current problem is problem %i." % problem)
    generate(problem + 1, prompt_default=False)


# --verify / -v
def verify(problem):
    """Verifies the solution to a problem."""
    filename = filename or get_filename(problem)
    boldname = click.style(filename, bold=True)

    if not os.path.isfile(filename):
        click.secho('Error: {0} not found.'.format(boldname), fg='red')
        sys.exit(1)

    # get_solution() will exit here if the solution does not exist
    solution = get_solution(problem)

    click.echo('Checking {0} against solution: '.format(boldname), nl=False)

    cmd = [sys.executable or 'python', filename]
    start = clock()
    proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
    stdout, stderr = proc.communicate()
    end = clock()
    time_info = format_time(start, end)

    # Python 3 returns bytes; use a valid encoding like ASCII as the output
    # will fall in that range
    if isinstance(stdout, bytes):
        output = stdout.decode('ascii')

    # Return value of anything other than 0 indicates an error
    if proc.poll() != 0:
        click.secho('Error calling {0}.'.format(boldname), fg='red')
        click.secho(time_info, fg='cyan')
        sys.exit(1)

    # Split output lines into array; make empty output more readable
    output_lines = output.splitlines() if output else ['[no output]']

    # If output is multi-lined, print the first line of the output on a
    # separate line from the "checking against solution" message, and
    # skip the solution check (multi-line solution won't be correct)
    if len(output_lines) > 1:
        is_correct = False
        click.echo('') # force output to start on next line
        for line in output_lines:
            click.secho(line, bold=True, fg='red')
    else:
        is_correct = output_lines[0] == solution
        fg_colour = 'green' if is_correct else 'red'
        click.secho(output_lines[0], bold=True, fg=fg_colour)

    click.secho(time_info, fg='cyan')

    # Exit here if answer was incorrect
{'fraction_non_alphanumeric': 0.11661005984150088, 'fraction_numerical': 0.0045285460132621705, 'mean_word_length': 2.690540137272456, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7412970', 'n_tokens_mistral': 3743, 'n_tokens_neox': 3514, 'n_words': 1139}