Bill Allombert on Fri, 31 Jan 2025 18:59:22 +0100 |
[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]
Re: program to verify ISBN numbers (10 digit or 13 digit) |
On Fri, Jan 31, 2025 at 02:08:24AM +0100, Karim Belabas wrote: > Here's a further shortened version. verify_isbn(N)= { my(M, n, z); M = Map([10, [[10,9,8,7,6,5,4,3,2,1]~, 11]; 13, [[1,3,1,3,1,3,1,3,1,3,1,3,1]~, 10]]); n = if (type(N)=="t_STR", apply(eval, select(c -> c != "-", Vec(N))), type(N)=="t_INT", digits(N)); if (!mapisdefined(M, #n, &z) || (n * z[1]) % z[2], print("Invalid! ", N) , print("Valid ISBN: ", N)); } 2 comments: - for the '13' case, this just check this is an EAN13. To be an ISBN it needs to start by 978 or 979. - for the '10' case, the last digit can be the letter X to denote 10, since the checksum is mod 11. Game: why do we get this: ? verify_isbn("0-0000-0001-X"); Valid ISBN: 0-0000-0001-X Cheers, Bill, small-scale book publisher.