aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-12-05 00:54:51 +0000
committerGitHub <noreply@github.com>2022-12-05 00:54:51 +0000
commitc8b947ba8dbbeaa01fc42705c2bc6fa0b7a51d86 (patch)
tree1f33e0b741ea6ebe881db0f1d5c8a12fb0147e18
parent7dc15f1cfdf1cc669b64db950b4ee079235f5963 (diff)
parent59a12db62a64b1da25d0bd99aefc5f5586c9ba86 (diff)
downloadperlweeklychallenge-club-c8b947ba8dbbeaa01fc42705c2bc6fa0b7a51d86.tar.gz
perlweeklychallenge-club-c8b947ba8dbbeaa01fc42705c2bc6fa0b7a51d86.tar.bz2
perlweeklychallenge-club-c8b947ba8dbbeaa01fc42705c2bc6fa0b7a51d86.zip
Merge pull request #7205 from Util/branch-for-challenge-193
Add TWC 192 blog post and solutions by Bruce Gray (Raku and Perl)
-rw-r--r--challenge-193/bruce-gray/blog.txt1
-rw-r--r--challenge-193/bruce-gray/perl/ch-1.pl12
-rw-r--r--challenge-193/bruce-gray/perl/ch-2.pl34
-rw-r--r--challenge-193/bruce-gray/raku/ch-1.raku4
-rw-r--r--challenge-193/bruce-gray/raku/ch-2.raku41
5 files changed, 92 insertions, 0 deletions
diff --git a/challenge-193/bruce-gray/blog.txt b/challenge-193/bruce-gray/blog.txt
new file mode 100644
index 0000000000..2e523626ab
--- /dev/null
+++ b/challenge-193/bruce-gray/blog.txt
@@ -0,0 +1 @@
+https://blogs.perl.org/users/bruce_gray/2022/12/twc-193-evens-and-oddballs.html \ No newline at end of file
diff --git a/challenge-193/bruce-gray/perl/ch-1.pl b/challenge-193/bruce-gray/perl/ch-1.pl
new file mode 100644
index 0000000000..d0d2aaf9b6
--- /dev/null
+++ b/challenge-193/bruce-gray/perl/ch-1.pl
@@ -0,0 +1,12 @@
+use v5.36;
+
+sub task1 ($n) {
+ my @r = ('');
+
+ @r = ( map("0$_", @r),
+ map("1$_", @r) ) for 1..$n;
+
+ return @r;
+}
+
+say join " ", task1($_) for 0..3;
diff --git a/challenge-193/bruce-gray/perl/ch-2.pl b/challenge-193/bruce-gray/perl/ch-2.pl
new file mode 100644
index 0000000000..7ba887b648
--- /dev/null
+++ b/challenge-193/bruce-gray/perl/ch-2.pl
@@ -0,0 +1,34 @@
+use v5.36;
+use List::Util qw<mesh pairvalues>;
+use List::MoreUtils qw<slide>;
+use List::Categorize qw<categorize>;
+
+sub diffs ( $s ) {
+ state %A_N = mesh ['a'..'z'], [0..25];
+
+ return join ':',
+ slide { $b - $a }
+ @A_N{ split '', $s };
+}
+sub oddballs ( @s ) {
+ return grep { @{$_} == 1 }
+ pairvalues
+ categorize { diffs($_) } @s;
+}
+sub task2 ( @s ) {
+ my @r = oddballs(@s);
+ warn if @r != 1;
+ return $r[0][0];
+}
+
+
+my @tests = (
+ [ 'abc', qw<adc wzy abc > ],
+ [ 'bob', qw<aaa bob ccc ddd> ],
+);
+use Test::More;
+plan tests => 0+@tests;
+for (@tests) {
+ my ( $expected, @input ) = @{$_};
+ is task2(@input), $expected;
+}
diff --git a/challenge-193/bruce-gray/raku/ch-1.raku b/challenge-193/bruce-gray/raku/ch-1.raku
new file mode 100644
index 0000000000..993a504b47
--- /dev/null
+++ b/challenge-193/bruce-gray/raku/ch-1.raku
@@ -0,0 +1,4 @@
+multi sub task1 ( 1 ) { <0 1> }
+multi sub task1 ( UInt $n ) { [X~] (<0 1> xx $n) }
+
+say .&task1 for ^4;
diff --git a/challenge-193/bruce-gray/raku/ch-2.raku b/challenge-193/bruce-gray/raku/ch-2.raku
new file mode 100644
index 0000000000..91759c1fb8
--- /dev/null
+++ b/challenge-193/bruce-gray/raku/ch-2.raku
@@ -0,0 +1,41 @@
+sub oddballs ( @list, &matcher ) {
+ my %h = @list
+ .classify(&matcher)
+ .values
+ .classify({ .elems == 1 ?? 'Oddball' !! 'Clique' });
+
+ warn "Multiple cliques exist" if %h<Clique>.elems > 1;
+
+ return %h<Oddball>.list;
+}
+sub neighbor_distances ( Str $s --> Str ) {
+ return $s.comb
+ .map(&ord)
+ .rotor(2 => -1)
+ .map({ .[1] - .[0] })
+ .Str;
+}
+sub task2 (@list) {
+ die "Must have at least 3 to have an oddball" if @list.elems < 3;
+
+ my @o = oddballs( @list, &neighbor_distances );
+
+ warn "No oddballs found" if not @o;
+ warn "More than one oddball" if @o.elems > 1;
+
+ return @o.head;
+}
+
+
+constant @tests =
+ ( 'abc', <adc wzy abc > ),
+ ( 'bob', <aaa bob ccc ddd> ),
+
+ # ( 'aaz', <aaa bbb ccc ddd aaz aay> ), # More than one oddball;
+ # ( 'aaz', <aaa bbb ccd dde aaz > ), # More than one clique;
+;
+use Test;
+plan +@tests;
+for @tests -> ( $expected, @input ) {
+ is task2(@input), $expected;
+}