Skip to content

Luhn: Add a test with an odd number of spaces#1500

Merged
SleeplessByte merged 1 commit intoexercism:masterfrom
siebenschlaefer:luhn-odd-number-of-spaces
Apr 30, 2019
Merged

Luhn: Add a test with an odd number of spaces#1500
SleeplessByte merged 1 commit intoexercism:masterfrom
siebenschlaefer:luhn-odd-number-of-spaces

Conversation

@siebenschlaefer
Copy link
Copy Markdown
Contributor

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:

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.

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.
@SleeplessByte SleeplessByte merged commit d7fdadc into exercism:master Apr 30, 2019
@SleeplessByte
Copy link
Copy Markdown
Member

Three ✔️ makes the merge!

Thanks @siebenschlaefer , this was definitely missing!

@siebenschlaefer siebenschlaefer deleted the luhn-odd-number-of-spaces branch May 16, 2019 09:32
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
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants