/
ch-2.pl
174 lines (128 loc) · 3.84 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
#!perl
###############################################################################
=comment
Perl Weekly Challenge 163
=========================
TASK #2
-------
*Summations*
Submitted by: Mohammad S Anwar
You are given a list of positive numbers, @n.
Write a script to find out the summations as described below.
Example 1
Input: @n = (1, 2, 3, 4, 5)
Output: 42
1 2 3 4 5
2 5 9 14
5 14 28
14 42
42
The nth Row starts with the second element of the (n-1)th row.
The following element is sum of all elements except first element of previous
row.
You stop once you have just one element in the row.
Example 2
Input: @n = (1, 3, 5, 7, 9)
Output: 70
1 3 5 7 9
3 8 15 24
8 23 47
23 70
70
=cut
###############################################################################
#--------------------------------------#
# Copyright © 2022 PerlMonk Athanasius #
#--------------------------------------#
#==============================================================================
=comment
Assumption
----------
"Positive numbers" are integers greater than zero.
=cut
#==============================================================================
use strict;
use warnings;
use Const::Fast;
use Regexp::Common qw( number );
const my $VERBOSE => 1;
const my $USAGE =>
"Usage:
perl $0 [<n> ...]
[<n> ...] A list of positive integers\n";
#------------------------------------------------------------------------------
BEGIN
#------------------------------------------------------------------------------
{
$| = 1;
print "\nChallenge 163, Task #2: Summations (Perl)\n\n";
}
#==============================================================================
MAIN:
#==============================================================================
{
my @n = parse_command_line();
printf "Input: \@n = (%s)\n", join ', ', @n;
my @last = @n;
my @next = $last[ 1 ];
my @triangle = [ @last ];
while (scalar @last > 1)
{
push @next, $next[ $_ - 2 ] + $last[ $_ ] for 2 .. $#last;
push @triangle, [ @next ];
@last = @next;
@next = $last[ 1 ];
}
printf "Output: %d\n", $triangle[ -1 ][ 0 ];
display_triangle( \@triangle ) if $VERBOSE;
}
#------------------------------------------------------------------------------
sub display_triangle
#------------------------------------------------------------------------------
{
my ($triangle) = @_;
# Pre-calculate the maximum width of each column
my @col_widths;
for my $col (0 .. $#{ $triangle->[ 0 ] })
{
$col_widths[ $col ] = length $triangle->[ $col ][ 0 ];
}
# Print the triangle
print "\n";
my $row_idx = 0;
for my $row (@$triangle)
{
print ' ';
for my $i (0 .. $row_idx - 1)
{
printf ' %*s', $col_widths[ $i ], '';
}
for my $j (0 .. $#$row)
{
printf ' %*s', $col_widths[ $j + $row_idx ], $row->[ $j ];
}
print "\n";
++$row_idx;
}
}
#------------------------------------------------------------------------------
sub parse_command_line
#------------------------------------------------------------------------------
{
@ARGV or error( 'No command line arguments found' );
for (@ARGV)
{
/ ^ $RE{num}{int} $ /x
or error( qq["$_" is not a valid integer] );
$_ > 0 or error( qq["$_" is not positive] );
}
return @ARGV;
}
#------------------------------------------------------------------------------
sub error
#------------------------------------------------------------------------------
{
my ($message) = @_;
die "ERROR: $message\n$USAGE";
}
###############################################################################