aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-190/LoneWolfiNTj/perl/ch-1.pl42
-rwxr-xr-xchallenge-190/LoneWolfiNTj/perl/ch-2.pl97
2 files changed, 139 insertions, 0 deletions
diff --git a/challenge-190/LoneWolfiNTj/perl/ch-1.pl b/challenge-190/LoneWolfiNTj/perl/ch-1.pl
new file mode 100755
index 0000000000..2c065c5a0d
--- /dev/null
+++ b/challenge-190/LoneWolfiNTj/perl/ch-1.pl
@@ -0,0 +1,42 @@
+#! /usr/bin/env perl
+
+# PWCC_190_P1_Capital-Correctness_Robbie-Hatley.pl
+
+=pod
+
+Task 1: Capital Detection
+Submitted by: Mohammad S Anwar
+You are given a string with alphabetic characters only: A..Z and a..z.
+Write a script to find out if the usage of Capital is appropriate.
+It's appropriate if it satisfies at least one of the following rules:
+1) Only first letter is capital and all others are small.
+2) Every letter is small.
+3) Every letter is capital.
+
+=cut
+
+# NOTE: Input is via built-in-array (default) or CLI. If using CLI,
+# input should be space-separated sequence of unquoted clusters
+# of English letters (/[a-zA-Z]+/).
+
+# NOTE: Output will be to stdout and will consist of a 0 or 1 for
+# each letter cluster, 0 meaning "inappropriate capital use"
+# and 1 meaning "appropriate capital use".
+
+use v5.36;
+
+sub appropriate ($string)
+{
+ $string =~ m/(^[A-Z][a-z]*$)|(^[a-z]+$)|(^[A-Z]+$)/ and return 1
+ or return 0;
+}
+
+my @array = qw( Perl TPF PyThon raku );
+if (scalar(@ARGV)>0) {@array = @ARGV}
+
+for (@array)
+{
+ my $a = appropriate($_);
+ $a and say "$a - capitalization of $_ is correct."
+ or say "$a - capitalization of $_ is incorrect.";
+}
diff --git a/challenge-190/LoneWolfiNTj/perl/ch-2.pl b/challenge-190/LoneWolfiNTj/perl/ch-2.pl
new file mode 100755
index 0000000000..4c48ebf09c
--- /dev/null
+++ b/challenge-190/LoneWolfiNTj/perl/ch-2.pl
@@ -0,0 +1,97 @@
+#! /usr/bin/env perl
+
+# PWCC_190_P2_Decoded-List_Robbie-Hatley.pl
+
+=pod
+
+You are given an encoded string consisting of a sequence of
+numeric characters (/[0-9]+/). Encoding is simply done by mapping
+A,B,C,D,... to 1,2,3,4,... etc. Write a script to find the all
+valid different decodings in sorted order.
+
+=cut
+
+# NOTE: Input is via built-in-array (default) or CLI. If using CLI,
+# input should be space-separated sequence of unquoted positive
+# integers (/^[1-9][0-9]*$/).
+
+# NOTE: Output will be to stdout and will consist of a sorted List
+# of all possible decodings using partitions of each
+# digit cluster into numbers.
+
+use v5.36;
+
+sub is_positive_integer ($s)
+{
+ $s =~ m/^[1-9][0-9]*$/ and return 1
+ or return 0;
+}
+
+sub are_all_positive_integers_1_26 (@a)
+{
+ for (@a)
+ {
+ return 0 unless is_positive_integer $_ && $_ >= 1 && $_ <= 26;
+ }
+ return 1;
+}
+
+# Return an array of refs to arrays of all possible partitionings
+# of a string into substrings:
+sub string_partitions ($string)
+{
+ my @partitions;
+ my $size = length($string);
+ if ( 0 == $size )
+ {
+ @partitions = ([]);
+ }
+ else
+ {
+ my ($first, $second);
+ for ( my $part = 1 ; $part <= $size ; ++$part )
+ {
+ $first = substr($string, 0, $part );
+ $second = substr($string, $part, $size - $part);
+ my @partials = string_partitions($second);
+ for (@partials) {unshift @$_, $first;}
+ push @partitions, @partials;
+ }
+ }
+ return @partitions;
+}
+
+# Decode an array of positives integers, 1-26, into a text string:
+sub decode (@a)
+{
+ return 'DecodeError' unless are_all_positive_integers_1_26(@a);
+ my $s = ''; # output string
+ foreach my $o (@a){$s.=chr(64+$o);}
+ return $s;
+}
+
+# Default input:
+my @array = qw( 11 1115 127 );
+
+# Non-Default input:
+if (scalar(@ARGV)>0) {@array = @ARGV}
+
+# Set-up output to be ', '-separated:
+$,=', ';
+
+foreach my $s (@array)
+{
+ not is_positive_integer $s # If an input is invalid,
+ and say "$s is not a positive integer" # alert user
+ and next; # and skip that input.
+ my @d = string_partitions($s); # Array of refs to arrays of all possible partitionings of $s (HARD!!!).
+ my $e; # One possible decoding of $s
+ my @e; # Array of all decodings of $s
+ for my $ar (@d)
+ { # For each partitioning of $s,
+ $e = decode(@$ar); # get the corresponding decoding,
+ push(@e,$e) # and push it onto @e,
+ unless $e eq 'DecodeError'; # unless a decode error occurred.
+ }
+ say sort @e; # Print sorted list of valid decodings.
+}