There are two quite different questions.
Each 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 has
ISBN-10 0-201-18395-1
ISBN-13 9780201183955
so 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 same
time.�� In AWK I would write
���� x = a_putative_ISBN_10
���� y = x
���� gsub(/[- ]+/, "", 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 occur
or 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 problem
they are (a) excessive, (b) inefficient, and (c) insufficient.
���� digit count := 0.
���� check sum := 0.
���� for each character c of the string
������������ if c is not a hyphen then
�������������������� if c is a digit then
���������������������������� digit value := c's value as a digit
�������������������� else if c is X and digit count = 9 then
���������������������������� digit value := 10
�������������������� else
���������������������������� return 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 like
���� let no_dashes = filter (/= '-') (explode string) in
���� length no_dashes = 10 and
���� let check = last no_dashes in
���� (is_digit check or check = 'X') and
���� all is_digit (take 9 no_dashes) and
���� let xval c = if x = 'X' then 10 else digit_value c in
���� dot (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].
������ ^r
�� dot: other
������ ^self with: other inject: 0 into: [:acc :x :y | x*y + acc]
(These methods are so obvious that it would be absurd to claim
any intellectual property rights to them.)
I also have "fusion" methods like
SequenceableCollection>>
from: start to: finish allSatisfy: testBlock
�� self 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 compilers
don't work as hard at eliminating intermediate data
structures as functional language compilers.�� (Having
other priorities.)
���� (isdigit (last no_dashes
�������������������� return false if digit count > 10.
��������������������