/
ch-2.pl
222 lines (158 loc) · 5.3 KB
/
ch-2.pl
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!perl
###############################################################################
=comment
Perl Weekly Challenge 140
=========================
TASK #2
-------
*Multiplication Table*
Submitted by: Mohammad S Anwar
You are given 3 positive integers, $i, $j and $k.
Write a script to print the $kth element in the sorted multiplication table of
$i and $j.
Example 1
Input: $i = 2; $j = 3; $k = 4
Output: 3
Since the multiplication of 2 x 3 is as below:
1 2 3
2 4 6
The sorted multiplication table:
1 2 2 3 4 6
Now the 4th element in the table is "3".
Example 2
Input: $i = 3; $j = 3; $k = 6
Output: 4
Since the multiplication of 3 x 3 is as below:
1 2 3
2 4 6
3 6 9
The sorted multiplication table:
1 2 2 3 3 4 6 6 9
Now the 6th element in the table is "4".
=cut
###############################################################################
#--------------------------------------#
# Copyright © 2021 PerlMonk Athanasius #
#--------------------------------------#
#==============================================================================
=comment
Assumption
----------
A "positive integer" is an integer greater than or equal to one.
Configuration
-------------
Set $VERBOSE to a true value (the default) to display an explanation of the
output like that in the Examples. Set it to a false value to remove the explan-
ation.
Algorithm
---------
(Straightforward:) construct the multiplication table; sort it in ascending
numerical order; then index it to obtain the desired output.
=cut
#==============================================================================
use strict;
use warnings;
use Const::Fast;
use Regexp::Common qw( number );
const my $VERBOSE => 1;
const my $USAGE =>
"Usage:
perl $0 <i> <j> <k>
<i> Positive integer: maximum multiplier for the table
<j> Positive integer: maximum multiplicand for the table
<k> Positive integer: 1-based index into the sorted table\n";
#------------------------------------------------------------------------------
BEGIN
#------------------------------------------------------------------------------
{
$| = 1;
print "\nChallenge 140, Task #2: Multiplication Table (Perl)\n\n";
}
#==============================================================================
MAIN:
#==============================================================================
{
my ($i, $j, $k) = parse_command_line();
print "Input: \$i = $i; \$j = $j; \$k = $k\n";
my @table;
for my $row (1 .. $i) # Construct the table
{
for my $col (1 .. $j)
{
push @table, $col * $row;
}
}
my @sorted = sort { $a <=> $b } @table; # Sort the table
my $element = $sorted[ $k - 1 ]; # Index the sorted table
print "Output: $element\n";
explain( $i, $j, $k, \@table, \@sorted, $element ) if $VERBOSE;
}
#------------------------------------------------------------------------------
sub explain
#------------------------------------------------------------------------------
{
my ($i, $j, $k, $table, $sorted, $element) = @_;
print "\nSince the multiplication of $i x $j is as below:\n";
# (1) Pre-compute table column widths
my @widths;
for my $idx (($i - 1) * $j .. $i * $j - 1)
{
push @widths, length $table->[ $idx ];
}
# (2) Print the table
for my $row (0 .. $i - 1)
{
print ' ';
for my $idx ($row * $j .. ($row + 1) * $j - 1)
{
printf ' %*d', $widths[ $idx % $j ], $table->[ $idx ];
}
print "\n";
}
# (3) Print the contents of the sorted table
printf "\nThe sorted multiplication table:\n\n %s\n",
join ' ', @$sorted;
# (4) Explain the output
printf qq[\nNow the %s element in the table is "%d".\n],
ordinal( $k ), $element;
}
#------------------------------------------------------------------------------
sub ordinal
#------------------------------------------------------------------------------
{
my ($n) = @_;
my $suffix = 'th';
my $digit0 = substr $n, -1, 1;
if (length $n < 2 || substr( $n, -2, 1 ) ne '1')
{
$suffix = $digit0 eq '1' ? 'st' :
$digit0 eq '2' ? 'nd' :
$digit0 eq '3' ? 'rd' : 'th';
}
return $n . $suffix;
}
#------------------------------------------------------------------------------
sub parse_command_line
#------------------------------------------------------------------------------
{
my $args = scalar @ARGV;
$args == 3 or error( "Expected 3 command line arguments, found $args" );
my ($i, $j, $k) = @ARGV;
for my $n ($i, $j, $k)
{
$n =~ / ^ $RE{num}{int} $ /x
or error( qq["$n" is not a valid integer] );
$n > 0 or error( qq["$n" is not positive] );
}
# Check that $k is a valid index
$k <= $i * $j or error( "\$k = $k is too large" );
return ($i, $j, $k);
}
#------------------------------------------------------------------------------
sub error
#------------------------------------------------------------------------------
{
my ($message) = @_;
die "ERROR: $message\n$USAGE";
}
###############################################################################