views:

126

answers:

4

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())
+8  A: 

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.

Eli Bendersky
Sorry, I should have made myself more clear. I'm looking for the number by itself with no context so that I can do something like `assertEquals(my_solution(), expected_answer)`. I'll update the question
Daenyth
+1 writing tests for Project Euler problems only makes sense if you already have the answer and you want to optimize it.
Jurily
You can test the algorithm for solving the problem with a smaller limit against a value derived by brute force (I often did that).
starblue
+1  A: 

The unit test IS the answer.

The problems are usually so simple (not in terms of difficulty, but at least code layout) that breaking them up into various methods/classes is usually silly.

Dominic Bou-Samra
I suppose it's overkill, but it's also motivated by wanting to learn how to use unit tests in python. I also like to be able to check my work without referencing anyone else's code. Basically I just want to have a correct/incorrect statement relating to my algorithm.
Daenyth
+1  A: 

The problem page on the project Euler website has an input to check your answer. That's all I really need.

Daenyth
A: 

Despite the fact that these problems are more of a challenge without an answer to steer towards, a quick google search yielded:

http://code.google.com/p/projecteuler-solutions/wiki/ProjectEulerSolutions

njak32