Unit Testing for the Euler Project

I'm starting to ask questions in the Euler project, and I would like to approach it with a TDD style, but it's hard for me to find a numerical answer to a question that does not include code. Is there any resource with this data so that I can make test cases that will tell me if I solved the problem correctly?

My motivation for this is that I feel that the algorithm is the answer, not the number. If I look at the sample code of another user, this will destroy the problem in order to figure out how to solve the problem.

Edit: I'm looking for a specific answer number with no context or algorithm so that I can do something like the following. I know this is more detailed, but I would like to get a result with an error / message about whether my algorithm works correctly, and not look at an example of another code to find out if I did 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())
+5
source share
7 answers

The problem page on the Euler project website has an entry to verify your answer. That’s all I really need.

+4
source

TDD . , TDD - Euler (PE). " " TDD.

TDD - . , , .

PE , , . TDD , , PE. , utils PE , , .. , . PE - , , , .

+11

, , .

, Python ( ). , , - " assert ' . , . , , , 30.

+2

unit test .

, , ( , , , ), /, .

+1

, 3 , , , Project Euler TDD.

Python, .

:

  • ( ) , /, , . , - , , , .
  • Euler / . , , .
  • / , , . , 12 ( ) - get_triangle_num_with_n_or_more_divisors (n).
  • , . : test_example test_problem. test_problem @unittest.skip('Unimplemented'), . :

    import unittest
    
    from problems.p0014 import get_triangle_num_with_n_or_more_divisors
    
    class TestHighlyDivisibleTriangleNumber(unittest.TestCase):
        def test_example(self):
            self.assertEquals(get_triangle_num_with_n_or_more_divisors(1),
                              1)
            self.assertEquals(get_triangle_num_with_n_or_more_divisors(2),
                              3)
            self.assertEquals(get_triangle_num_with_n_or_more_divisors(6),
                              28)
    
        @unittest.skip('Unimplemented')
        def test_problem(self):
            self.assertEquals(get_triangle_num_with_n_or_more_divisors(500),
                              'TODO: Replace this with answer')
    

Project Euler, TDD. , . , , , .

get_triangle_num_with_n_or_more_divisors. test_example , ; , , .

+1

, :

Hackerrank, Project Euler, TDD. . , . , .

? , , , . , , .

JavaScript:

var cases = [
  {input: '1\n15', output: '45'},
  ...
];

describe('Multiples of 3 and 5', function() {
  cases.forEach((v, i) => {
    it('test case #' + i, function () {
      assert.equal(unit(v.input), v.output);
    })
  });
});

Although Hackerrank uses stdin and stdout, I'm still trying to extract the main code into a function and use functional programming.

0
source

All Articles