From f43e58f9d951d2dacc7175d65662eb2be7e06165 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Mon, 4 Dec 2023 11:57:29 +0000 Subject: - Added solutions by Niels van Dijke. - Added solutions by Laurent Rosenfeld. --- challenge-246/laurent-rosenfeld/blog.txt | 1 + challenge-246/laurent-rosenfeld/perl/ch-1.pl | 12 ++++++ challenge-246/laurent-rosenfeld/raku/ch-1.raku | 1 + challenge-246/perlboy1967/perl/ch-1.pl | 31 ++++++++++++++ challenge-246/perlboy1967/perl/ch-2.pl | 59 ++++++++++++++++++++++++++ challenge-246/perlboy1967/perl/ch1.pl | 31 -------------- challenge-246/perlboy1967/perl/ch2.pl | 59 -------------------------- 7 files changed, 104 insertions(+), 90 deletions(-) create mode 100644 challenge-246/laurent-rosenfeld/blog.txt create mode 100644 challenge-246/laurent-rosenfeld/perl/ch-1.pl create mode 100644 challenge-246/laurent-rosenfeld/raku/ch-1.raku create mode 100755 challenge-246/perlboy1967/perl/ch-1.pl create mode 100755 challenge-246/perlboy1967/perl/ch-2.pl delete mode 100755 challenge-246/perlboy1967/perl/ch1.pl delete mode 100755 challenge-246/perlboy1967/perl/ch2.pl (limited to 'challenge-246') diff --git a/challenge-246/laurent-rosenfeld/blog.txt b/challenge-246/laurent-rosenfeld/blog.txt new file mode 100644 index 0000000000..cb3a874b27 --- /dev/null +++ b/challenge-246/laurent-rosenfeld/blog.txt @@ -0,0 +1 @@ +https://blogs.perl.org/users/laurent_r/2023/12/perl-weekly-challenge-246-6-out-of-49.html diff --git a/challenge-246/laurent-rosenfeld/perl/ch-1.pl b/challenge-246/laurent-rosenfeld/perl/ch-1.pl new file mode 100644 index 0000000000..347c0d34b5 --- /dev/null +++ b/challenge-246/laurent-rosenfeld/perl/ch-1.pl @@ -0,0 +1,12 @@ +use strict; +use warnings; +use feature 'say'; + +my %result; +while (%result < 6) { + # get random integers in the range 1..49 + my $rand = int( rand 49) + 1; + # discard duplicates + $result{$rand} = 1 unless exists $result{$rand}; +} +say join " ", keys %result; diff --git a/challenge-246/laurent-rosenfeld/raku/ch-1.raku b/challenge-246/laurent-rosenfeld/raku/ch-1.raku new file mode 100644 index 0000000000..ab237a21b3 --- /dev/null +++ b/challenge-246/laurent-rosenfeld/raku/ch-1.raku @@ -0,0 +1 @@ +say (1..49).pick: 6; diff --git a/challenge-246/perlboy1967/perl/ch-1.pl b/challenge-246/perlboy1967/perl/ch-1.pl new file mode 100755 index 0000000000..c1fdf6ca2b --- /dev/null +++ b/challenge-246/perlboy1967/perl/ch-1.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/ch-2.pl b/challenge-246/perlboy1967/perl/ch-2.pl new file mode 100755 index 0000000000..9d2000f6a6 --- /dev/null +++ b/challenge-246/perlboy1967/perl/ch-2.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; + diff --git a/challenge-246/perlboy1967/perl/ch1.pl b/challenge-246/perlboy1967/perl/ch1.pl deleted file mode 100755 index c1fdf6ca2b..0000000000 --- a/challenge-246/perlboy1967/perl/ch1.pl +++ /dev/null @@ -1,31 +0,0 @@ -#!/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 deleted file mode 100755 index 9d2000f6a6..0000000000 --- a/challenge-246/perlboy1967/perl/ch2.pl +++ /dev/null @@ -1,59 +0,0 @@ -#!/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; - -- cgit