All looks great to me by eye but would you mind making the following minor modifications before I approve it?
Seed the random number generator with the current time using math.randomseed(os.time()) before executing the random tests. IIRC Lua uses a fixed default seed for math.random() which means that the sequence of "random" numbers generated will be identical in each program run unless it is explicitly seeded (with the current time).
Place the random test in a for loop so that it gets executed at least 10 times (and ideally about 100 times if possible), like such:
-- Don't forget to seed math.random ;)math.randomseed(os.time())
-- Execute random tests in a loopfori = 1, 10, 1dorandomInput = math.random() -- or some other randomly generated input-- as deemed appropriate for the Kataassert.are.same(referenceSolution(randomInput--[[ or inputs --]]), userSolution(randomInput))
end
Thank you for your answer.
I was hoping for better solution than copy over final solution to test cases.
Random part in test cases is uncommented and final solution is copied over.
I don't know how to require the "reference" function into test case. If you do please tell me.
To compare the user solution against a reference solution, you have to define the reference solution in the Test Cases section itself. For example:
-- Reference Solution (do NOT include in Sample Test Cases!!!)localfunctionsolution(a, b)
-- Simply copy and paste the function body of your working "Final Solution"-- into that of the reference solution. Note however that-- (1) you should also define your set of helper functions in the Submit Tests-- (and copy them over) if you used any in your "Final Solution" and-- (2) take care to ensure that the reference solution calls **itself**-- and NOT the user solution if there are any recursive casesreturna * bend-- Submit Testsdescribe('The multiply function', function ()
it('should work for some fixed tests', function ()
assert.are.same(15, multiply(3, 5))
assert.are.same(24, multiply(4, 6))
assert.are.same(81, multiply(9, 9))
assert.are.same(180, multiply(15, 12))
end)
it('should work for some random tests', function ()
-- Seed the random number generator with the current timemath.randomseed(os.time())
-- Execute 100 random testsfori = 1, 100, 1doa = math.random(12)
b = math.random(12)
assert.are.same(solution(a, b), multiply(a, b))
endend)
end)
Since I don't actually know Lua, I can't guarantee that the code example I provided above would compile (I haven't tested it on an online compiler) but hopefully you get the idea :)
It would be best to adhere to the Kata Description and name the user solution interpreter to ensure consistency between Lua and other language versions.
Why are the random tests currently commented out? ;)
Next time you publish a translation, please ensure that the tab is on "Test Cases" (i.e. the Sumbit tests) and not the "Sample Test Cases" so I can confirm that the "actual" test cases are indeed working as expected, cheers :)
I guess it's the overflow considerations?
what is meant with the "worst case" ?
If the goal is to pass the test, that's fine. But this code cannot be considered best practice because it does not consider the worst case.
Approved some time ago
A sturdier initial value for
max
could be-math.huge
, which is-inf
when Lua was compiled with floating-point numbers.This comment is hidden because it contains spoiler information about the solution
Prepared Erlang translation:
https://www.codewars.com/kumite/5a4bc2118803859bed00012c
Please review.
Many thanks for your contribution; reviewed and approved :D
No problem.
1-2) Added
All looks great to me by eye but would you mind making the following minor modifications before I approve it?
math.randomseed(os.time())
before executing the random tests. IIRC Lua uses a fixed default seed formath.random()
which means that the sequence of "random" numbers generated will be identical in each program run unless it is explicitly seeded (with the current time).for
loop so that it gets executed at least10
times (and ideally about100
times if possible), like such:Cheers :)
Thank you for your answer.
I was hoping for better solution than copy over final solution to test cases.
Random part in test cases is uncommented and final solution is copied over.
To compare the user solution against a reference solution, you have to define the reference solution in the Test Cases section itself. For example:
Since I don't actually know Lua, I can't guarantee that the code example I provided above would compile (I haven't tested it on an online compiler) but hopefully you get the idea :)
Thank you for feedback.
I am doing translation for the first time, so I don't know the in and outs yet.
interpreter
to ensure consistency between Lua and other language versions.Lua translation Kumited!
https://www.codewars.com/kumite/5a496360b3bfa8aa2900008f/
Please review and approve!