diff options
| -rw-r--r-- | challenge-051/ryan-thompson/README.md | 9 | ||||
| -rw-r--r-- | challenge-051/ryan-thompson/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-051/ryan-thompson/perl.txt | 328 | ||||
| -rw-r--r-- | challenge-051/ryan-thompson/perl/ch-1.pl | 37 | ||||
| -rw-r--r-- | challenge-051/ryan-thompson/perl/ch-2.pl | 18 | ||||
| -rw-r--r-- | challenge-051/ryan-thompson/raku.txt | 328 | ||||
| -rw-r--r-- | challenge-051/ryan-thompson/raku/ch-1.p6 | 24 | ||||
| -rw-r--r-- | challenge-051/ryan-thompson/raku/ch-2.p6 | 16 |
8 files changed, 756 insertions, 5 deletions
diff --git a/challenge-051/ryan-thompson/README.md b/challenge-051/ryan-thompson/README.md index e79dd77e11..454d7be469 100644 --- a/challenge-051/ryan-thompson/README.md +++ b/challenge-051/ryan-thompson/README.md @@ -1,18 +1,17 @@ # Ryan Thompson -## Week 050 Solutions +## Week 051 Solutions -### Task 1 › Merge Intervals +### Task 1 › 3Sum * [Perl](perl/ch-1.pl) * [Raku](raku/ch-1.p6) -### Task 2 › Noble Integers +### Task 2 › Colourful Numbers * [Perl](perl/ch-2.pl) * [Raku](raku/ch-2.p6) ## Blogs - * [Task 1 › Merge Intervals](http://www.ry.ca/2020/03/merge-intervals/) - * [Task 2 › Noble Integers](http://www.ry.ca/2020/03/noble-integers/) + * [Week 051 › 3Sum and Colourful Numbers](http://www.ry.ca/2020/03/pwc-051/) diff --git a/challenge-051/ryan-thompson/blog.txt b/challenge-051/ryan-thompson/blog.txt new file mode 100644 index 0000000000..8e74b6e8dc --- /dev/null +++ b/challenge-051/ryan-thompson/blog.txt @@ -0,0 +1 @@ +http://www.ry.ca/2020/03/pwc-051/ diff --git a/challenge-051/ryan-thompson/perl.txt b/challenge-051/ryan-thompson/perl.txt new file mode 100644 index 0000000000..c866b94ea4 --- /dev/null +++ b/challenge-051/ryan-thompson/perl.txt @@ -0,0 +1,328 @@ +234 +235 +237 +238 +239 +243 +245 +246 +247 +249 +253 +254 +256 +257 +258 +259 +263 +264 +265 +267 +268 +269 +273 +274 +275 +276 +278 +279 +283 +284 +285 +286 +287 +289 +293 +294 +295 +296 +297 +298 +324 +325 +327 +328 +329 +342 +345 +346 +347 +348 +349 +352 +354 +356 +357 +358 +359 +362 +364 +365 +367 +368 +369 +372 +374 +375 +376 +378 +379 +382 +384 +385 +386 +387 +389 +392 +394 +395 +396 +397 +398 +423 +425 +426 +427 +429 +432 +435 +436 +437 +438 +439 +452 +453 +456 +457 +458 +459 +462 +463 +465 +467 +468 +469 +472 +473 +475 +476 +478 +479 +482 +483 +485 +486 +487 +489 +492 +493 +495 +496 +497 +498 +523 +524 +526 +527 +528 +529 +532 +534 +536 +537 +538 +539 +542 +543 +546 +547 +548 +549 +562 +563 +564 +567 +568 +569 +572 +573 +574 +576 +578 +579 +582 +583 +584 +586 +587 +589 +592 +593 +594 +596 +597 +598 +624 +625 +627 +628 +629 +634 +635 +637 +638 +639 +642 +643 +645 +647 +648 +649 +652 +653 +654 +657 +658 +659 +672 +673 +674 +675 +678 +679 +682 +683 +684 +685 +687 +689 +692 +693 +694 +695 +697 +698 +723 +724 +725 +726 +728 +729 +732 +734 +735 +736 +738 +739 +742 +743 +745 +746 +748 +749 +752 +753 +754 +756 +758 +759 +762 +763 +764 +765 +768 +769 +782 +783 +784 +785 +786 +789 +792 +793 +794 +795 +796 +798 +823 +825 +826 +827 +829 +832 +834 +835 +836 +837 +839 +843 +845 +846 +847 +849 +852 +853 +854 +856 +857 +859 +862 +863 +864 +865 +867 +869 +872 +873 +874 +875 +876 +879 +892 +893 +894 +895 +896 +897 +923 +924 +925 +926 +927 +928 +932 +934 +935 +936 +937 +938 +942 +943 +945 +946 +947 +948 +952 +953 +954 +956 +957 +958 +962 +963 +964 +965 +967 +968 +972 +973 +974 +975 +976 +978 +982 +983 +984 +985 +986 +987 diff --git a/challenge-051/ryan-thompson/perl/ch-1.pl b/challenge-051/ryan-thompson/perl/ch-1.pl new file mode 100644 index 0000000000..8e16bf01e4 --- /dev/null +++ b/challenge-051/ryan-thompson/perl/ch-1.pl @@ -0,0 +1,37 @@ +#!/usr/bin/env perl +# +# ch-1.pl - 3 Sum +# +# 2020 Ryan Thompson <rjt@cpan.org> + +use 5.010; +use warnings; +use strict; +no warnings 'uninitialized'; + +sub sum3 { + my $T = shift; + # Pre-build hash of numbers greater than $y for O(1) lookups in inner loop + my @a = @_; my %Lh = map { shift @a => { map { $_ => 1 } @a } } 1..$#a; + + my @r; + while (my $x = shift) { + $Lh{$_}{ $T-$x-$_ } and push @r, [$x, $_, $T-$x-$_] for @_; + } + @r; +} + +use Test::More; + +is_deeply [sum3( -2 => -4, -1, 0, 3, 4 )], [[-4, -1, 3]]; +is_deeply [sum3( -1 => -6, -5, 0, 5 )], [[-6, 0, 5]]; +is_deeply [sum3( 0 => -6, -1, 0, 1, 2 )], [[-1, 0, 1]]; +is_deeply [sum3( 2 => -8, -5, -2, 3, 4, 5 )], [[-5, 3, 4]]; +is_deeply [sum3( -3 => -7, -6, -5, 0, 5, 6, 8 )], [[-6, -5, 8]]; +is_deeply [sum3( 0 => -10, -9, -5, -4, -2, -1, 1, 5 )], [[-4, -1, 5]]; +is_deeply [sum3( -6 => -19, -13, -11, -9, -2, -1, 0, 1, 3, 7, 8, 9, 10, 11, 14, 16 )], [ + [-19, -1, 14], [-19, 3, 10], [-13, -9, 16], [-13, -2, 9], [-13, -1, 8], + [-13, 0, 7], [-11, -9, 14], [-11, -2, 7], [-9, 0, 3], +]; + +done_testing; diff --git a/challenge-051/ryan-thompson/perl/ch-2.pl b/challenge-051/ryan-thompson/perl/ch-2.pl new file mode 100644 index 0000000000..fd123f3e17 --- /dev/null +++ b/challenge-051/ryan-thompson/perl/ch-2.pl @@ -0,0 +1,18 @@ +#!/usr/bin/env perl + +# ch-2.p6 - Colourful numbers with 3 digits +# +# Ryan Thompson <rjt@cpan.org> + +use 5.010; +use warnings; +use strict; + +sub is_colourful3 { + my ($x, $y, $z) = split //, $_[0]; + my %seen; + $seen{$_}++ and return 0 for $x, $y, $z, $x*$y, $y*$z, $x*$y*$z; + return 1; +} + +say for grep is_colourful3($_), 111..987; diff --git a/challenge-051/ryan-thompson/raku.txt b/challenge-051/ryan-thompson/raku.txt new file mode 100644 index 0000000000..c866b94ea4 --- /dev/null +++ b/challenge-051/ryan-thompson/raku.txt @@ -0,0 +1,328 @@ +234 +235 +237 +238 +239 +243 +245 +246 +247 +249 +253 +254 +256 +257 +258 +259 +263 +264 +265 +267 +268 +269 +273 +274 +275 +276 +278 +279 +283 +284 +285 +286 +287 +289 +293 +294 +295 +296 +297 +298 +324 +325 +327 +328 +329 +342 +345 +346 +347 +348 +349 +352 +354 +356 +357 +358 +359 +362 +364 +365 +367 +368 +369 +372 +374 +375 +376 +378 +379 +382 +384 +385 +386 +387 +389 +392 +394 +395 +396 +397 +398 +423 +425 +426 +427 +429 +432 +435 +436 +437 +438 +439 +452 +453 +456 +457 +458 +459 +462 +463 +465 +467 +468 +469 +472 +473 +475 +476 +478 +479 +482 +483 +485 +486 +487 +489 +492 +493 +495 +496 +497 +498 +523 +524 +526 +527 +528 +529 +532 +534 +536 +537 +538 +539 +542 +543 +546 +547 +548 +549 +562 +563 +564 +567 +568 +569 +572 +573 +574 +576 +578 +579 +582 +583 +584 +586 +587 +589 +592 +593 +594 +596 +597 +598 +624 +625 +627 +628 +629 +634 +635 +637 +638 +639 +642 +643 +645 +647 +648 +649 +652 +653 +654 +657 +658 +659 +672 +673 +674 +675 +678 +679 +682 +683 +684 +685 +687 +689 +692 +693 +694 +695 +697 +698 +723 +724 +725 +726 +728 +729 +732 +734 +735 +736 +738 +739 +742 +743 +745 +746 +748 +749 +752 +753 +754 +756 +758 +759 +762 +763 +764 +765 +768 +769 +782 +783 +784 +785 +786 +789 +792 +793 +794 +795 +796 +798 +823 +825 +826 +827 +829 +832 +834 +835 +836 +837 +839 +843 +845 +846 +847 +849 +852 +853 +854 +856 +857 +859 +862 +863 +864 +865 +867 +869 +872 +873 +874 +875 +876 +879 +892 +893 +894 +895 +896 +897 +923 +924 +925 +926 +927 +928 +932 +934 +935 +936 +937 +938 +942 +943 +945 +946 +947 +948 +952 +953 +954 +956 +957 +958 +962 +963 +964 +965 +967 +968 +972 +973 +974 +975 +976 +978 +982 +983 +984 +985 +986 +987 diff --git a/challenge-051/ryan-thompson/raku/ch-1.p6 b/challenge-051/ryan-thompson/raku/ch-1.p6 new file mode 100644 index 0000000000..c0916f45ac --- /dev/null +++ b/challenge-051/ryan-thompson/raku/ch-1.p6 @@ -0,0 +1,24 @@ +#!/usr/bin/env perl6 + +# ch-1.p6 - 3 Sum +# +# Ryan Thompson <rjt@cpan.org> + +sub sum3( Int $T, *@L ) { + @L.combinations(3).grep( $T == *.sum ) +} + +use Test; + +is-deeply [sum3( 0, -25, -10, -7, -3, 2, 4, 8, 10 )], + [ $(-10, 2, 8), $(-7, -3, 10) ]; +is-deeply [sum3( -2, -4, -1, 0, 3, 4 )], [ $(-4, -1, 3) ]; +is-deeply [sum3( -1, -6, -5, 0, 5 )], [ $(-6, 0, 5) ]; +is-deeply [sum3( 0, -6, -1, 0, 1, 2 )], [ $(-1, 0, 1) ]; +is-deeply [sum3( 2, -8, -5, -2, 3, 4, 5 )], [ $(-5, 3, 4) ]; +is-deeply [sum3( -3, -7, -6, -5, 0, 5, 6, 8 )], [ $(-6, -5, 8) ]; +is-deeply [sum3( 0, -10, -9, -5, -4, -2, -1, 1, 5 )], [ $(-4, -1, 5) ]; +is-deeply [sum3( -4, -11, -7, -6, -5, 2, 3, 4, 5, 6, 9 )], + [ $(-11, 2, 5), $(-11, 3, 4), $(-7, -6, 9) ]; +is-deeply [sum3( 1, -10, -5, -3, 0, 6, 7, 8, 9, 11 )], + [ $(-10, 0, 11), $(-5, -3, 9), $(-5, 0, 6) ]; diff --git a/challenge-051/ryan-thompson/raku/ch-2.p6 b/challenge-051/ryan-thompson/raku/ch-2.p6 new file mode 100644 index 0000000000..913fe32007 --- /dev/null +++ b/challenge-051/ryan-thompson/raku/ch-2.p6 @@ -0,0 +1,16 @@ +#!/usr/bin/env perl6 + +# ch-2.p6 - Colourful numbers of 3 digits +# +# Ryan Thompson <rjt@cpan.org> + +# This *almost* works, but combinations includes (first,last) +#$n.comb».Int.combinations(1..3)».reduce({$^a * $^b}).unique.elems == 7; + +#| Returns True if a 3-digit number is colourful +sub colourful( Int $n where { 100 ≤ $n ≤ 999 } --> Bool ) { + my @D = $n.comb».Int; + !([0], [1], [2], [0,1], [1,2], [0,1,2]).map({ [*] @D[$_] }).repeated; +} + +.say for (100..999).grep: &colourful; |
