-
Notifications
You must be signed in to change notification settings - Fork 319
/
ch-1.raku
149 lines (110 loc) · 4.39 KB
/
ch-1.raku
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
use v6d;
###############################################################################
=begin comment
Perl Weekly Challenge 162
=========================
TASK #1
-------
*ISBN-13*
Submitted by: Mohammad S Anwar
Write a script to generate the check digit of given ISBN-13 code. Please refer
[ https://en.wikipedia.org/wiki/International_Standard_Book_Number#ISBN-13_
check_digit_calculation |wikipedia] for more information.
Example
ISBN-13 check digit for '978-0-306-40615-7' is 7.
=end comment
###############################################################################
#--------------------------------------#
# Copyright © 2022 PerlMonk Athanasius #
#--------------------------------------#
#==============================================================================
=begin comment
Note
----
The following are accepted as input:
- A 13-digit ISBN code
- A 12-digit ISBN code, optionally followed by a question mark to indicate the
unknown check digit
- A 10-digit ISBN code, which will be converted to its 13-digit equivalent.
This must include a check digit, but it will not be checked for correctness.
All forms may contain separators (spaces or hyphens). No checks are made for
the correctness of separator placement.
=end comment
#==============================================================================
subset ISBN-char of Str where * ~~ / ^ <[ - \h 0 .. 9 ]> ** 10..17 $ /;
my Bool constant $VERBOSE = False;
#------------------------------------------------------------------------------
BEGIN
#------------------------------------------------------------------------------
{
"\nChallenge 162, Task #1: ISBN-13 (Raku)\n".put;
}
#==============================================================================
sub MAIN
(
#| 10-, 12-, or 13-digit ISBN code (may contain separators)
Str:D $code where { $code ~~ / ^ <[- \h 0 .. 9 ]>+ $ / &&
10 <= $code.chars <= 17 }
)
#==============================================================================
{
my Str $code-val = validate-code( $code );
"Input: $code".put;
my Str $digits = S:g/ <-[\d]> // given $code-val;
my UInt @digits = $digits.split( '', :skip-empty ).map: { .Int };
+@digits == 12 or die "ERROR: @digits contains { +@digits } characters";
loop (my UInt $i = 1; $i < +@digits; $i += 2)
{
@digits[ $i ] *= 3;
}
my UInt $sum = [+] @digits;
my UInt $r = 10 - ($sum % 10);
my UInt $check = $r < 10 ?? $r !! 0;
$code-val ~~ s/ \? $ //; # Remove trailing '?'
$code-val ~= '-' unless $code ~~ / \- $ /; # Add trailing hyphen
"Output: ISBN-13 check digit for '%s' is %d\n".printf:
$code ~ $check, $check;
}
#------------------------------------------------------------------------------
sub validate-code( Str:D $orig-code --> Str:D )
#------------------------------------------------------------------------------
{
my Str $code = $orig-code;
$code ~~ s:g/ <[- \h ]> //; # Remove separators (if any)
$code ~~ s/ \? $ //; # Remove a trailing '?' (if any)
$code ~~ / (<-[ 0 .. 9 ]>) / # Look for illegal characters
and error( qq[Invalid input character "$1"] );
my UInt $len = $code.chars;
$code = $orig-code; # Restore original separators
if $len == 10
{
$code = '978-' ~ $code;
$code.= chop: 1; # Remove trailing check digit
}
elsif $len == 13
{
$code.= chop: 1; # Remove trailing check digit
}
elsif $len != 12 # Missing check digit is allowed
{ # for ISBN-13 (not for ISBN-10)
error( 'Invalid length' );
}
return $code;
}
#------------------------------------------------------------------------------
sub error( Str:D $message )
#------------------------------------------------------------------------------
{
"ERROR: $message".put;
USAGE();
exit 0;
}
#------------------------------------------------------------------------------
sub USAGE()
#------------------------------------------------------------------------------
{
my Str $usage = $*USAGE;
$usage ~~ s/ ($*PROGRAM-NAME) /raku $0/;
$usage.put;
}
##############################################################################