Implement exercise - luhn#249
Conversation
|
Implement new exercise luhn, ready for review. |
arcuru
left a comment
There was a problem hiding this comment.
The tests look good, but a few things with the formatting and the example need fixing :)
|
Currently all the valid numbers in the tests have an even number of spaces or all the digits are bool luhn::valid(const std::string& str) {
int sum = 0, digit_count = 0, factor = 1 + str.size() % 2;
for (auto i = 0u; i < str.size(); ++i) {
if (str[i] == ' ') continue;
if (str[i] < '0' || '9' < str[i]) return false;
++digit_count;
factor = 3 - factor;
int digit = (str[i] - '0') * factor;
if (digit > 9) digit -= 9;
sum += digit;
}
return digit_count > 1 && sum % 10 == 0;
}Please add a test where the the number is valid and it has an an odd number of spaces, e.g. BOOST_AUTO_TEST_CASE(valid_number_with_an_odd_number_of_spaces)
{
const bool actual = luhn::valid("095 245 88");
const bool expected {true};
BOOST_TEST(expected == actual);
} |
|
Hold on. @junming403 please keep the original test that matches the problem specifications https://github.com/exercism/problem-specifications/blob/master/exercises/luhn/canonical-data.json @siebenschlaefer that does seem like it might be a problem with the tests, however we do try to keep the test suites here identical the main problem specifications. Please change the canonical test suite in the problem-specifications repo so all the other tracks can benefit too :) |
|
@patricksjackson Thanks, I didn't realize the rules are that tight. I submitted a PR to the problem-specifications repo: exercism/problem-specifications#1500 |
|
@siebenschlaefer It's not really that the rules are tight, it's that I don't want to be in a position to maintain a good set of tests, so it's easier to let the central team do it. There are actually some places where we might have an extra test for a C++ specific thing, but those are the only exceptions we generally make. @junming403 I'll come back and review your updates when I get the chance, it might take until the weekend. |
| if (*c == ' ') { | ||
| continue; | ||
| } else if (isdigit(*c)) { | ||
| int digit = (int)*c - '0'; |
|
Updated according to review |
|
Thanks! |
No description provided.