Op 8-9-2020 om 08:30 schreef Roelof
Wobben:
Op 8-9-2020 om 04:22 schreef Richard O'Keefe:
There are two quite different questions.(1) Where may dashes occur in a real ISBN-10?(2) What does Exercism require in the specification and check in thetest cases?
For (1) the rules areEach ISBN consists of 5 elements with each section being separated by spaces or hyphens. Three of the five elements may be of varying length:
- Prefix element – currently this can only be either 978 or 979. It is always 3 digits in length
- Registration group element – this identifies the particular country, geographical region, or language area participating in the ISBN system. This element may be between 1 and 5 digits in length
- Registrant element - this identifies the particular publisher or imprint. This may be up to 7 digits in length
- Publication element – this identifies the particular edition and format of a specific title. This may be up to 6 digits in length
- Check digit – this is always the final single digit that mathematically validates the rest of the number. It is calculated using a Modulus 10 system with alternate weights of 1 and 3.
An ISBN-10 does not have the three-digit prefix. So we have[0-9]{1,5} -- prefix[0-9]{1,7} -- registrant[0-9]{1,6} -- publication[0-9X]-- check digit
As an examplw, "Standard C++ IOStreams and Locales" by Langer & Kreft hasISBN-10 0-201-18395-1ISBN-13 9780201183955so I shall assume the separators are optional./^[0-9]{1,5}[- ]?[0-9]{1,7}[- ]?[0-9]{1,6}[- ]?[0-9X]$/Of course the elements cannot all have their maximum length at the sametime. In AWK I would writex = a_putative_ISBN_10y = xgsub(/[- ]+/, "", y)if (x ~ /^[0-9]{1,5}[- ]?[0-9]{1,7}[- ]?[0-9]{1,6}[- ]?[0-9X]$/ \&& y ~ /^[0-9]{9,9}[0-9X]$/ \) {x *might* be valid, we still need to check the checksum}
For (2), there appear to be no restrictions on where dashes may occuror how many: "These may be communicated with or without hyphens".Exercism doesn't allow spaces.
Regular expressions are elegant in their own way, BUT for this problemthey are (a) excessive, (b) inefficient, and (c) insufficient.
digit count := 0.check sum := 0.
for each character c of the stringif c is not a hyphen thenif c is a digit thendigit value := c's value as a digitelse if c is X and digit count = 9 thendigit value := 10elsereturn false.digit count := digit count + 1.if digit count > 10 then return false.check sum := (11 - digit count) * digit value + check sum.return check sum mod 11 is zero.
Part of the insight here is "don't DO it, just PRETEND you did."That is, instead of copying the string without the hyphens,just ignore the hyphens as they turn up.Another part is "if you are only going to use it once, don't store it."That is, we need a digit's value just once, in the update to check sum,so we should compute it just before we need it, not store it.
Now the pseudo-code above is classic sequential imperative coding.
Classic functional coding does something likelet no_dashes = filter (/= '-') (explode string) inlength no_dashes = 10 andlet check = last no_dashes in(is_digit check or check = 'X') andall is_digit (take 9 no_dashes) andlet xval c = if x = 'X' then 10 else digit_value c indot (map xval no_dashes) [10,9..1]) mod 11 = 0
This pseudo-code translates nicely to Smalltalk too.You might want to add
SequenceableCollection>>with: other inject: initial into: aBlock|r|r := initial.self with: other do: [:x :y |r := aBlock value: r value: x value: y].^rdot: other^self with: other inject: 0 into: [:acc :x :y | x*y + acc]
(These methods are so obvious that it would be absurd to claimany intellectual property rights to them.)
I also have "fusion" methods likeSequenceableCollection>>from: start to: finish allSatisfy: testBlockself from: start to: finish do: [:each |(aBlock value: each) ifFalse: [^false]].^true
so that ((seq copyFrom: a to: z) allSatisfy: blk)can be done as (seq from: a to: z allSatisfy: blk)without making a copy.
Fusion methods are useful because Smalltall compilersdon't work as hard at eliminating intermediate datastructures as functional language compilers. (Havingother priorities.)
(isdigit (last no_dashes
return false if digit count > 10.
Thanks for letting me see this.
But still I wonder if this is really the OOP way and if that function does more then 1 thing.
It looks to me that its iterating trrough the string. Calculating the crc and checking it.
I learned that it is a good thing that a function and a class does only 1 thing.
Roelof
I learned on other oop languages that for mainibility it's needed that a class does one thing and a method does also one thing.
So when software changes , you can easily make the changes. Its I think called SOLID and I like that idea.
So I try to make that also work in Pharo but it seems not so important. It's look like me that getting it work is more important.
Roelof