diff options
| author | PerlMonk-Athanasius <PerlMonk.Athanasius@gmail.com> | 2025-07-13 23:38:44 +1000 |
|---|---|---|
| committer | PerlMonk-Athanasius <PerlMonk.Athanasius@gmail.com> | 2025-07-13 23:38:44 +1000 |
| commit | c0b103f8212853b3f4ecb04caa996cbf1f65f2b7 (patch) | |
| tree | b10eb186777639875ce40e002eddcf2425d80e04 | |
| parent | 67f8df57472ca970259dcaa18a6b61f400df7ff2 (diff) | |
| download | perlweeklychallenge-club-c0b103f8212853b3f4ecb04caa996cbf1f65f2b7.tar.gz perlweeklychallenge-club-c0b103f8212853b3f4ecb04caa996cbf1f65f2b7.tar.bz2 perlweeklychallenge-club-c0b103f8212853b3f4ecb04caa996cbf1f65f2b7.zip | |
Perl & Raku solutions to Task 1 for Week 329
| -rw-r--r-- | challenge-329/athanasius/perl/ch-1.pl | 163 | ||||
| -rw-r--r-- | challenge-329/athanasius/raku/ch-1.raku | 152 |
2 files changed, 315 insertions, 0 deletions
diff --git a/challenge-329/athanasius/perl/ch-1.pl b/challenge-329/athanasius/perl/ch-1.pl new file mode 100644 index 0000000000..1f0a22c4e4 --- /dev/null +++ b/challenge-329/athanasius/perl/ch-1.pl @@ -0,0 +1,163 @@ +#!perl + +################################################################################ +=comment + +Perl Weekly Challenge 329 +========================= + +TASK #1 +------- +*Counter Integers* + +Submitted by: Mohammad Sajid Anwar + +You are given a string containing only lower case English letters and digits. + +Write a script to replace every non-digit character with a space and then return +all the distinct integers left. + +Example 1 + + Input: $str = "the1weekly2challenge2" + Output: 1, 2 + + 2 is appeared twice, so we count it one only. + +Example 2 + + Input: $str = "go21od1lu5c7k" + Output: 21, 1, 5, 7 + +Example 3 + + Input: $str = "4p3e2r1l" + Output: 4, 3, 2, 1 + +=cut +################################################################################ + +#--------------------------------------# +# Copyright © 2025 PerlMonk Athanasius # +#--------------------------------------# + +#=============================================================================== +=comment + +Interface +--------- +1. If no command-line arguments are given, the test suite is run. Otherwise: +2. A string containing only digits and lower-case English letters is entered on + the command-line. + +=cut +#=============================================================================== + +use v5.32; # Enables strictures +use warnings; +use Const::Fast; +use Test::More; + +const my $USAGE => <<END; +Usage: + perl $0 <str> + perl $0 + + <str> A string containing only digits and lower-case English letters +END + +#------------------------------------------------------------------------------- +BEGIN +#------------------------------------------------------------------------------- +{ + $| = 1; + print "\nChallenge 329, Task #1: Counter Integers (Perl)\n\n"; +} + +#=============================================================================== +MAIN: +#=============================================================================== +{ + my $argc = scalar @ARGV; + + if ($argc == 0) + { + run_tests(); + } + elsif ($argc == 1) + { + my $str = $ARGV[ 0 ]; + + $str =~ / ([^a-z0-9]) /x and error( qq[Invalid character "$1"] ); + + print qq[Input: \$str = "$str"\n]; + + my $ints = find_ints( $str ); + + printf "Output: %s\n", join ', ', @$ints; + } + else + { + error( "Expected 1 or 0 command-line arguments, found $argc" ); + } +} + +#------------------------------------------------------------------------------- +sub find_ints +#------------------------------------------------------------------------------- +{ + my ($str) = @_; + $str =~ / ([^a-z0-9]) /x and die qq[Invalid character "$1"]; + + my (@ints, %dict); + + for my $int (grep { length } split / [^0-9]+ /x, $str) + { + push @ints, $int if ++$dict{ $int } == 1; + } + + return \@ints; +} + +#------------------------------------------------------------------------------- +sub run_tests +#------------------------------------------------------------------------------- +{ + print "Running the test suite\n"; + + while (my $line = <DATA>) + { + chomp $line; + + my ($test_name, $str, $expected_str) = split / \| /x, $line; + + for ($test_name, $str, $expected_str) + { + s/ ^ \s+ //x; + s/ \s+ $ //x; + } + + my $ints = find_ints( $str ); + my @exp = split / \s+ /x, $expected_str; + + is_deeply $ints, \@exp, $test_name; + } + + done_testing; +} + +#------------------------------------------------------------------------------- +sub error +#------------------------------------------------------------------------------- +{ + my ($message) = @_; + + die "ERROR: $message\n$USAGE"; +} + +################################################################################ + +__DATA__ +Example 1|the1weekly2challenge2| 1 2 +Example 2|go21od1lu5c7k |21 1 5 7 +Example 3|4p3e2r1l | 4 3 2 1 diff --git a/challenge-329/athanasius/raku/ch-1.raku b/challenge-329/athanasius/raku/ch-1.raku new file mode 100644 index 0000000000..31510bbebe --- /dev/null +++ b/challenge-329/athanasius/raku/ch-1.raku @@ -0,0 +1,152 @@ +use v6d; + +################################################################################ +=begin comment + +Perl Weekly Challenge 329 +========================= + +TASK #1 +------- +*Counter Integers* + +Submitted by: Mohammad Sajid Anwar + +You are given a string containing only lower case English letters and digits. + +Write a script to replace every non-digit character with a space and then return +all the distinct integers left. + +Example 1 + + Input: $str = "the1weekly2challenge2" + Output: 1, 2 + + 2 is appeared twice, so we count it one only. + +Example 2 + + Input: $str = "go21od1lu5c7k" + Output: 21, 1, 5, 7 + +Example 3 + + Input: $str = "4p3e2r1l" + Output: 4, 3, 2, 1 + +=end comment +################################################################################ + +#--------------------------------------# +# Copyright © 2025 PerlMonk Athanasius # +#--------------------------------------# + +#=============================================================================== +=begin comment + +Interface +--------- +1. If no command-line arguments are given, the test suite is run. Otherwise: +2. A string containing only digits and lower-case English letters is entered on + the command-line. + +=end comment +#=============================================================================== + +use Test; + +#------------------------------------------------------------------------------- +BEGIN +#------------------------------------------------------------------------------- +{ + "\nChallenge 329, Task #1: Counter Integers (Raku)\n".put; +} + +#=============================================================================== +multi sub MAIN +( + #| A string containing only digits and lower-case English letters + + Str:D $str where / ^ <[ a..z 0..9 ]>* $ / +) +#=============================================================================== +{ + qq[Input: \$str = "$str"].put; + + my UInt @ints = find-ints( $str ); + + qq[Output: %s\n].printf: @ints.join: ', '; +} + +#=============================================================================== +multi sub MAIN() # No input: run the test suite +#=============================================================================== +{ + run-tests(); +} + +#------------------------------------------------------------------------------- +sub find-ints( Str:D $str where / ^ <[ a..z 0..9 ]>* $ / --> List:D[UInt:D] ) +#------------------------------------------------------------------------------- +{ + my UInt @ints; + my UInt %dict{UInt}; + + for $str.split: / <-[ 0..9 ]>+ /, :skip-empty -> Str $dig-str + { + my UInt $int = +$dig-str; + + @ints.push: $int if ++%dict{ $int } == 1; + } + + return @ints; +} + +#------------------------------------------------------------------------------- +sub run-tests() +#------------------------------------------------------------------------------- +{ + 'Running the test suite'.put; + + for test-data.lines -> Str $line + { + my Str ($test-name, $str, $expected-str) = $line.split: / \| /; + + for $test-name, $str, $expected-str + { + s/ ^ \s+ //; + s/ \s+ $ //; + } + + my UInt @ints = find-ints( $str ); + my UInt @exp = $expected-str.split( / \s+ / ).map: { .Int }; + + is-deeply @ints, @exp, $test-name; + } + + done-testing; +} + +#------------------------------------------------------------------------------- +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|the1weekly2challenge2| 1 2 + Example 2|go21od1lu5c7k |21 1 5 7 + Example 3|4p3e2r1l | 4 3 2 1 + END +} + +################################################################################ |
