GithubHelp home page GithubHelp logo

eulerpy's Introduction

EulerPy Build Status PyPI Version Homebrew Version

EulerPy is a command line tool designed to streamline the process of solving Project Euler problems using Python. The package focuses on two main tasks: firstly, to create Python "template" files with a docstring containing the text of a Project Euler problem for ease-of-reference, and secondly, to check whether a problem has been solved correctly.

Installation

EulerPy can be installed (and updated) from PyPI using pip:

$ pip install --upgrade EulerPy

Conversely, it can be uninstalled using pip as well.

$ pip uninstall EulerPy

Alternatively, OS X users can install EulerPy using Homebrew:

$ brew install euler-py

Usage

First, you'll want to cd to the directory where your Project Euler files are being stored.

$ mkdir ~/project-euler
$ cd ~/project-euler

At this point, you'll probably want to run the euler command, which will prompt to create 001.py, a file containing the text to Project Euler problem #1 as its docstring.

$ euler
No Project Euler files found in the current directory.
Generate file for problem 1? [Y/n]: Y
Successfully created "001.py".

$ cat 001.py
"""
Project Euler Problem 1
=======================

If we list all the natural numbers below 10 that are multiples of 3 or 5,
we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.
"""

At this point, you can open up your editor of choice and code up a solution to the problem, making sure to print() the output. Once you feel that you've solved the problem, run the euler command again to verify your solution is correct. If the answer is correct, the solution will be printed in green and the script will ask to generate the next problem file. If incorrect, the solution will be printed in red instead. Additionally, the time elapsed during the solution-checking process will also be printed.

$ euler
Checking "001.py" against solution: [no output] # (output in red)

$ echo print 42 >> 001.py
$ euler
Checking "001.py" against solution: 42 # (output in green)
Generate file for problem 2? [Y/n]: Y
Successfully created "002.py".

EulerPy also has a few command line options that act as different commands (use euler --help to see a summary of all the options).

--cheat / -c

The --cheat option will print the answer to a problem after prompting the user to ensure that they want to see it. If no problem argument is given, it will print the answer to the problem that they are currently working on.

$ euler --cheat
View answer to problem 2? [y/N]: Y
The answer to problem 2 is <redacted>.

$ euler --cheat 100
View answer to problem 100? [y/N]: Y
The answer to problem 100 is <redacted>.

--generate / -g

The --generate option will create a Python file for the given problem number. If no problem number is given, it will overwrite the most recent problem with a file containing only the problem docstring (after prompting the user).

$ euler --generate
Generate file for problem 2? [Y/n]: Y
"002.py" already exists. Overwrite? [y/N]:
Successfully created "002.py".

$ euler --generate 5
Generate file for problem 5? [Y/n]: n
Aborted!

euler <problem> is equivalent to euler --generate <problem> if the file does not exist.

$ cat 005.py
cat: 005.py: No such file or directory

$ euler 5
Generate file for problem 5? [Y/n]: n
Aborted!

The file generation process will also automatically copy any relevant resource files to a resources subdirectory.

$ euler 22
Generate file for problem 22? [Y/n]: Y
Successfully created "022.py".
Copied "names.txt" to project-euler/resources.

--preview / -p

The --preview option will print the text of a given problem to the terminal; if no problem number is given, it will print the next problem instead.

$ euler --preview
Project Euler Problem 3
The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143?

$ euler --preview 5
Project Euler Problem 5
2520 is the smallest number that can be divided by each of the numbers
from 1 to 10 without any remainder.

What is the smallest number that is evenly divisible by all of the numbers
from 1 to 20?

--skip / -s

The --skip option will prompt the user to "skip" to the next problem. As of EulerPy v1.1, it will also append a "skipped" suffix to the skipped problem file.

$ euler --skip
Current problem is problem 2.
Generate file for problem 3? [y/N]: Y
Successfully created "003.py".
Renamed "002.py" to "002-skipped.py".

--verify / -v

The --verify option will check whether a given problem file outputs the correct solution to the problem. If no problem number is given, it will check the current problem.

$ euler --verify
Checking "003.py" against solution: [no output] # (output in red)

$ euler --verify 1
Checking "001.py" against solution: <redacted> # (output in green)

As of EulerPy v1.1, verifying a skipped problem file will remove the "skipped" suffix from its filename.

$ euler --verify 2
Checking "002-skipped.py" against solution: <redacted>
Renamed "002-skipped.py" to "002.py".

euler <problem> is equivalent to euler --verify <problem> if the file does exist.

$ cat 001.py
"""
Project Euler Problem 1
=======================

If we list all the natural numbers below 10 that are multiples of 3 or 5,
we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.
"""


$ euler 1
Checking "001.py" against solution: <redacted>

--verify-all

The --verify-all option was added in EulerPy v1.1. It essentially runs --verify on all the problem files it can find in the current directory, but also prints an overview of all of the problems in the directory. Note that if the verification encounters a KeyboardInterrupt exception, it will skip the verification of that specific file. This allows for the ability to skip verifying some files but not others, in the case that some solutions are taking too long to compute.

