From bccd07cf70ebe71c2742461b6ab8174aff6b2514 Mon Sep 17 00:00:00 2001 From: Util Date: Sun, 4 Dec 2022 16:57:47 -0600 Subject: Add TWC 192 blog post and solutions by Bruce Gray (Raku and Perl) --- challenge-193/bruce-gray/blog.txt | 1 + challenge-193/bruce-gray/perl/ch-1.pl | 12 ++++++++++ challenge-193/bruce-gray/perl/ch-2.pl | 34 +++++++++++++++++++++++++++ challenge-193/bruce-gray/raku/ch-1.raku | 4 ++++ challenge-193/bruce-gray/raku/ch-2.raku | 41 +++++++++++++++++++++++++++++++++ 5 files changed, 92 insertions(+) create mode 100644 challenge-193/bruce-gray/blog.txt create mode 100644 challenge-193/bruce-gray/perl/ch-1.pl create mode 100644 challenge-193/bruce-gray/perl/ch-2.pl create mode 100644 challenge-193/bruce-gray/raku/ch-1.raku create mode 100644 challenge-193/bruce-gray/raku/ch-2.raku diff --git a/challenge-193/bruce-gray/blog.txt b/challenge-193/bruce-gray/blog.txt new file mode 100644 index 0000000000..4052606999 --- /dev/null +++ b/challenge-193/bruce-gray/blog.txt @@ -0,0 +1 @@ +http://blogs.perl.org/users/bruce_gray/2022/12/twc-193_evens_and_oddballs.html 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; +use List::MoreUtils qw; +use List::Categorize qw; + +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 ], + [ 'bob', qw ], +); +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.elems > 1; + + return %h.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', ), + ( 'bob', ), + + # ( 'aaz', ), # More than one oddball; + # ( 'aaz', ), # More than one clique; +; +use Test; +plan +@tests; +for @tests -> ( $expected, @input ) { + is task2(@input), $expected; +} -- cgit From 59a12db62a64b1da25d0bd99aefc5f5586c9ba86 Mon Sep 17 00:00:00 2001 From: Util Date: Sun, 4 Dec 2022 17:07:43 -0600 Subject: Correcting http:// to https:// --- challenge-193/bruce-gray/blog.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-193/bruce-gray/blog.txt b/challenge-193/bruce-gray/blog.txt index 4052606999..2e523626ab 100644 --- a/challenge-193/bruce-gray/blog.txt +++ b/challenge-193/bruce-gray/blog.txt @@ -1 +1 @@ -http://blogs.perl.org/users/bruce_gray/2022/12/twc-193_evens_and_oddballs.html +https://blogs.perl.org/users/bruce_gray/2022/12/twc-193-evens-and-oddballs.html \ No newline at end of file -- cgit