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
|
#!perl
################################################################################
=comment
Perl Weekly Challenge 258
=========================
TASK #2
-------
*Sum of Values*
Submitted by: Mohammad Sajid Anwar
You are given an array of integers, @int and an integer $k.
Write a script to find the sum of values whose index binary representation has
exactly $k number of 1-bit set.
Example 1
Input: @ints = (2, 5, 9, 11, 3), $k = 1
Output: 17
Binary representation of index 0 = 0
Binary representation of index 1 = 1
Binary representation of index 2 = 10
Binary representation of index 3 = 11
Binary representation of index 4 = 100
So the indices 1, 2 and 4 have total one 1-bit sets.
Therefore the sum, $ints[1] + $ints[2] + $ints[3] = 17
Example 2
Input: @ints = (2, 5, 9, 11, 3), $k = 2
Output: 11
Example 3
Input: @ints = (2, 5, 9, 11, 3), $k = 0
Output: 2
=cut
################################################################################
#--------------------------------------#
# Copyright © 2024 PerlMonk Athanasius #
#--------------------------------------#
#===============================================================================
=comment
Assumption
----------
$k is greater than or equal to zero.
Interface
---------
1. If no command-line arguments are given, the test suite is run. Otherwise:
2. $k is given as the first command-line argument, followed by one or more
elements of @ints.
=cut
#===============================================================================
use v5.32.1; # Enables strictures
use warnings;
use Const::Fast;
use Regexp::Common qw( number );
use Test::More;
const my $USAGE => <<END;
Usage:
perl $0 <k> [<ints> ...]
perl $0
<k> Target number of 1-digits in each index
[<ints> ...] Non-empty list of integers
END
#-------------------------------------------------------------------------------
BEGIN
#-------------------------------------------------------------------------------
{
$| = 1;
print "\nChallenge 258, Task #2: Sum of Values (Perl)\n\n";
}
#===============================================================================
MAIN:
#===============================================================================
{
my $argc = scalar @ARGV;
if ($argc == 0)
{
run_tests();
}
elsif ($argc == 1)
{
error( 'Expected 0 or 2+ command-line arguments, found 1' );
}
else
{
my ($k, @ints) = @ARGV;
for ($k, @ints)
{
/ ^ $RE{num}{int} $ /x or error( qq["$_" is not a valid integer] );
}
$k >= 0 or error( '$k is negative' );
printf "Input: \@ints = (%s), \$k = %d\n", join( ', ', @ints ), $k;
my $sum = find_sum( \@ints, $k );
print "Output: $sum\n";
}
}
#-------------------------------------------------------------------------------
sub find_sum
#-------------------------------------------------------------------------------
{
my ($ints, $k) = @_;
my $sum = 0;
for my $i (0 .. $#$ints)
{
my $binary = sprintf '%b', $i;
my $one_bits = $binary =~ tr/1/1/;
$sum += $ints->[ $i ] if $one_bits == $k;
}
return $sum;
}
#-------------------------------------------------------------------------------
sub run_tests
#-------------------------------------------------------------------------------
{
print "Running the test suite\n";
while (my $line = <DATA>)
{
chomp $line;
my ($test_name, $int_str, $k, $expected) = split / \| /x, $line;
for ($test_name, $int_str, $k, $expected)
{
s/ ^ \s+ //x;
s/ \s+ $ //x;
}
my @ints = split / \s+ /x, $int_str;
my $sum = find_sum( \@ints, $k );
is $sum, $expected, $test_name;
}
done_testing;
}
#-------------------------------------------------------------------------------
sub error
#-------------------------------------------------------------------------------
{
my ($message) = @_;
die "ERROR: $message\n$USAGE";
}
################################################################################
__DATA__
Example 1| 2 5 9 11 3|1|17
Example 2| 2 5 9 11 3|2|11
Example 3| 2 5 9 11 3|0| 2
Singleton|42 |0|42
Negatives|-1 -3 -5 -7 |1|-8
|