diff options
| -rw-r--r-- | challenge-340/zapwai/perl/ch-1.pl | 39 | ||||
| -rw-r--r-- | challenge-340/zapwai/perl/ch-2.pl | 26 |
2 files changed, 65 insertions, 0 deletions
diff --git a/challenge-340/zapwai/perl/ch-1.pl b/challenge-340/zapwai/perl/ch-1.pl new file mode 100644 index 0000000000..8798a6dc8c --- /dev/null +++ b/challenge-340/zapwai/perl/ch-1.pl @@ -0,0 +1,39 @@ +use v5.38; + +sub strip($s) { + my @l = split '', $s; + my $bad_index = "-1"; + for my $i (0 .. $#l - 1) { + if ($l[$i] eq $l[$i+1]) { + $bad_index = $i; + last; + } + } + if ($bad_index == -1) { + return $s; + } else { + splice @l, $bad_index, 2; + return join '', @l; + } +} + +sub proc($s) { + say "Input: $s"; + while (1) { + my $pre = $s; + $s = strip($s); + last if ($pre eq $s); + } + say "Output: $s"; +} + +my $str = "abbaca"; +proc($str); +$str = "azxxzy"; +proc($str); +$str = "aaaaaaaa"; +proc($str); +$str = "aabccba"; +proc($str); +$str = "abcddcba"; +proc($str); diff --git a/challenge-340/zapwai/perl/ch-2.pl b/challenge-340/zapwai/perl/ch-2.pl new file mode 100644 index 0000000000..2982ccf0ea --- /dev/null +++ b/challenge-340/zapwai/perl/ch-2.pl @@ -0,0 +1,26 @@ +use v5.38; + +sub proc($s) { + say "Input: $s"; + my @w = split " ", $s; + my @n; + for my $w (@w) { + push @n, $w if ($w =~ /^\d+$/); + } + my $o = "true"; + for my $i (0 .. $#n - 1) { + $o = "false" if ($n[$i+1] <= $n[$i]); + } + say "Output: $o"; +} + +my $str = "The cat has 3 kittens 7 toys 10 beds"; +proc($str); +$str = "Alice bought 5 apples 2 oranges 9 bananas"; +proc($str); +$str = "I ran 1 mile 2 days 3 weeks 4 months"; +proc($str); +$str = "Bob has 10 cars 10 bikes"; +proc($str); +$str = "Zero is 0 one is 1 two is 2"; +proc($str); |
