diff options
| -rw-r--r-- | challenge-280/athanasius/perl/ch-1.pl | 165 | ||||
| -rw-r--r-- | challenge-280/athanasius/perl/ch-2.pl | 161 | ||||
| -rw-r--r-- | challenge-280/athanasius/raku/ch-1.raku | 165 | ||||
| -rw-r--r-- | challenge-280/athanasius/raku/ch-2.raku | 161 |
4 files changed, 652 insertions, 0 deletions
diff --git a/challenge-280/athanasius/perl/ch-1.pl b/challenge-280/athanasius/perl/ch-1.pl new file mode 100644 index 0000000000..759c9b3941 --- /dev/null +++ b/challenge-280/athanasius/perl/ch-1.pl @@ -0,0 +1,165 @@ +#!perl + +################################################################################ +=comment + +Perl Weekly Challenge 280 +========================= + +TASK #1 +------- +*Twice Appearance* + +Submitted by: Mohammad Sajid Anwar + +You are given a string, $str, containing lowercase English letters only. + +Write a script to print the first letter that appears twice. + +Example 1 + + Input: $str = "acbddbca" + Output: "d" + +Example 2 + + Input: $str = "abccd" + Output: "c" + +Example 3 + + Input: $str = "abcdabbb" + Output: "a" + +=cut +################################################################################ + +#--------------------------------------# +# Copyright © 2024 PerlMonk Athanasius # +#--------------------------------------# + +#=============================================================================== +=comment + +Interface +--------- +1. If no command-line arguments are given, the test suite is run. +Otherwise: +2. The input string 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 of lower case letters +END + +#------------------------------------------------------------------------------- +BEGIN +#------------------------------------------------------------------------------- +{ + $| = 1; + print "\nChallenge 280, Task #1: Twice Appearance (Perl)\n\n"; +} + +#=============================================================================== +MAIN: +#=============================================================================== +{ + my $argc = scalar @ARGV; + + if ($argc == 0) + { + run_tests(); + } + elsif ($argc == 1) + { + my $str = $ARGV[ 0 ]; + + $str =~ / ^ [a-z]* $ /x or error( qq[Invalid input string "$str"] ); + + print qq[Input: \$str = "$str"\n]; + + my $frl = find_first_repeated_letter( $str ); + + printf "Output: %s\n", $frl eq '' ? '<none>' : qq["$frl"]; + } + else + { + error( "Expected 1 or 0 command-line arguments, found $argc" ); + } +} + +#------------------------------------------------------------------------------- +sub find_first_repeated_letter +#------------------------------------------------------------------------------- +{ + my ($str) = @_; + $str =~ / ^ [a-z]* $ /x or die 'Invalid string'; + my $frl = ''; + my %dict; + + for my $letter (split //, $str) + { + if (++$dict{ $letter } > 1) + { + $frl = $letter; + last; + } + } + + return $frl; +} + +#------------------------------------------------------------------------------- +sub run_tests +#------------------------------------------------------------------------------- +{ + print "Running the test suite\n"; + + while (my $line = <DATA>) + { + chomp $line; + + my ($test_name, $str, $expected) = split / \| /x, $line; + + for ($test_name, $str, $expected) + { + s/ ^ \s+ //x; + s/ \s+ $ //x; + } + + my $frl = find_first_repeated_letter( $str ); + + is $frl, $expected, $test_name; + } + + done_testing; +} + +#------------------------------------------------------------------------------- +sub error +#------------------------------------------------------------------------------- +{ + my ($message) = @_; + + die "ERROR: $message\n$USAGE"; +} + +################################################################################ + +__DATA__ +Example 1 |acbddbca |d +Example 2 |abccd |c +Example 3 |abcdabbb |a +Empty | | +Singletons|abcdefghijklmnop| diff --git a/challenge-280/athanasius/perl/ch-2.pl b/challenge-280/athanasius/perl/ch-2.pl new file mode 100644 index 0000000000..a1cdb18c79 --- /dev/null +++ b/challenge-280/athanasius/perl/ch-2.pl @@ -0,0 +1,161 @@ +#!perl + +################################################################################ +=comment + +Perl Weekly Challenge 280 +========================= + +TASK #2 +------- +*Count Asterisks* + +Submitted by: Mohammad Sajid Anwar + +You are given a string, $str, where every two consecutive vertical bars are +grouped into a pair. + +Write a script to return the number of asterisks, *, excluding any between each +pair of vertical bars. + +Example 1 + + Input: $str = "p|*e*rl|w**e|*ekly|" + Output: 2 + + The characters we are looking here are "p" and "w**e". + +Example 2 + + Input: $str = "perl" + Output: 0 + +Example 3 + + Input: $str = "th|ewe|e**|k|l***ych|alleng|e" + Output: 5 + + The characters we are looking here are "th", "e**", "l***ych" and "e". + +=cut +################################################################################ + +#--------------------------------------# +# Copyright © 2024 PerlMonk Athanasius # +#--------------------------------------# + +#=============================================================================== +=comment + +Interface +--------- +1. If no command-line arguments are given, the test suite is run. +Otherwise: +2. A single string is entered on the command-line. + +=cut +#=============================================================================== + +use v5.32; # Enables strictures and warnings +use Const::Fast; +use Test::More; + +const my $USAGE => <<END; +Usage: + perl $0 <str> + perl $0 + + <str> A string +END + +#------------------------------------------------------------------------------- +BEGIN +#------------------------------------------------------------------------------- +{ + $| = 1; + print "\nChallenge 280, Task #2: Count Asterisks (Perl)\n\n"; +} + +#=============================================================================== +MAIN: +#=============================================================================== +{ + my $argc = scalar @ARGV; + + if ($argc == 0) + { + run_tests(); + } + elsif ($argc == 1) + { + my $str = $ARGV[ 0 ]; + + print qq[Input: \$str = "$str"\n]; + + my $asterisks = count_asterisks( $str ); + + print "Output: $asterisks\n"; + } + else + { + error( "Expected 1 or 0 command-line arguments, found $argc" ); + } +} + +#------------------------------------------------------------------------------- +sub count_asterisks +#------------------------------------------------------------------------------- +{ + my ($str) = @_; + + # 1. Remove all bar-pairs (including any asterisks they contain) + + $str =~ s/ \| [^|]* \| //gx; + + # 2. Return the number of asterisks remaining + + return $str =~ tr/*//; +} + +#------------------------------------------------------------------------------- +sub run_tests +#------------------------------------------------------------------------------- +{ + print "Running the test suite\n"; + + while (my $line = <DATA>) + { + chomp $line; + + my ($test_name, $str, $expected) = split / \! /x, $line; + + for ($test_name, $str, $expected) + { + s/ ^ \s+ //x; + s/ \s+ $ //x; + } + + my $asterisks = count_asterisks( $str ); + + is $asterisks, $expected, $test_name; + } + + done_testing; +} + +#------------------------------------------------------------------------------- +sub error +#------------------------------------------------------------------------------- +{ + my ($message) = @_; + + die "ERROR: $message\n$USAGE"; +} + +################################################################################ + +__DATA__ +Example 1 ! p|*e*rl|w**e|*ekly| ! 2 +Example 2 ! perl ! 0 +Example 3 ! th|ewe|e**|k|l***ych|alleng|e ! 5 +Unmatched bar ! a*a|b**b|c***|d****d ! 8 diff --git a/challenge-280/athanasius/raku/ch-1.raku b/challenge-280/athanasius/raku/ch-1.raku new file mode 100644 index 0000000000..f03a8b68ef --- /dev/null +++ b/challenge-280/athanasius/raku/ch-1.raku @@ -0,0 +1,165 @@ +use v6d; + +################################################################################ +=begin comment + +Perl Weekly Challenge 280 +========================= + +TASK #1 +------- +*Twice Appearance* + +Submitted by: Mohammad Sajid Anwar + +You are given a string, $str, containing lowercase English letters only. + +Write a script to print the first letter that appears twice. + +Example 1 + + Input: $str = "acbddbca" + Output: "d" + +Example 2 + + Input: $str = "abccd" + Output: "c" + +Example 3 + + Input: $str = "abcdabbb" + Output: "a" + +=end comment +################################################################################ + +#--------------------------------------# +# Copyright © 2024 PerlMonk Athanasius # +#--------------------------------------# + +#=============================================================================== +=begin comment + +Interface +--------- +1. If no command-line arguments are given, the test suite is run. +Otherwise: +2. The input string is entered on the command-line. + +=end comment +#=============================================================================== + +use Test; + +#------------------------------------------------------------------------------- +BEGIN +#------------------------------------------------------------------------------- +{ + "\nChallenge 280, Task #1: Twice Appearance (Raku)\n".put; +} + +#=============================================================================== +multi sub MAIN +( + Str:D $str where / ^ <[ a .. z ]>* $ / #= A string of lower case letters +) +#=============================================================================== +{ + qq[Input: \$str = "$str"].put; + + my Str $frl = find-first-repeated-letter( $str ); + + "Output: %s\n".printf: $frl eq '' ?? '<none>' !! qq["$frl"]; +} + +#=============================================================================== +multi sub MAIN() # No input: run the test suite +#=============================================================================== +{ + run-tests(); +} + +#------------------------------------------------------------------------------- +sub find-first-repeated-letter +( + Str:D $str where / ^ <[ a .. z ]>* $ / +--> Str:D +) +#------------------------------------------------------------------------------- +{ + my Str $frl = ''; + my UInt %dict{Str}; + + for $str.split( '', :skip-empty ) -> Str $letter + { + if ++%dict{ $letter } > 1 + { + $frl = $letter; + last; + } + } + + return $frl; +} + +#------------------------------------------------------------------------------- +sub run-tests() +#------------------------------------------------------------------------------- +{ + 'Running the test suite'.put; + + for test-data.lines -> Str $line + { + my Str ($test-name, $str, $expected) = $line.split: / \| /; + + for $test-name, $str, $expected + { + s/ ^ \s+ //; + s/ \s+ $ //; + } + + my Str $frl = find-first-repeated-letter( $str ); + + is $frl, $expected, $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 |acbddbca |d + Example 2 |abccd |c + Example 3 |abcdabbb |a + Empty | | + Singletons|abcdefghijklmnop| + END +} + +################################################################################ diff --git a/challenge-280/athanasius/raku/ch-2.raku b/challenge-280/athanasius/raku/ch-2.raku new file mode 100644 index 0000000000..f843c71ada --- /dev/null +++ b/challenge-280/athanasius/raku/ch-2.raku @@ -0,0 +1,161 @@ +use v6d; + +################################################################################ +=begin comment + +Perl Weekly Challenge 280 +========================= + +TASK #2 +------- +*Count Asterisks* + +Submitted by: Mohammad Sajid Anwar + +You are given a string, $str, where every two consecutive vertical bars are +grouped into a pair. + +Write a script to return the number of asterisks, *, excluding any between each +pair of vertical bars. + +Example 1 + + Input: $str = "p|*e*rl|w**e|*ekly|" + Output: 2 + + The characters we are looking here are "p" and "w**e". + +Example 2 + + Input: $str = "perl" + Output: 0 + +Example 3 + + Input: $str = "th|ewe|e**|k|l***ych|alleng|e" + Output: 5 + + The characters we are looking here are "th", "e**", "l***ych" and "e". + +=end comment +################################################################################ + +#--------------------------------------# +# Copyright © 2024 PerlMonk Athanasius # +#--------------------------------------# + +#=============================================================================== +=begin comment + +Interface +--------- +1. If no command-line arguments are given, the test suite is run. +Otherwise: +2. A single string is entered on the command-line. + +=end comment +#=============================================================================== + +use Test; + +#------------------------------------------------------------------------------- +BEGIN +#------------------------------------------------------------------------------- +{ + "\nChallenge 280, Task #2: Count Asterisks (Raku)\n".put; +} + +#=============================================================================== +multi sub MAIN +( + Str:D $str #= A string +) +#=============================================================================== +{ + qq[Input: \$str = "$str"].put; + + my UInt $asterisks = count-asterisks( $str ); + + "Output: $asterisks".put; +} + +#=============================================================================== +multi sub MAIN() # No input: run the test suite +#=============================================================================== +{ + run-tests(); +} + +#------------------------------------------------------------------------------- +sub count-asterisks( Str:D $str --> UInt:D ) +#------------------------------------------------------------------------------- +{ + # 1. Make of copy of $str with all bar-pairs (including any asterisks they + # contain) removed + + my Str $s = S:g/ \| <-[ | ]>* \| // with $str; + + # 2. Return the number of asterisks remaining + + return +( $s ~~ tr/*// ); +} + +#------------------------------------------------------------------------------- +sub run-tests() +#------------------------------------------------------------------------------- +{ + 'Running the test suite'.put; + + for test-data.lines -> Str $line + { + my Str ($test-name, $str, $expected) = $line.split: / \! /; + + for $test-name, $str, $expected + { + s/ ^ \s+ //; + s/ \s+ $ //; + } + + my UInt $asterisks = count-asterisks( $str ); + + is $asterisks, $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 ! p|*e*rl|w**e|*ekly| ! 2 + Example 2 ! perl ! 0 + Example 3 ! th|ewe|e**|k|l***ych|alleng|e ! 5 + Unmatched bar ! a*a|b**b|c***|d****d ! 8 + END +} + +################################################################################ |