$ euler --verify-all
Checking "001.py" against solution: <redacted>

Checking "002.py" against solution: ^C

Checking "003.py" against solution: [no output]

---------------------------------------------------------------
C = correct, I = incorrect, E = error, S = skipped, . = missing

Problems 001-020: C S I . .   . . . . .   . . . . .   . . . . .

This option should be run after upgrading to v1.1 from EulerPy v1.0, as it will automatically rename any problems that have been skipped using --skip, making them easy to distinguish from those that have been correctly solved.

File Prefixes

As of v1.3.0, EulerPy will attempt to keep the prefix of problem files consistent. The motivation behind this is that import 001 results in a syntax error whereas import euler001 is valid. By using the latter naming scheme, it is possible to reuse code written in previous files.

$ mv 003.py euler003.py

$ euler --skip
Current problem is problem 3.
Generate file for problem 4? [y/N]: Y
Successfully created "euler004.py".
Renamed "euler003.py" to "euler003-skipped.py".

Contributing

See CONTRIBUTING.rst.

Miscellaneous

The text for the problems in problems.txt were derived from Kyle Keen's Local Euler project, and the solutions in solutions.txt were derived from Bai Li's projecteuler-solutions repository.

See this blog post for insight into the development process.

EulerPy uses Click as a dependency for its CLI functionality.

License

EulerPy is licensed under the MIT License.

eulerpy's People

Contributors

alefnula avatar alexeymk avatar bsoyka avatar c0mkid avatar gdfelt avatar ikeviny avatar mitchellmcmillan avatar orsenthil avatar qu4tro avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

eulerpy's Issues

s/solution/answer/g

For some reason i was expecting to see a solution in code when i want to cheat, not just the answer...

I guess solution is the proj. Euler terminology see what makes more sense...

Change timing functionality to include CPU timing.

Alright, so I initially made the decision to use wall timings rather than CPU timings in #8. This was mainly because I didn't want the output to become too cluttered with information, but I also wanted to maintain a one-liner output, and having various different timings all on one line (other than total time) might make things confusing.

However, after sleeping on it, I remembered that I specifically coded in functionality to allow for multi-line outputs (lines 111-118 in euler.py). In commit ec3b50b, I changed the behaviour of multi-line outputs to print from this:

$ euler -v 1
Checking "001.py" against solution: 123
12345

To this:

$ euler -v 1
Checking "001.py" against solution:
123
12345

Since the a multi-line output is not going to print the correct solution anyways (the Project Euler solutions are, for the most part, single values), this is just an easier-to-read way of printing the output.

Having realized that I'm not really restricted to single-line outputs anyways, I think that I would now be more satisfied with the following behaviour of the timing output:

POSIX:

$ euler -v 1
Checking "010.py" against solution: 123
Time elapsed: user 3.17 s, sys: 33.9 ms, total: 3.21 s

POSIX (multi-line output):

$ euler -v 1
Checking "010.py" against solution:
123
12345
Time elapsed: user 3.17 s, sys: 33.9 ms, total: 3.21 s

Windows:

$ euler -v 1
Checking "010.py" against solution: 123
Time elapsed: 3.21 s

I apologize for my indecisiveness about this, @alefnula; I'm not really too experienced with making these sorts of executive project decisions yet.

Line endings interfering with solution checking on Windows?

From this thread over on Reddit, it seems as though there's an issue with solution checking on Windows. However, as I am running OS X (and don't have a Windows partition/VM set up at the moment), I can't verify anything about this issue.

From what I can gather, it seems as though the problem could be mitigated by changing line 129 in euler.py from is_correct = output == solution to is_correct = output.strip() == solution to strip the \r character printed by the output on Windows.

Could someone running Windows verify if this is indeed the case?

Should --cheat be removed?

I'm not sure which way to go on this topic. I understand that there may be some concern surrounding people simply going into the solutions file to grab answers, but the repository that I grabbed the solutions from is only one search away anyways. To be completely honest, I think that if someone feels as though they've "solved" a problem simply because they are in possession of the correct answer, it's their loss for not understanding the process behind deriving the answer. However, if the general consensus is that it's detrimental to the Project Euler community to include functionality that allows for the answer to be printed, I'll remove it (and also encrypt the answer file).

Decrapation of time.clock() method in python 3.8

As you can see in this issue from the CPython repository the method time.clock() has been removed as of version 3.8 (Official python bug).

In your clock() function inside the utils module you are using this method making this whole package unusable (in case the resource module is not importable)

# Use the resource module instead of time.clock() if possible (on Unix)
try:
    import resource

except ImportError:
    import time

    def clock():
        """
        Under Windows, system CPU time can't be measured. Return time.clock()
        as user time and None as system time.
        """
        return time.clock(), None

