aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Köhler <jean-luc@picard.franken.de>2024-07-29 09:43:23 +0200
committerThomas Köhler <jean-luc@picard.franken.de>2024-07-29 09:43:23 +0200
commit1c3a6af8c70de4e8b8c8f54167f013cdffcd0829 (patch)
tree01d16837ca5e9df7e29e8d6fade8c5dd51b665a9
parentb9587166580480d2f5dddce64bfcb8d33ef4e127 (diff)
downloadperlweeklychallenge-club-1c3a6af8c70de4e8b8c8f54167f013cdffcd0829.tar.gz
perlweeklychallenge-club-1c3a6af8c70de4e8b8c8f54167f013cdffcd0829.tar.bz2
perlweeklychallenge-club-1c3a6af8c70de4e8b8c8f54167f013cdffcd0829.zip
Add solution 280.
Signed-off-by: Thomas Köhler <jean-luc@picard.franken.de>
-rw-r--r--challenge-280/jeanluc2020/blog-1.txt1
-rw-r--r--challenge-280/jeanluc2020/blog-2.txt1
-rwxr-xr-xchallenge-280/jeanluc2020/perl/ch-1.pl51
-rwxr-xr-xchallenge-280/jeanluc2020/perl/ch-2.pl62
4 files changed, 115 insertions, 0 deletions
diff --git a/challenge-280/jeanluc2020/blog-1.txt b/challenge-280/jeanluc2020/blog-1.txt
new file mode 100644
index 0000000000..2fce71dca6
--- /dev/null
+++ b/challenge-280/jeanluc2020/blog-1.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-280-1.html
diff --git a/challenge-280/jeanluc2020/blog-2.txt b/challenge-280/jeanluc2020/blog-2.txt
new file mode 100644
index 0000000000..be3d90626a
--- /dev/null
+++ b/challenge-280/jeanluc2020/blog-2.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-280-2.html
diff --git a/challenge-280/jeanluc2020/perl/ch-1.pl b/challenge-280/jeanluc2020/perl/ch-1.pl
new file mode 100755
index 0000000000..7b6d3b6ffd
--- /dev/null
+++ b/challenge-280/jeanluc2020/perl/ch-1.pl
@@ -0,0 +1,51 @@
+#!/usr/bin/env perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-280/#TASK1
+#
+# Task 1: Twice Appearance
+# ========================
+#
+# 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"
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# Read one character at a time, and return it if it was already
+# in the string before.
+
+use strict;
+use warnings;
+
+twice_appearance("acbddbca");
+twice_appearance("abccd");
+twice_appearance("abcdabbb");
+
+sub twice_appearance {
+ my $str = shift;
+ my $seen = {};
+ print "Input: \"$str\"\n";
+ foreach my $char (split //, $str) {
+ return print "Output: $char\n" if $seen->{$char};
+ $seen->{$char} = 1;
+ }
+ print "Output: None\n";
+}
diff --git a/challenge-280/jeanluc2020/perl/ch-2.pl b/challenge-280/jeanluc2020/perl/ch-2.pl
new file mode 100755
index 0000000000..c7d2dec62b
--- /dev/null
+++ b/challenge-280/jeanluc2020/perl/ch-2.pl
@@ -0,0 +1,62 @@
+#!/usr/bin/env perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-280/#TASK2
+#
+# Task 2: Count Asterisks
+# =======================
+#
+# 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|"
+## Ouput: 2
+##
+## The characters we are looking here are "p" and "w**e".
+#
+## Example 2
+##
+## Input: $str = "perl"
+## Ouput: 0
+#
+## Example 3
+##
+## Input: $str = "th|ewe|e**|k|l***ych|alleng|e"
+## Ouput: 5
+##
+## The characters we are looking here are "th", "e**", "l***ych" and "e".
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# Split $str at | characters, and then only look at the odd
+# ones. Split those into single characters and count the stars.
+
+use strict;
+use warnings;
+
+count_asterisks("p|*e*rl|w**e|*ekly|");
+count_asterisks("perl");
+count_asterisks("th|ewe|e**|k|l***ych|alleng|e");
+
+sub count_asterisks {
+ my $str = shift;
+ my @parts = split /\|/, $str;
+ my $index = 0;
+ my $count = 0;
+ print "Input: \"$str\"\n";
+ foreach my $part (@parts) {
+ $index++;
+ next unless $index % 2;
+ foreach my $char (split //, $part) {
+ $count++ if $char eq '*';
+ }
+ }
+ print "Output: $count\n";
+}