A space to discuss and keep up software development and manage your software career
An inclusive community for gaming enthusiasts
News and discussion of science and technology such as AI, VR, cryptocurrency, quantum computing, and more.
From composing and gigging to gear, hot music takes, and everything in between.
Discussing AI software development, and showing off what we're building.
A general discussion space for the Forem community. If it doesn't have a home elsewhere, it belongs here
Movie and TV enthusiasm, criticism and everything in-between.
Memes and software development shitposting
Web design, graphic design and everything in-between
A community of golfers and golfing enthusiasts
Your central hub for all things security. From ethical hacking and CTFs to GRC and career development, for beginners and pros alike
For engineers building software at scale. We discuss architecture, cloud-native, and SRE—the hard-won lessons you can't just Google
Discussing the core forem open source software project — features, bugs, performance, self-hosting.
A collaborative community for all things Crypto—from Bitcoin to protocol development and DeFi to NFTs and market analysis.
A place for parents to the share the joys, challenges, and wisdom that come from raising kids. We're here for them and for each other.
A community for makers, hobbyists, and professionals to discuss Arduino, Raspberry Pi, 3D printing, and much more.
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.