else:
    def clock():
        """
        Returns a tuple (t_user, t_system) since the start of the process.
        This is done via a call to resource.getrusage, so it avoids the
        wraparound problems in time.clock().
        """
        return resource.getrusage(resource.RUSAGE_CHILDREN)[:2]

I don't know how willingly are you about still maintaining this repository. But I had to change this locally in my windows machine.

Problem 8

Hi all,

it seems the solution for Problem 8 is incorrect. When multiply 13 consecutive digits you get 23514624000, however the problem statement asks for 5 consecutive digits...

Translations

For educational purpose it would be cool to have translations of questions, so that non-english speaker could solve them.

The locale module in python returns a list of OS supported languages, so we can automatically select the system language if the translation is available (fallback to english otherwise). This behavior should also be over-writable with a command line parameter -l, --lang.

Here are some french translations for instance: http://blog.lucaswillems.com/532/traduction-problemes-1-50-project-euler

I tried and met with this traceback

$ euler -g 1
Traceback (most recent call last):
  File "/Users/skumaran/AllPython/CPython-2.6.8/bin/euler", line 9, in <module>
    load_entry_point('EulerPy==1.0.1', 'console_scripts', 'euler')()
  File "/Users/skumaran/AllPython/CPython-2.6.8/lib/python2.6/site-packages/click/core.py", line 488, in __call__
    return self.main(*args, **kwargs)
  File "/Users/skumaran/AllPython/CPython-2.6.8/lib/python2.6/site-packages/click/core.py", line 474, in main
    self.invoke(ctx)
  File "/Users/skumaran/AllPython/CPython-2.6.8/lib/python2.6/site-packages/click/core.py", line 653, in invoke
    ctx.invoke(self.callback, **ctx.params)
  File "/Users/skumaran/AllPython/CPython-2.6.8/lib/python2.6/site-packages/click/core.py", line 325, in invoke
    return callback(*args, **kwargs)
  File "/Users/skumaran/AllPython/CPython-2.6.8/lib/python2.6/contextlib.py", line 34, in __exit__
    self.gen.throw(type, value, traceback)
  File "/Users/skumaran/AllPython/CPython-2.6.8/lib/python2.6/site-packages/click/core.py", line 57, in augment_usage_errors
    yield
  File "/Users/skumaran/AllPython/CPython-2.6.8/lib/python2.6/site-packages/click/core.py", line 325, in invoke
    return callback(*args, **kwargs)
  File "/Users/skumaran/AllPython/CPython-2.6.8/lib/python2.6/site-packages/EulerPy/euler.py", line 194, in main
    generate_file(problem)
  File "/Users/skumaran/AllPython/CPython-2.6.8/lib/python2.6/site-packages/EulerPy/euler.py", line 88, in generate_file
    click.confirm("Generate file for problem #{}?".format(problem), default=default, abort=True)
ValueError: zero length field name in format
[localhost euler]$

Cache data files

I suggest that we should cache all the data files used throughout the problems, I quickly extracted them all here:

http://projecteuler.net/project/names.txt
http://projecteuler.net/project/words.txt
http://projecteuler.net/project/poker.txt
http://projecteuler.net/project/cipher1.txt
http://projecteuler.net/project/triangle.txt
http://projecteuler.net/project/keylog.txt
http://projecteuler.net/project/matrix.txt
http://projecteuler.net/project/matrix.txt
http://projecteuler.net/project/matrix.txt
http://projecteuler.net/project/roman.txt
http://projecteuler.net/project/sudoku.txt
http://projecteuler.net/project/words.txt
http://projecteuler.net/project/base_exp.txt
http://projecteuler.net/project/triangles.txt
http://projecteuler.net/project/sets.txt
http://projecteuler.net/project/network.txt

Image: http://projecteuler.net/project/images/p_201_laserbeam.gif
Image: http://projecteuler.net/project/images/p_208_robotwalk.gif
Image: http://projecteuler.net/project/images/p_215_crackfree.gif
Image: http://projecteuler.net/project/images/p_220.gif
Image: http://projecteuler.net/project/images/p_226_formula.gif
Image: http://projecteuler.net/project/images/p_226_scoop2.gif
Image: http://projecteuler.net/project/images/p_228.png
Image: http://projecteuler.net/project/images/p_237.gif
Image: http://projecteuler.net/project/images/p_244_start.gif
Image: http://projecteuler.net/project/images/p_244_example.gif
Image: http://projecteuler.net/project/images/p_244_start.gif
Image: http://projecteuler.net/project/images/p_244_target.gif
Image: http://projecteuler.net/project/images/p_246_anim.gif
Image: http://projecteuler.net/project/images/p_246_ellipse.gif
Image: http://projecteuler.net/project/images/p_247_hypersquares.gif
Image: http://projecteuler.net/project/images/p_251_cardano.gif
Image: http://projecteuler.net/project/images/p_252_convexhole.gif
Image: http://projecteuler.net/project/images/p_255_Heron.gif
Image: http://projecteuler.net/project/images/p_255_Example.gif
Image: http://projecteuler.net/project/images/p_256_tatami3.gif

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.