diff options
| author | Dave Jacoby <jacoby.david@gmail.com> | 2023-01-03 13:51:05 -0500 |
|---|---|---|
| committer | Dave Jacoby <jacoby.david@gmail.com> | 2023-01-03 13:51:05 -0500 |
| commit | 3936477f4d102343c9f21cde1cc99e5e6d113d19 (patch) | |
| tree | 388b3cbad9c6f33e6c5d6555b6982f8e4aa16164 /challenge-198 | |
| parent | ee249218f373166edca2b95144a9b0b59e200e05 (diff) | |
| download | perlweeklychallenge-club-3936477f4d102343c9f21cde1cc99e5e6d113d19.tar.gz perlweeklychallenge-club-3936477f4d102343c9f21cde1cc99e5e6d113d19.tar.bz2 perlweeklychallenge-club-3936477f4d102343c9f21cde1cc99e5e6d113d19.zip | |
DAJ 198
Diffstat (limited to 'challenge-198')
| -rw-r--r-- | challenge-198/dave-jacoby/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-198/dave-jacoby/perl/ch-1.pl | 29 | ||||
| -rw-r--r-- | challenge-198/dave-jacoby/perl/ch-2.pl | 27 |
3 files changed, 57 insertions, 0 deletions
diff --git a/challenge-198/dave-jacoby/blog.txt b/challenge-198/dave-jacoby/blog.txt new file mode 100644 index 0000000000..98ad646bd9 --- /dev/null +++ b/challenge-198/dave-jacoby/blog.txt @@ -0,0 +1 @@ +https://jacoby.github.io/2023/01/03/mind-the-gap-weekly-challenge-198.html
\ No newline at end of file diff --git a/challenge-198/dave-jacoby/perl/ch-1.pl b/challenge-198/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..412300592e --- /dev/null +++ b/challenge-198/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,29 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ fc say postderef signatures state }; + +my @examples = ( [ 1, 0, 3, 0, 0, 5 ], [ 2, 5, 8, 1 ], [3] ); + +for my $e (@examples) { + my @list = $e->@*; + my $out = max_gap(@list); + my $list = join ', ', @list; + say <<"END"; + Input: \@list = ($list) + Output: $out +END +} + +sub max_gap( @list ) { + return 0 if scalar @list < 2; + @list = sort @list; + my %hash; + for my $i ( 1 .. -1 + scalar @list ) { + my $gap = abs $list[$i] - $list[ $i - 1 ]; + $hash{$gap}++; + } + my ($max) = sort { $b <=> $a } keys %hash; + return $hash{$max}; +} diff --git a/challenge-198/dave-jacoby/perl/ch-2.pl b/challenge-198/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..044a2f68a4 --- /dev/null +++ b/challenge-198/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,27 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; +use Algorithm::Permute; + +my @examples = ( 10, 15, 1, 25 ); + +for my $e (@examples) { + my $out = prime_count($e); + say <<"END"; + Input: \$n = $e + Output: $out +END +} + +# 1 is not a prime, but 2 is, but the regex in is_prime accepts 1, so +# we start on 2 and go to the given max +# grep ( is_prime($_) ) gives only the list of primes +# scalar gives us the count, which is what we want +sub prime_count ( $e ) { + return scalar grep { is_prime($_) } 2 .. $e; +} + +# https://catonmat.net/perl-regex-that-matches-composite-numbers +sub is_prime ( $n ) { ( '1' x $n ) !~ /\A(11+?)\1+\z/ } |
