diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-11-19 09:51:36 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-11-19 09:51:36 +0000 |
| commit | 2d65f77755fa315407e5332d292f9e6ebdb74873 (patch) | |
| tree | 5a2ad4fbfcd70f17dacc5b8e4376c5dada64d3d8 | |
| parent | c2e845031df38bb57aeaffa7d7362e9456e9b941 (diff) | |
| parent | 9a6436fc139e4faccced2c93ef22c8e7028df081 (diff) | |
| download | perlweeklychallenge-club-2d65f77755fa315407e5332d292f9e6ebdb74873.tar.gz perlweeklychallenge-club-2d65f77755fa315407e5332d292f9e6ebdb74873.tar.bz2 perlweeklychallenge-club-2d65f77755fa315407e5332d292f9e6ebdb74873.zip | |
Merge pull request #7105 from choroba/ech191
Add solutions to 191: Twice Largest & Cute List by E. Choroba
| -rwxr-xr-x | challenge-191/e-choroba/perl/ch-1.pl | 36 | ||||
| -rwxr-xr-x | challenge-191/e-choroba/perl/ch-2.pl | 42 |
2 files changed, 78 insertions, 0 deletions
diff --git a/challenge-191/e-choroba/perl/ch-1.pl b/challenge-191/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..80e8ac2b13 --- /dev/null +++ b/challenge-191/e-choroba/perl/ch-1.pl @@ -0,0 +1,36 @@ +#! /usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +use constant { + MAX => 1, + MAX_BUT_ONE => 0, +}; + +sub twice_largest ($list) { + return 1 if 1 == @$list; + + my @last2 = @$list[ $list->[0] < $list->[1] ? (0, 1) : (1, 0)]; + for my $i (2 .. $#$list) { + if ($list->[$i] > $last2[MAX_BUT_ONE]) { + if ($list->[$i] > $last2[MAX]) { + @last2[MAX_BUT_ONE, MAX] = ($last2[MAX], $list->[$i]); + } else { + $last2[MAX_BUT_ONE] = $list->[$i]; + } + } + } + return ($last2[MAX] >= $last2[MAX_BUT_ONE] * 2) ? 1 : -1 +} + +use Test::More tests => 4 + 3; + +is twice_largest([1, 2, 3, 4]), -1, 'Example 1'; +is twice_largest([1, 2, 0, 5]), 1, 'Example 2'; +is twice_largest([2, 6, 3, 1]), 1, 'Example 3'; +is twice_largest([4, 5, 2, 3]), -1, 'Example 4'; + +is twice_largest([2]), 1, 'Single element'; +is twice_largest([2, 5]), 1, 'Two elements positive'; +is twice_largest([2, 3]), -1, 'Two elements negative'; diff --git a/challenge-191/e-choroba/perl/ch-2.pl b/challenge-191/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..fba3364be5 --- /dev/null +++ b/challenge-191/e-choroba/perl/ch-2.pl @@ -0,0 +1,42 @@ +#! /usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +use Memoize; + +memoize('_cute_list', NORMALIZER => sub { "$_[0] @{ $_[1] }" }); +sub cute_list ($n) { + _cute_list(1, [1 .. $n]); +} + +sub are_divisible ($m, $n) { 0 == $m % $n || 0 == $n % $m } + +sub _cute_list ($pos, $list) { + return are_divisible($pos, $list->[0]) ? 1 : 0 if 1 == @$list; + + my $sum = 0; + for my $i (0 .. $#$list) { + $sum += _cute_list($pos + 1, [@$list[grep $_ != $i, 0 .. $#$list]]) + if are_divisible($pos, $list->[$i]); + } + return $sum +} + +use Test::More tests => 1 + 14; +is cute_list(2), 2, 'Example 1'; + +is cute_list(1), 1, 'Size 1'; +is cute_list(3), 3, 'Size 3'; +is cute_list(4), 8, 'Size 4'; +is cute_list(5), 10, 'Size 5'; +is cute_list(6), 36, 'Size 6'; +is cute_list(7), 41, 'Size 7'; +is cute_list(8), 132, 'Size 8'; +is cute_list(9), 250, 'Size 9'; +is cute_list(10), 700, 'Size 10'; +is cute_list(11), 750, 'Size 11'; +is cute_list(12), 4010, 'Size 12'; +is cute_list(13), 4237, 'Size 13'; +is cute_list(14), 10680, 'Size 14'; +is cute_list(15), 24679, 'Max'; |
