aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Köhler <jean-luc@picard.franken.de>2025-09-22 19:20:15 +0200
committerThomas Köhler <jean-luc@picard.franken.de>2025-09-22 19:20:15 +0200
commit84f87f1f7bdbbdfd62e55acf786bbfca786a60c1 (patch)
treefab04da0c4dcbcfbc5a8682577e5e7fa0c85c10a
parentc4e70544812c339e0344ad3127de18a5dbf98c34 (diff)
downloadperlweeklychallenge-club-84f87f1f7bdbbdfd62e55acf786bbfca786a60c1.tar.gz
perlweeklychallenge-club-84f87f1f7bdbbdfd62e55acf786bbfca786a60c1.tar.bz2
perlweeklychallenge-club-84f87f1f7bdbbdfd62e55acf786bbfca786a60c1.zip
Add solution 340.
Signed-off-by: Thomas Köhler <jean-luc@picard.franken.de>
-rw-r--r--challenge-340/jeanluc2020/blog-1.txt1
-rw-r--r--challenge-340/jeanluc2020/blog-2.txt1
-rwxr-xr-xchallenge-340/jeanluc2020/perl/ch-1.pl84
-rwxr-xr-xchallenge-340/jeanluc2020/perl/ch-2.pl77
4 files changed, 163 insertions, 0 deletions
diff --git a/challenge-340/jeanluc2020/blog-1.txt b/challenge-340/jeanluc2020/blog-1.txt
new file mode 100644
index 0000000000..bb6ca87e44
--- /dev/null
+++ b/challenge-340/jeanluc2020/blog-1.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-340-1.html
diff --git a/challenge-340/jeanluc2020/blog-2.txt b/challenge-340/jeanluc2020/blog-2.txt
new file mode 100644
index 0000000000..6c79cd8c9b
--- /dev/null
+++ b/challenge-340/jeanluc2020/blog-2.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-340-2.html
diff --git a/challenge-340/jeanluc2020/perl/ch-1.pl b/challenge-340/jeanluc2020/perl/ch-1.pl
new file mode 100755
index 0000000000..e554fbc22c
--- /dev/null
+++ b/challenge-340/jeanluc2020/perl/ch-1.pl
@@ -0,0 +1,84 @@
+#!/usr/bin/env perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-340/#TASK1
+#
+# Task 1: Duplicate Removals
+# ==========================
+#
+# You are given a string, $str, consisting of lowercase English letters.
+#
+# Write a script to return the final string after all duplicate removals have been made. Repeat duplicate removals on the given string until we no longer can.
+#
+# # A duplicate removal consists of choosing two adjacent and equal letters and removing them.
+#
+## Example 1
+##
+## Input: $str = 'abbaca'
+## Output: 'ca'
+##
+## Step 1: Remove 'bb' => 'aaca'
+## Step 2: Remove 'aa' => 'ca'
+#
+#
+## Example 2
+##
+## Input: $str = 'azxxzy'
+## Output: 'ay'
+##
+## Step 1: Remove 'xx' => 'azzy'
+## Step 2: Remove 'zz' => 'ay'
+#
+#
+## Example 3
+##
+## Input: $str = 'aaaaaaaa'
+## Output: ''
+##
+## Step 1: Remove 'aa' => 'aaaaaa'
+## Step 2: Remove 'aa' => 'aaaa'
+## Step 3: Remove 'aa' => 'aa'
+## Step 4: Remove 'aa' => ''
+#
+#
+## Example 4
+##
+## Input: $str = 'aabccba'
+## Output: 'a'
+##
+## Step 1: Remove 'aa' => 'bccba'
+## Step 2: Remove 'cc' => 'bba'
+## Step 3: Remove 'bb' => 'a'
+#
+#
+## Example 5
+##
+## Input: $str = 'abcddcba'
+## Output: ''
+##
+## Step 1: Remove 'dd' => 'abccba'
+## Step 2: Remove 'cc' => 'abba'
+## Step 3: Remove 'bb' => 'aa'
+## Step 4: Remove 'aa' => ''
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# We can use a regular expression with backreference: Replace a single
+# character followed by itself with an empty string while this still
+# works.
+
+use v5.36;
+
+duplicate_removals('abbaca');
+duplicate_removals('azxxzy');
+duplicate_removals('aaaaaaaa');
+duplicate_removals('aabccba');
+duplicate_removals('abcddcba');
+
+sub duplicate_removals ($str) {
+ say "Input: $str";
+ while($str =~ s/(.)\1//) { }
+ say "Output: $str";
+}
diff --git a/challenge-340/jeanluc2020/perl/ch-2.pl b/challenge-340/jeanluc2020/perl/ch-2.pl
new file mode 100755
index 0000000000..2527875e86
--- /dev/null
+++ b/challenge-340/jeanluc2020/perl/ch-2.pl
@@ -0,0 +1,77 @@
+#!/usr/bin/env perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-340/#TASK2
+#
+# Task 2: Ascending Numbers
+# =========================
+#
+# You are given a string, $str, is a list of tokens separated by a single
+# space. Every token is either a positive number consisting of digits 0-9 with
+# no leading zeros, or a word consisting of lowercase English letters.
+#
+# Write a script to check if all the numbers in the given string are strictly
+# increasing from left to right.
+#
+## Example 1
+##
+## Input: $str = "The cat has 3 kittens 7 toys 10 beds"
+## Output: true
+##
+## Numbers 3, 7, 10 - strictly increasing.
+#
+#
+## Example 2
+##
+## Input: $str = 'Alice bought 5 apples 2 oranges 9 bananas'
+## Output: false
+#
+#
+## Example 3
+##
+## Input: $str = 'I ran 1 mile 2 days 3 weeks 4 months'
+## Output: true
+#
+#
+## Example 4
+##
+## Input: $str = 'Bob has 10 cars 10 bikes'
+## Output: false
+#
+#
+## Example 5
+##
+## Input: $str = 'Zero is 0 one is 1 two is 2'
+## Output: true
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# We collect all numbers in an array. Then we just start at the
+# beginning and check if each one is less than or equal than the
+# previous one: If it is, we can return false. In the end, we can
+# return true as all numbers were bigger than the previous one.
+
+use v5.36;
+
+ascending_numbers("The cat has 3 kittens 7 toys 10 beds");
+ascending_numbers('Alice bought 5 apples 2 oranges 9 bananas');
+ascending_numbers('I ran 1 mile 2 days 3 weeks 4 months');
+ascending_numbers('Bob has 10 cars 10 bikes');
+ascending_numbers('Zero is 0 one is 1 two is 2');
+
+sub ascending_numbers($str) {
+ say "Input: $str";
+ my @numbers = grep { /^[0-9]+$/ } split / /, $str;
+ my $start = -1;
+ my $result = "true";
+ foreach my $n (@numbers) {
+ if($n <= $start) {
+ $result = "false";
+ last;
+ }
+ $start = $n;
+ }
+ say "Output: $result";
+}