Thank you for contribution.
I have really tried my best to understand, but have failed.
Could you use examples?
Actually I had an error. You shouldn't convert the digit. And instead modulo through 7.
For 9999|8
The end result isn't 0, so the digit isn't valid
For 1234|2
The end result is 0, so the digit is valid
For 1169|7
1.1169 % 7 = 0 //If you modulo through 7, when the digit sum is 7 instead 0 comes out, so you can consider 0=7.
Updated code
boolean checkdigit(int digit,int checksum): int sum = digit % 7 //% is the modulo operator if(sum == 0): sum = 7 return checksum - sum == 0
You can also consider 0 and 7 equal and spare the conversion from 0 to 7:
boolean checkdigit(int digit,int checksum): int sum = digit % 7 //% is the modulo operator return checksum - sum == 0 || checksum - sum == 7
Or you could modulo the difference through 7. The difference must be either 7 or 0 and both have the same result for modulo 7.
boolean checkdigit(int digit,int checksum): int sum = digit % 7 //% is the modulo operator return (checksum - sum) % 7 == 0
So in one line it would look like this
boolean check = (checksum - digit % 7 ) % 7 == 0
Thank you for clarifying. I have now understood.
It is actually a really neat solution it basically reduces the code to just two functions.
(defn is-valid-modified? [number] (let [first-four (quot number 10) check-sum (rem number 10)] (-> first-four (mod 7) (- check-sum) (mod 7) (= 0)))) user> (is-valid-modified? 10006) true user> (is-valid-modified? 99993) true user> (is-valid-modified? 99998) false user> (is-valid-modified? 12342) true user> (is-valid-modified? 11697) true
(defn validate-scratch-card [card-number] (let [f (comp is-valid-modified? #(Integer/parseInt %))] (->> (string/split card-number #"(-|\s)") (map f) (every? true?)))) user> (validate-scratch-card "10006 12342 00081 99998") false user> (validate-scratch-card "10006 12342 00081 99993") true
I have learnt something, thank you.
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Thank you for contribution.
I have really tried my best to understand, but have failed.
Could you use examples?
Actually I had an error. You shouldn't convert the digit. And instead modulo through 7.
For 9999|8
The end result isn't 0, so the digit isn't valid
For 1234|2
The end result is 0, so the digit is valid
For 1169|7
1.1169 % 7 = 0 //If you modulo through 7, when the digit sum is 7 instead 0 comes out, so you can consider 0=7.
The end result is 0, so the digit is valid
Updated code
You can also consider 0 and 7 equal and spare the conversion from 0 to 7:
Or you could modulo the difference through 7. The difference must be either 7 or 0 and both have the same result for modulo 7.
So in one line it would look like this
Thank you for clarifying. I have now understood.
It is actually a really neat solution it basically reduces the code to just two functions.
I have learnt something, thank you.