Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making unit tests for project Euler

Tags:

unit-testing

I'm starting to go through the questions in project Euler, and I'd like to approach it with a TDD style, but I'm having trouble finding the numeric answer to the question that doesn't include the code. Is there any resource with that data so that I can make test cases that will tell me if I've solved the problem correctly?

My motivation for this is that I feel like the algorithm is the answer, not the number. If I look at someone else's code sample, it ruins the challenge of figuring out how to solve the problem.

Edit: I'm looking specifically for the number of the answer with no context or algorithm with it so that I can do something like the following. I know it's more verbose, but I'd like to be able to have a pass/fail result to tell me whether or not my algorithm is correct, rather than looking at someone else's code example to know whether I've done it correctly.

import unittest
class ProblemOneTest(unittest.TestCase):
    def test_me(self):
        self.assertEquals(solve_problem_one(),233168)

if __name__ == '__main__':
    print "Problem 1 possible answer: %d" % solve_problem_one()
    sys.exit(unittest.main())
like image 258
Daenyth Avatar asked Jun 26 '10 12:06

Daenyth


People also ask

How do you structure unit tests?

A typical unit test contains 3 phases: First, it initializes a small piece of an application it wants to test (also known as the system under test, or SUT), then it applies some stimulus to the system under test (usually by calling a method on it), and finally, it observes the resulting behavior.

Does Project Euler have answers?

Problems are of varying difficulty, but each is solvable in less than a minute of CPU time using an efficient algorithm on a modestly powered computer. As of 27 April 2021, Project Euler has more than 1,000,000 users who have solved at least one problem, in over 100 different programming languages.


1 Answers

TDD and project Euler assignments don't necessarily go well together. First and foremost, TDD won't help you solve any project Euler (PE) problems. This reminds me of that well known attempt by a guy to "solve Sudoku" by using TDD.

TDD is not a design technique. It can be very useful when applicable, but don't think of it as a silver bullet.

A PE problem usually involves some heavy computation that ends in a single number, which is the answer. To apply TDD mindfully, I recommend using it for the mathematical utilities you will develop as parts of your endeavors to solve PE problems. For example, my utils module for PE consists of functions for computing primes, splitting numbers to digits, checking for palindromes, and so on. This module has a set of tests, because these functions are general enough to be tested. The PE solutions themselves don't have tests - the only real test needed for them is to eventually generate the correct answer.

like image 155
Eli Bendersky Avatar answered Oct 21 '22 05:10

Eli Bendersky