aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2022-12-01 09:17:56 +0000
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2022-12-01 09:17:56 +0000
commit2773de5344383b65ee99bc008f52d5c31ec11f6c (patch)
tree73a045e060ae1a21e9c65b346c4d1762aeac438a
parent88525a38388629850110ef595750b58c8d4947cc (diff)
parent51419883d20e4bb4b758ec069c5957e55821e5ed (diff)
downloadperlweeklychallenge-club-2773de5344383b65ee99bc008f52d5c31ec11f6c.tar.gz
perlweeklychallenge-club-2773de5344383b65ee99bc008f52d5c31ec11f6c.tar.bz2
perlweeklychallenge-club-2773de5344383b65ee99bc008f52d5c31ec11f6c.zip
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
-rw-r--r--challenge-193/wlmb/blog.txt1
-rwxr-xr-xchallenge-193/wlmb/perl/ch-1.pl17
-rwxr-xr-xchallenge-193/wlmb/perl/ch-2.pl28
3 files changed, 46 insertions, 0 deletions
diff --git a/challenge-193/wlmb/blog.txt b/challenge-193/wlmb/blog.txt
new file mode 100644
index 0000000000..39bc499180
--- /dev/null
+++ b/challenge-193/wlmb/blog.txt
@@ -0,0 +1 @@
+https://wlmb.github.io/2022/11/29/PWC193/
diff --git a/challenge-193/wlmb/perl/ch-1.pl b/challenge-193/wlmb/perl/ch-1.pl
new file mode 100755
index 0000000000..902478acaa
--- /dev/null
+++ b/challenge-193/wlmb/perl/ch-1.pl
@@ -0,0 +1,17 @@
+#!/usr/bin/env perl
+# Perl weekly challenge 193
+# Task 1: Binary String
+#
+# See https://wlmb.github.io/2022/11/29/PWC193/#task-1-binary-string
+use v5.36;
+die <<"EOF" unless @ARGV;
+Usage; $0 n1 [n2...]
+to print all binary numbers with n_i digits
+EOF
+foreach(@ARGV){
+ say("Expected positive number: $_"),next unless $_>0;
+ print "Binary numbers with $_ digit", $_>1?"s:":": ";
+ my $i=0;
+ print sprintf "%0${_}b ", $i++ while $i<2**$_;
+ say "";
+}
diff --git a/challenge-193/wlmb/perl/ch-2.pl b/challenge-193/wlmb/perl/ch-2.pl
new file mode 100755
index 0000000000..a395a38e3b
--- /dev/null
+++ b/challenge-193/wlmb/perl/ch-2.pl
@@ -0,0 +1,28 @@
+#!/usr/bin/env perl
+# Perl weekly challenge 193
+# Task 2: Odd String
+#
+# See https://wlmb.github.io/2022/11/29/PWC193/#task-2-odd-string
+use v5.36;
+use List::Util qw(reduce);
+use bigint; # in case of many-lettered words
+sub str2num($str){ # convert a string into an array of differences and a number
+ die "Only a..z allowed and >1 chars: $str" unless $str=~/^[a-z]{2,}$/;
+ $str=lc $str; # normalize to lower case
+ state $length=length $str;
+ die "Length of string must be larger than 1: $str" unless $length>1;
+ die "All strings musts have the same length: $str"
+ unless length $str == $length;
+ my ($next, @rest)=map {ord($_)-ord("a")} split "", $str;
+ my $previous;
+ reduce {51*$a+$b} # interpret elements as base-51 digits
+ map {
+ ($previous, $next)=($next, $_);
+ $next-$previous+25
+ } @rest
+}
+my %repetitions_of;
+my %number_of;
+++$repetitions_of{$number_of{$_}=str2num($_)} for @ARGV; # initialize hashes
+my @odd = grep {$repetitions_of{$number_of{$_}}==1} keys %number_of; # filter odd's
+say join(", ", @ARGV), " -> ", join(", ", @odd);