aboutsummaryrefslogtreecommitdiff
path: root/challenge-280
diff options
context:
space:
mode:
authorBob Lied <boblied+github@gmail.com>2024-08-01 09:09:32 -0500
committerBob Lied <boblied+github@gmail.com>2024-08-01 09:09:32 -0500
commitac04c24b01ca5d2c868bb94feb3ced6e587be496 (patch)
tree11fc9e3048af1dae184e01254b2bc5e4a3ba9d86 /challenge-280
parentb9587166580480d2f5dddce64bfcb8d33ef4e127 (diff)
downloadperlweeklychallenge-club-ac04c24b01ca5d2c868bb94feb3ced6e587be496.tar.gz
perlweeklychallenge-club-ac04c24b01ca5d2c868bb94feb3ced6e587be496.tar.bz2
perlweeklychallenge-club-ac04c24b01ca5d2c868bb94feb3ced6e587be496.zip
Week 280 solutions
Diffstat (limited to 'challenge-280')
-rw-r--r--challenge-280/bob-lied/README6
-rw-r--r--challenge-280/bob-lied/perl/ch-1.pl95
-rw-r--r--challenge-280/bob-lied/perl/ch-2.pl78
3 files changed, 176 insertions, 3 deletions
diff --git a/challenge-280/bob-lied/README b/challenge-280/bob-lied/README
index c57c6cd9ae..ad25c661d8 100644
--- a/challenge-280/bob-lied/README
+++ b/challenge-280/bob-lied/README
@@ -1,4 +1,4 @@
-Solutions to weekly challenge 279 by Bob Lied
+Solutions to weekly challenge 280 by Bob Lied
-https://perlweeklychallenge.org/blog/perl-weekly-challenge-279/
-https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-279/bob-lied
+https://perlweeklychallenge.org/blog/perl-weekly-challenge-280/
+https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-280/bob-lied
diff --git a/challenge-280/bob-lied/perl/ch-1.pl b/challenge-280/bob-lied/perl/ch-1.pl
new file mode 100644
index 0000000000..5773899f60
--- /dev/null
+++ b/challenge-280/bob-lied/perl/ch-1.pl
@@ -0,0 +1,95 @@
+#!/usr/bin/env perl
+# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu:
+#=============================================================================
+# Copyright (c) 2024, Bob Lied
+#=============================================================================
+# ch-1.pl Perl Weekly Challenge 280 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"
+#=============================================================================
+
+use v5.40;
+
+
+use Getopt::Long;
+my $Verbose = false;
+my $DoTest = false;
+my $Benchmark = 0;
+
+GetOptions("test" => \$DoTest, "verbose" => \$Verbose, "benchmark:i" => \$Benchmark);
+exit(!runTest()) if $DoTest;
+exit( runBenchmark($Benchmark) ) if $Benchmark;
+
+say twice(lc $_) for @ARGV;
+
+sub twice($str)
+{
+ my %present;
+ for ( split(//, $str) )
+ {
+ return $_ if $present{$_}++;
+ }
+ return "";
+}
+
+sub twice2($str)
+{
+ my @present; # = (false) x 26;
+ for ( split(//, $str) )
+ {
+ return $_ if $present[ord($_)]++;
+ }
+ return "";
+}
+
+sub twice3($str)
+{
+ my @present; # = (false) x 26;
+ while ( my $c = substr($str, 0, 1, "") )
+ {
+ return $c if $present[ord($c)]++;
+ }
+ return "";
+}
+
+
+sub runTest
+{
+ use Test2::V0;
+
+ is( twice("acbddbca"), "d", "Example 1");
+ is( twice("abccd") , "c", "Example 2");
+ is( twice("abcdabbb"), "a", "Example 3");
+ is( twice("abcdefgh"), "", "No repeats");
+
+ is( twice2("acbddbca"), "d", "t2 Example 1");
+ is( twice2("abccd") , "c", "t2 Example 2");
+ is( twice2("abcdabbb"), "a", "t2 Example 3");
+ is( twice2("abcdefgh"), "", "t2 No repeats");
+
+ is( twice3("acbddbca"), "d", "t3 Example 1");
+ is( twice3("abccd") , "c", "t3 Example 2");
+ is( twice3("abcdabbb"), "a", "t3 Example 3");
+ is( twice3("abcdefgh"), "", "t3 No repeats");
+
+ done_testing;
+}
+
+sub runBenchmark($repeat)
+{
+ use Benchmark qw/cmpthese/;
+ my $str = "abcdefghijklmnopqrstuvwxyzz";
+
+ cmpthese($repeat, {
+ withhash => sub { twice($str) },
+ witharray => sub { twice2($str) },
+ withsubstr => sub { twice3($str) },
+ });
+}
diff --git a/challenge-280/bob-lied/perl/ch-2.pl b/challenge-280/bob-lied/perl/ch-2.pl
new file mode 100644
index 0000000000..2866fecb77
--- /dev/null
+++ b/challenge-280/bob-lied/perl/ch-2.pl
@@ -0,0 +1,78 @@
+#!/usr/bin/env perl
+# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu:
+#=============================================================================
+# Copyright (c) 2024, Bob Lied
+#=============================================================================
+# ch-2.pl Perl Weekly Challenge 280 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".
+#=============================================================================
+
+use v5.40;
+
+
+use Getopt::Long;
+my $Verbose = false;
+my $DoTest = false;
+my $Benchmark = 0;
+
+GetOptions("test" => \$DoTest, "verbose" => \$Verbose, "benchmark:i" => \$Benchmark);
+exit(!runTest()) if $DoTest;
+exit( runBenchmark($Benchmark) ) if $Benchmark;
+
+say count2($_) for @ARGV;
+
+sub count($str)
+{
+ my @t = split(/\|/, $str);
+ my $first = join "", @t[ map { $_ * 2 } 0 .. int($#t/2) ];
+ return $first =~ tr/*//d;
+}
+
+
+sub count2($str)
+{
+ my @t = split(/\|/, $str);
+ my $n = 0;
+ while ( defined(my $s = shift @t) )
+ {
+ $n += $s =~ tr/*//d;
+ shift @t;
+ }
+ return $n;
+}
+
+sub runTest
+{
+ use Test2::V0;
+
+ is( count("p|*e*rl|w**e|*ekly|"), 2, "Example 1");
+ is( count("perl"), 0, "Example 2");
+ is( count("th|ewe|e**|k|l***ych|alleng|e"), 5, "Example 3");
+
+ is( count2("p|*e*rl|w**e|*ekly|"), 2, "Example 1");
+ is( count2("perl"), 0, "Example 2");
+ is( count2("th|ewe|e**|k|l***ych|alleng|e"), 5, "Example 3");
+
+ done_testing;
+}
+
+sub runBenchmark($repeat)
+{
+ use Benchmark qw/cmpthese/;
+
+ cmpthese($repeat, {
+ label => sub { },
+ });
+}