diff options
Diffstat (limited to 'challenge-258')
| -rw-r--r-- | challenge-258/athanasius/perl/ch-1.pl | 170 | ||||
| -rw-r--r-- | challenge-258/athanasius/perl/ch-2.pl | 184 | ||||
| -rw-r--r-- | challenge-258/athanasius/raku/ch-1.raku | 170 | ||||
| -rw-r--r-- | challenge-258/athanasius/raku/ch-2.raku | 176 |
4 files changed, 700 insertions, 0 deletions
diff --git a/challenge-258/athanasius/perl/ch-1.pl b/challenge-258/athanasius/perl/ch-1.pl new file mode 100644 index 0000000000..d228b7b697 --- /dev/null +++ b/challenge-258/athanasius/perl/ch-1.pl @@ -0,0 +1,170 @@ +#!perl + +################################################################################ +=comment + +Perl Weekly Challenge 258 +========================= + +TASK #1 +------- +*Count Even Digits Number* + +Submitted by: Mohammad Sajid Anwar + +You are given a array of positive integers, @ints. + +Write a script to find out how many integers have even number of digits. + +Example 1 + + Input: @ints = (10, 1, 111, 24, 1000) + Output: 3 + + There are 3 integers having even digits i.e. 10, 24 and 1000. + +Example 2 + + Input: @ints = (111, 1, 11111) + Output: 0 + +Example 3 + + Input: @ints = (2, 8, 1024, 256) + Output: 1 + +=cut +################################################################################ + +#--------------------------------------# +# Copyright © 2024 PerlMonk Athanasius # +#--------------------------------------# + +#=============================================================================== +=comment + +Assumption +---------- +"Positive" integers are greater than or equal to zero. + +Interface +--------- +1. If no command-line arguments are given, the test suite is run. Otherwise: +2. If $VERBOSE is set to a true value, the required output is followed by a list + of the even-digit numbers found. + +=cut +#=============================================================================== + +use v5.32.1; # Enables strictures +use warnings; +use Const::Fast; +use Regexp::Common qw( number ); +use Test::More; + +const my $VERBOSE => 1; +const my $USAGE => <<END; +Usage: + perl $0 [<ints> ...] + perl $0 + + [<ints> ...] A non-empty list of positive integers +END + +#------------------------------------------------------------------------------- +BEGIN +#------------------------------------------------------------------------------- +{ + $| = 1; + print "\nChallenge 258, Task #1: Count Even Digits Number (Perl)\n\n"; +} + +#=============================================================================== +MAIN: +#=============================================================================== +{ + if (scalar @ARGV == 0) + { + run_tests(); + } + else + { + my @ints = @ARGV; + + for (@ints) + { + / ^ $RE{num}{int} $ /x or error( qq["$_" is not a valid integer] ); + $_ >= 0 or error( "$_ is negative" ); + } + + printf "Input: \@ints = (%s)\n", join ', ', @ints; + + my $evens = find_even_digit_numbers( \@ints ); + + printf "Output: %d\n", scalar @$evens; + + if ($VERBOSE) + { + printf "\nEven-digit integers: %s\n", + join ', ', sort { $a <=> $b } @$evens; + } + } +} + +#------------------------------------------------------------------------------- +sub find_even_digit_numbers +#------------------------------------------------------------------------------- +{ + my ($ints) = @_; + my @evens; + + for my $i (@$ints) + { + push @evens, $i if length( $i ) % 2 == 0; + } + + return \@evens; +} + +#------------------------------------------------------------------------------- +sub run_tests +#------------------------------------------------------------------------------- +{ + print "Running the test suite\n"; + + while (my $line = <DATA>) + { + chomp $line; + + my ($test_name, $int_str, $expected) = split / \| /x, $line; + + for ($test_name, $int_str, $expected) + { + s/ ^ \s+ //x; + s/ \s+ $ //x; + } + + my @ints = split / \s+ /x, $int_str; + my $evens = find_even_digit_numbers( \@ints ); + + is scalar @$evens, $expected, $test_name; + } + + done_testing; +} + +#------------------------------------------------------------------------------- +sub error +#------------------------------------------------------------------------------- +{ + my ($message) = @_; + + die "ERROR: $message\n$USAGE"; +} + +################################################################################ + +__DATA__ +Example 1| 10 1 111 24 1000|3 +Example 2|111 1 11111 |0 +Example 3| 2 8 1024 256 |1 diff --git a/challenge-258/athanasius/perl/ch-2.pl b/challenge-258/athanasius/perl/ch-2.pl new file mode 100644 index 0000000000..1bd1d08382 --- /dev/null +++ b/challenge-258/athanasius/perl/ch-2.pl @@ -0,0 +1,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 diff --git a/challenge-258/athanasius/raku/ch-1.raku b/challenge-258/athanasius/raku/ch-1.raku new file mode 100644 index 0000000000..9a3482ddda --- /dev/null +++ b/challenge-258/athanasius/raku/ch-1.raku @@ -0,0 +1,170 @@ +use v6d; + +################################################################################ +=begin comment + +Perl Weekly Challenge 258 +========================= + +TASK #1 +------- +*Count Even Digits Number* + +Submitted by: Mohammad Sajid Anwar + +You are given a array of positive integers, @ints. + +Write a script to find out how many integers have even number of digits. + +Example 1 + + Input: @ints = (10, 1, 111, 24, 1000) + Output: 3 + + There are 3 integers having even digits i.e. 10, 24 and 1000. + +Example 2 + + Input: @ints = (111, 1, 11111) + Output: 0 + +Example 3 + + Input: @ints = (2, 8, 1024, 256) + Output: 1 + +=end comment +################################################################################ + +#--------------------------------------# +# Copyright © 2024 PerlMonk Athanasius # +#--------------------------------------# + +#=============================================================================== +=begin comment + +Assumption +---------- +"Positive" integers are greater than or equal to zero. + +Interface +--------- +1. If no command-line arguments are given, the test suite is run. Otherwise: +2. If VERBOSE is set to True, the required output is followed by a list of the + even-digit numbers found. + +=end comment +#=============================================================================== + +use Test; + +my Bool constant VERBOSE = True; + +#------------------------------------------------------------------------------- +BEGIN +#------------------------------------------------------------------------------- +{ + "\nChallenge 258, Task #1: Count Even Digits Number (Raku)\n".put; +} + +#=============================================================================== +multi sub MAIN +( + #| A non-empty list of positive integers + + *@ints where { .elems > 0 && .all ~~ UInt:D } +) +#=============================================================================== +{ + "Input: \@ints = (%s)\n".printf: @ints.join: ', '; + + my UInt @evens = find-even-digit-numbers( @ints ); + + "Output: %d\n".printf: @evens.elems; + + if VERBOSE + { + "\nEven-digit integers: %s\n".printf: @evens.sort.join: ', '; + } +} + +#=============================================================================== +multi sub MAIN() # No input: run the test suite +#=============================================================================== +{ + run-tests(); +} + +#------------------------------------------------------------------------------- +sub find-even-digit-numbers( List:D[UInt:D] $ints --> List:D[UInt:D] ) +#------------------------------------------------------------------------------- +{ + my UInt @evens; + + for @$ints -> UInt $i + { + @evens.push: $i if $i.chars %% 2; + } + + return @evens; +} + +#------------------------------------------------------------------------------- +sub run-tests() +#------------------------------------------------------------------------------- +{ + 'Running the test suite'.put; + + for test-data.lines -> Str $line + { + my Str ($test-name, $int-str, $expected) = $line.split: / \| /; + + for $test-name, $int-str, $expected + { + s/ ^ \s+ //; + s/ \s+ $ //; + } + + my Int @ints = ($int-str.split: / \s+ /, :skip-empty).map: { .Int }; + my UInt @evens = find-even-digit-numbers( @ints ); + + is @evens.elems, $expected.Int, $test-name; + } + + done-testing; +} + +#------------------------------------------------------------------------------- +sub error( Str:D $message ) +#------------------------------------------------------------------------------- +{ + "ERROR: $message".put; + + USAGE(); + + exit 0; +} + +#------------------------------------------------------------------------------- +sub USAGE() +#------------------------------------------------------------------------------- +{ + my Str $usage = $*USAGE; + + $usage ~~ s:g/ ($*PROGRAM-NAME) /raku $0/; + + $usage.put; +} + +#------------------------------------------------------------------------------- +sub test-data( --> Str:D ) +#------------------------------------------------------------------------------- +{ + return q:to/END/; + Example 1| 10 1 111 24 1000|3 + Example 2|111 1 11111 |0 + Example 3| 2 8 1024 256 |1 + END +} + +################################################################################ diff --git a/challenge-258/athanasius/raku/ch-2.raku b/challenge-258/athanasius/raku/ch-2.raku new file mode 100644 index 0000000000..d188004136 --- /dev/null +++ b/challenge-258/athanasius/raku/ch-2.raku @@ -0,0 +1,176 @@ +use v6d; + +################################################################################ +=begin 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 + +=end comment +################################################################################ + +#--------------------------------------# +# Copyright © 2024 PerlMonk Athanasius # +#--------------------------------------# + +#=============================================================================== +=begin 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. + +=end comment +#=============================================================================== + +use Test; + +#------------------------------------------------------------------------------- +BEGIN +#------------------------------------------------------------------------------- +{ + "\nChallenge 258, Task #2: Sum of Values (Raku)\n".put; +} + +#=============================================================================== +multi sub MAIN +( + UInt:D $k, #= Target number of 1-digits in each index + + *@ints where { .elems > 0 && .all ~~ Int:D } #= Non-empty list of integers +) +#=============================================================================== +{ + "Input: \@ints = (%s), \$k = %d\n".printf: @ints.join( ', ' ), $k; + + my Int $sum = find-sum( @ints, $k ); + + "Output: $sum".put; +} + +#=============================================================================== +multi sub MAIN() # No input: run the test suite +#=============================================================================== +{ + run-tests(); +} + +#------------------------------------------------------------------------------- +sub find-sum( List:D[Int:D] $ints, UInt:D $k --> Int:D ) +#------------------------------------------------------------------------------- +{ + my Int $sum = 0; + + for 0 .. $ints.end -> UInt $i + { + my Str $binary = '%b'.sprintf: $i; + my UInt $one-bits = +( tr/1/*/ with $binary ); + + $sum += $ints[ $i ] if $one-bits == $k; + } + + return $sum; +} + +#------------------------------------------------------------------------------- +sub run-tests() +#------------------------------------------------------------------------------- +{ + 'Running the test suite'.put; + + for test-data.lines -> Str $line + { + my Str ($test-name, $int-str, $k-str, $exp-str) = $line.split: / \| /; + + for $test-name, $int-str, $k-str, $exp-str + { + s/ ^ \s+ //; + s/ \s+ $ //; + } + + my Int @ints = $int-str.split( / \s+ /, :skip-empty ).map: { .Int }; + my Int $sum = find-sum( @ints, $k-str.Int ); + + is $sum, $exp-str.Int, $test-name; + } + + done-testing; +} + +#------------------------------------------------------------------------------- +sub error( Str:D $message ) +#------------------------------------------------------------------------------- +{ + "ERROR: $message".put; + + USAGE(); + + exit 0; +} + +#------------------------------------------------------------------------------- +sub USAGE() +#------------------------------------------------------------------------------- +{ + my Str $usage = $*USAGE; + + $usage ~~ s:g/ ($*PROGRAM-NAME) /raku $0/; + + $usage.put; +} + +#------------------------------------------------------------------------------- +sub test-data( --> Str:D ) +#------------------------------------------------------------------------------- +{ + return q:to/END/; + 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 + END +} + +################################################################################ |
