diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-12-04 11:49:26 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-12-04 11:49:26 +0000 |
| commit | d544385bd75b8f6efbf4e40e21ec389f78483a65 (patch) | |
| tree | 59913065ca24f7d0d7e7a23b9f6e02fa829e5360 | |
| parent | a5142150602b78793af374bf2e1e4c6f2644cca9 (diff) | |
| parent | 8d905842a27d5a445fd2060fcbc6c9b9c3d9f31c (diff) | |
| download | perlweeklychallenge-club-d544385bd75b8f6efbf4e40e21ec389f78483a65.tar.gz perlweeklychallenge-club-d544385bd75b8f6efbf4e40e21ec389f78483a65.tar.bz2 perlweeklychallenge-club-d544385bd75b8f6efbf4e40e21ec389f78483a65.zip | |
Merge pull request #9190 from PerlBoy1967/branch-for-challenge-246
w246 - Task 1 & 2
| -rwxr-xr-x | challenge-246/perlboy1967/perl/ch1.pl | 31 | ||||
| -rwxr-xr-x | challenge-246/perlboy1967/perl/ch2.pl | 59 |
2 files changed, 90 insertions, 0 deletions
diff --git a/challenge-246/perlboy1967/perl/ch1.pl b/challenge-246/perlboy1967/perl/ch1.pl new file mode 100755 index 0000000000..c1fdf6ca2b --- /dev/null +++ b/challenge-246/perlboy1967/perl/ch1.pl @@ -0,0 +1,31 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 246 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-246 + +Author: Niels 'PerlBoy' van Dijke + +Task 1: 6 out of 49 +Submitted by: Andreas Voegele + +6 out of 49 is a German lottery. + +Write a script that outputs six unique random integers from the range 1 to 49. + +=cut + +use v5.32; +use common::sense; + +use Test2::V0 -srand => 246; + +sub germanLottery () { + my @balls = (1..49); + sort { $a <=> $b } map { splice(@balls,rand $#balls + 1,1) } 1..6; +} + +is([germanLottery],[4,19,20,34,38,46]); + +done_testing; diff --git a/challenge-246/perlboy1967/perl/ch2.pl b/challenge-246/perlboy1967/perl/ch2.pl new file mode 100755 index 0000000000..9d2000f6a6 --- /dev/null +++ b/challenge-246/perlboy1967/perl/ch2.pl @@ -0,0 +1,59 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 246 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-246 + +Author: Niels 'PerlBoy' van Dijke + +Task 2: Linear Recurrence of Second Order +Submitted by: Jorg Sommrey + +You are given an array @a of five integers. + +Write a script to decide whether the given integers form a linear recurrence +of second order with integer factors. + +A linear recurrence of second order has the form + +a[n] = p * a[n-2] + q * a[n-1] with n > 1 + +where p and q must be integers. + +=cut + +use v5.32; +use common::sense; +use feature qw(signatures); + +use Test2::V0; + +use List::Util qw(sum0); +use List::MoreUtils qw(minmax); + +sub _chk :prototype($$$\@) ($n,$p,$q,$arL) { + $arL->[$n] == $p * $arL->[$n-2] + $q * $arL->[$n-1]; +} + +sub linearRecurrenceOfSecondOrder (@) { + my @l = @_; + my @i = (2..4); + my ($min,$max) = minmax(@_); + + for my $p ($min..$max) { + for my $q ($min..$max) { + return [ $p, $q] if (sum0(map { _chk($_, $p, $q,@l) } @i) == 3); + } + } + + return []; +} + + +is(linearRecurrenceOfSecondOrder(1,1,2,3,5),[1,1]); +is(linearRecurrenceOfSecondOrder(4,2,4,5,7),[]); +is(linearRecurrenceOfSecondOrder(4,1,2,-3,8),[1,-2]); + +done_testing; + |
