Luhn: Add a test with an odd number of spaces#1500
Merged
SleeplessByte merged 1 commit intoexercism:masterfrom Apr 30, 2019
Merged
Luhn: Add a test with an odd number of spaces#1500SleeplessByte merged 1 commit intoexercism:masterfrom
SleeplessByte merged 1 commit intoexercism:masterfrom
Conversation
Currently the test have only valid numbers (a) without spaces, (b) with
an even number of spaces, or (c) where all digits are 0.
That allows an approach where you iterate from left to right, starting
with a factor of `2 - length(number) % 2`
In Python:
```python
class Luhn(object):
def __init__(self, card_num):
self.card_num = card_num
def is_valid(self):
sum = 0
digit_count = 0
factor = 2 - len(self.card_num) % 2
for c in self.card_num:
if c == ' ': continue
if not ('0' <= c <= '9'): return False
digit_count += 1
digit = int(c) * factor
if digit > 9: digit -= 9
sum += digit
factor = 3 - factor
return digit_count > 1 and sum % 10 == 0
```
Adding a test case where a valid number has an odd number of spaces can
catch that invalid approach.
ErikSchierboom
approved these changes
Apr 30, 2019
cmccandless
approved these changes
Apr 30, 2019
SleeplessByte
approved these changes
Apr 30, 2019
Member
|
Three ✔️ makes the merge! Thanks @siebenschlaefer , this was definitely missing! |
ZapAnton
added a commit
to ZapAnton/rust
that referenced
this pull request
Nov 12, 2019
ZapAnton
added a commit
to ZapAnton/rust
that referenced
this pull request
Nov 12, 2019
coriolinus
pushed a commit
to exercism/rust
that referenced
this pull request
Nov 15, 2019
* luhn: Updated the exercise to the version 1.6.1 Relevant PRs: - exercism/problem-specifications#1420 - exercism/problem-specifications#1480 - exercism/problem-specifications#1500 - exercism/problem-specifications#1523 * luhn: Fixed the 'exercise' util generated comments in the test suite
tejasbubane
added a commit
to tejasbubane/haskell
that referenced
this pull request
Mar 14, 2020
sshine
pushed a commit
to exercism/haskell
that referenced
this pull request
Mar 25, 2020
Changes included: 1. exercism/problem-specifications#1480: 1.4.0 to 1.5.0: Change "055a 444 285" test into "055b 444 285" for subtle reasons. New test name: using ascii value for doubled non-digit isn't allowed 2. exercism/problem-specifications#1500: 1.5.0 to 1.6.0: Add test case: valid number with an odd number of spaces 3. exercism/problem-specifications#1635: 1.6.0 to 1.7.0: Add test case: invalid long number with an even remainder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Currently the test have only valid numbers (a) without spaces, (b) with
an even number of spaces, or (c) where all digits are 0.
That allows an approach where you iterate from left to right, starting
with a factor of
2 - length(number) % 2In Python:
Adding a test case where a valid number has an odd number of spaces can
catch that invalid approach.