Skip to content

luhn: add a test case #1246

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 29, 2018
Merged

luhn: add a test case #1246

merged 1 commit into from
May 29, 2018

Conversation

idealhack
Copy link
Contributor

@idealhack idealhack commented May 25, 2018

@rpottsoh rpottsoh changed the title add a test case to the luhn exercise luhn: add a test case May 25, 2018
Copy link
Member

@rpottsoh rpottsoh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At a quick glance your addition seems reasonable.
However, please increase second version component of the JSON file. If current version is x . y . z, make the new version x . y+1 . 0

@@ -105,6 +105,14 @@
"value": "091"
},
"expected": true
},
{
"description": "strings with non-digits is invalid",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this new case different from "valid strings with a non-digit included become invalid"?

Copy link
Contributor Author

@idealhack idealhack May 25, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

package luhn

import "strings"

func Valid(str string) bool {
	str = strings.Replace(str, " ", "", -1)
	if len(str) < 2 {
		return false
	}

	var sum = 0
	for i, ch := range str {
		val := int(ch - '0')
		if (len(str)-i)%2 == 0 {
			product := val * 2
			if product > 9 {
				product -= 9
			}
			sum += product
		} else {
			sum += val
		}
	}

	return sum%10 == 0
}

The code with a mistake above will pass current tests, but not this one.

So we have to check non-digits by adding:

if val > 9 || val < 0 {
    return false
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation. I can see now how "valid strings with a non-digit included become invalid" passes without the new test case.

Other reviewers may ask that your new test case be moved to an earlier location in the JSON file. I'll leave that open to others to address.

@idealhack thanks for your time in improving this exercise! 👍

@idealhack
Copy link
Contributor Author

@rpottsoh I've increased the minor version number, please take a look

@rpottsoh
Copy link
Member

version looks good. I am going to let this PR sit for now, to allow time for others to weigh in the change you are proposing.

@rpottsoh
Copy link
Member

I think enough time has passed for others to convey their thoughts on this PR. I am going to go ahead and merge. Thanks @idealhack for giving some of your time to Exercism and making Luhn a better exercise.

@rpottsoh rpottsoh merged commit 3930b0a into exercism:master May 29, 2018
@idealhack
Copy link
Contributor Author

@rpottsoh You are welcome. Exercism is great because of your work.

petertseng pushed a commit that referenced this pull request Mar 27, 2019
luhn 1.5.0

Continuation of #1246
#1246 placed a non-digit in a doubled position.

However, there are some solutions which still pass all tests:
They reject the above case, perhaps by being "clever" with integer division
(results in 1 or 0 for digits, but other values for non-digits)
but yet would incorrectly accept an input with a non-digit in non-doubled position

This commit adds such an input.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants