diff options
| -rw-r--r-- | challenge-277/jo-37/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-277/jo-37/perl/ch-1.pl | 87 | ||||
| -rwxr-xr-x | challenge-277/jo-37/perl/ch-2.pl | 71 |
3 files changed, 159 insertions, 0 deletions
diff --git a/challenge-277/jo-37/blog.txt b/challenge-277/jo-37/blog.txt new file mode 100644 index 0000000000..868e083858 --- /dev/null +++ b/challenge-277/jo-37/blog.txt @@ -0,0 +1 @@ +https://github.sommrey.de/the-bears-den/2024/07/12/ch-277.html diff --git a/challenge-277/jo-37/perl/ch-1.pl b/challenge-277/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..fdd4301f76 --- /dev/null +++ b/challenge-277/jo-37/perl/ch-1.pl @@ -0,0 +1,87 @@ +#!/usr/bin/perl -s + +use v5.24; +use Test2::V0; + +our ($tests, $examples, $verbose); + +run_tests() if $tests || $examples; # does not return + +die <<EOS unless @ARGV; +usage: $0 [-examples] [-tests] [-verbose] [--] [W11,w12,... W21,W22,...] + +-examples + run the examples from the challenge + +-tests + run some tests + +-verbose + list common words instead of their count + +W11,W12,... W21,W22,... + comma separated lists of words + +EOS + + +### Input and Output + +main: { + my @common = count_common(map [split /,/], @ARGV); + say $verbose ? "(@common)" : scalar @common; +} + + +### Implementation +# +# For details see: +# https://github.sommrey.de/the-bears-den/2024/07/12/ch-277.html#task-1 + + +sub count_common { + my %words; + my $li = $#_; + for my $i (0 .. $li) { + $words{$_}[$i]++ for $_[$i]->@*; + } + no warnings 'uninitialized'; + + grep {$li + 1 == grep $_ == 1, $words{$_}->@[0..$li]} keys %words +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + is scalar count_common( + ["Perl", "is", "my", "friend"], + ["Perl", "and", "Raku", "are", "friend"] + ), 2, 'example 1'; + + is scalar count_common( + ["Perl", "and", "Python", "are", "very", "similar"], + ["Python", "is", "top", "in", "guest", "languages"] + ), 1, 'example 2'; + + is scalar count_common( + ["Perl", "is", "imperative", "Lisp", "is", "functional"], + ["Crystal", "is", "similar", "to", "Ruby"] + ), 0, 'example 3'; + } + + SKIP: { + skip "tests" unless $tests; + + is [count_common( + [qw(one two two three four five)], + [qw(one two three three four six)], + [qw(one two three four four seven)] + )], ['one'], 'three arrays with one common word'; + } + + done_testing; + exit; +} diff --git a/challenge-277/jo-37/perl/ch-2.pl b/challenge-277/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..41d0ac2369 --- /dev/null +++ b/challenge-277/jo-37/perl/ch-2.pl @@ -0,0 +1,71 @@ +#!/usr/bin/perl -s + +use v5.24; +use Test2::V0; +use List::Util 'uniqint'; + +our ($tests, $examples); + +run_tests() if $tests || $examples; # does not return + +die <<EOS unless @ARGV; +usage: $0 [-examples] [-tests] [I...] + +-examples + run the examples from the challenge + +-tests + run some tests + +I... + list of integers + +EOS + + +### Input and Output + +say strong_pairs(@ARGV); + + +### Implementation +# +# For details see: +# https://github.sommrey.de/the-bears-den/2024/07/12/ch-277.html#task-2 + + +sub strong_pairs { + my @ints = uniqint sort {$a <=> $b} @_; + my ($yi, $cnt) = (1, 0); + for my $xi (0 .. $#ints) { + my $x = $ints[$xi]; + $yi++ while $yi < @ints && $ints[$yi] < 2 * $x; + $cnt += $yi - $xi - 1; + } + + $cnt; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is strong_pairs(1, 2, 3, 4, 5), 4, 'example 1'; + is strong_pairs(5, 7, 1, 7), 1, 'example 2'; + } + + SKIP: { + skip "tests" unless $tests; + + is strong_pairs(map 2**$_, 0 .. 9), 0, 'no strong pairs'; + is strong_pairs(map 2**$_ + 1, 0 .. 9), 9, 'neighbors are strong pairs'; + is strong_pairs(10 .. 19), 45, 'all pairs are strong'; + + } + + done_testing; + exit; +} |
