diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-12-09 21:41:40 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-12-09 21:41:40 +0000 |
| commit | bdb3403771ace60bfe276ff1344313c5d25421d8 (patch) | |
| tree | afedeb380accb816e0196ff36cfeab37f9a97e89 /challenge-246 | |
| parent | 3c1ecc6b938552014db14317e655085ab507ae90 (diff) | |
| parent | f799336296db128ce6e2c8f7d7d35437c6d4f83f (diff) | |
| download | perlweeklychallenge-club-bdb3403771ace60bfe276ff1344313c5d25421d8.tar.gz perlweeklychallenge-club-bdb3403771ace60bfe276ff1344313c5d25421d8.tar.bz2 perlweeklychallenge-club-bdb3403771ace60bfe276ff1344313c5d25421d8.zip | |
Merge pull request #9213 from LubosKolouch/master
Challenge 246 LK Perl Python Raku blog
Diffstat (limited to 'challenge-246')
| -rw-r--r-- | challenge-246/lubos-kolouch/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-246/lubos-kolouch/perl/ch-1.pl | 21 | ||||
| -rw-r--r-- | challenge-246/lubos-kolouch/perl/ch-2.pl | 38 | ||||
| -rw-r--r-- | challenge-246/lubos-kolouch/python/ch-1.py | 29 | ||||
| -rw-r--r-- | challenge-246/lubos-kolouch/python/ch-2.py | 38 | ||||
| -rw-r--r-- | challenge-246/lubos-kolouch/raku/ch-1.raku | 13 | ||||
| -rw-r--r-- | challenge-246/lubos-kolouch/raku/ch-2.raku | 30 |
7 files changed, 170 insertions, 0 deletions
diff --git a/challenge-246/lubos-kolouch/blog.txt b/challenge-246/lubos-kolouch/blog.txt new file mode 100644 index 0000000000..1c0987515d --- /dev/null +++ b/challenge-246/lubos-kolouch/blog.txt @@ -0,0 +1 @@ +https://egroup.kolouch.org/nextcloud/sites/lubos/2023-12-04_Weekly_challenge_246 diff --git a/challenge-246/lubos-kolouch/perl/ch-1.pl b/challenge-246/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..c9e7b2c514 --- /dev/null +++ b/challenge-246/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,21 @@ +use strict; +use warnings; +use List::Util 'shuffle'; + +sub pick_lotto_numbers { + my @numbers = ( 1 .. 49 ); + my @picked = ( shuffle @numbers )[ 0 .. 5 ]; + return @picked; +} + +# Test +use Test::More tests => 1; +my @result = sort { $a <=> $b } pick_lotto_numbers(); +is( scalar(@result), 6, 'Six numbers are picked' ); + +# Additional tests to check uniqueness and range can be added + +# Main +foreach my $num ( sort { $a <=> $b } pick_lotto_numbers() ) { + print "$num\n"; +} diff --git a/challenge-246/lubos-kolouch/perl/ch-2.pl b/challenge-246/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..769b287936 --- /dev/null +++ b/challenge-246/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,38 @@ +use strict; +use warnings; + +sub is_linear_recurrence { + my @a = @_; + + # Need at least 5 elements to form two equations and solve for p and q + return 0 if @a < 5; + + # Forming two equations: + # a[2] = p * a[0] + q * a[1] + # a[3] = p * a[1] + q * a[2] + # And solving for p and q + my $denominator = $a[0] * $a[2] - $a[1]**2; + return 0 if $denominator == 0; # Avoid division by zero + + my $p = ( $a[2] * $a[3] - $a[1] * $a[4] ) / $denominator; + my $q = ( $a[4] - $p * $a[2] ) / $a[3]; + + # Check if p and q are integers + return 0 if $p != int($p) || $q != int($q); + + # Verify the recurrence for the rest of the array + for my $i ( 4 .. $#a ) { + return 0 if $a[$i] != $p * $a[ $i - 2 ] + $q * $a[ $i - 1 ]; + } + + return 1; +} + +# Tests +use Test::More tests => 3; +ok( is_linear_recurrence( 1, 1, 2, 3, 5 ), 'Fibonacci sequence' ); +ok( !is_linear_recurrence( 4, 2, 4, 5, 7 ), 'Even-odd mismatch' ); +ok( is_linear_recurrence( 4, 1, 2, -3, 8 ), 'Custom sequence' ); + +# Main +print is_linear_recurrence( 4, 1, 2, -3, 8 ) ? "true\n" : "false\n"; diff --git a/challenge-246/lubos-kolouch/python/ch-1.py b/challenge-246/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..7428e5f573 --- /dev/null +++ b/challenge-246/lubos-kolouch/python/ch-1.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python + +import random +from typing import List + + +def pick_lotto_numbers() -> list[int]: + return random.sample(range(1, 50), 6) + + +# Test +import unittest + + +class TestLottoNumbers(unittest.TestCase): + def test_lotto_numbers(self): + result = pick_lotto_numbers() + self.assertEqual(len(result), 6) + self.assertEqual(len(set(result)), 6) + for num in result: + self.assertTrue(1 <= num <= 49) + + +if __name__ == "__main__": + unittest.main() + +# Main +for num in sorted(pick_lotto_numbers()): + print(num) diff --git a/challenge-246/lubos-kolouch/python/ch-2.py b/challenge-246/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..59a6157385 --- /dev/null +++ b/challenge-246/lubos-kolouch/python/ch-2.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python + +import numpy as np + + +def is_linear_recurrence(a): + if len(a) < 5: + return False + + # Forming matrix to solve the linear equations + A = np.array([[a[0], a[1]], [a[1], a[2]]]) + B = np.array([a[2], a[3]]) + + # Solve for p and q + try: + p, q = np.linalg.solve(A, B) + except np.linalg.LinAlgError: + return False # Cannot solve if the matrix A is singular + + # Check if p and q are integers + if not (p.is_integer() and q.is_integer()): + return False + + # Verify the recurrence for the rest of the array + for i in range(4, len(a)): + if a[i] != int(p) * a[i - 2] + int(q) * a[i - 1]: + return False + + return True + + +# Tests +assert is_linear_recurrence([1, 1, 2, 3, 5]) == True, "Fibonacci sequence" +assert is_linear_recurrence([4, 2, 4, 5, 7]) == False, "Even-odd mismatch" +assert is_linear_recurrence([4, 1, 2, -3, 8]) == True, "Custom sequence" + +# Main +print("true" if is_linear_recurrence([4, 1, 2, -3, 8]) else "false") diff --git a/challenge-246/lubos-kolouch/raku/ch-1.raku b/challenge-246/lubos-kolouch/raku/ch-1.raku new file mode 100644 index 0000000000..9172978212 --- /dev/null +++ b/challenge-246/lubos-kolouch/raku/ch-1.raku @@ -0,0 +1,13 @@ +sub pick-lotto-numbers() { + return (1..49).roll(*).unique.head(6); +} + +# Test +use Test; +plan 1; +my @result = pick-lotto-numbers().sort; +is @result.elems, 6, 'Six numbers are picked'; +# Additional tests for uniqueness and range can be added + +# Main +.say for pick-lotto-numbers().sort; diff --git a/challenge-246/lubos-kolouch/raku/ch-2.raku b/challenge-246/lubos-kolouch/raku/ch-2.raku new file mode 100644 index 0000000000..ae2a0de1df --- /dev/null +++ b/challenge-246/lubos-kolouch/raku/ch-2.raku @@ -0,0 +1,30 @@ +sub is-linear-recurrence(@a) { + return False if @a.elems < 5; + + # Solving for p and q using the first four elements + my $denominator = @a[0] * @a[2] - @a[1]**2; + return False if $denominator == 0; # Avoid division by zero + + my $p = (@a[2] * @a[3] - @a[1] * @a[4]) / $denominator; + my $q = (@a[4] - $p * @a[2]) / @a[3]; + + # Check if p and q are integers + return False if $p != $p.Int || $q != $q.Int; + + # Verify the recurrence for the rest of the array + for 4..^@a.elems -> $i { + return False if @a[$i] != $p.Int * @a[$i-2] + $q.Int * @a[$i-1]; + } + + return True; +} + +# Tests +use Test; +plan 3; +ok is-linear-recurrence([1, 1, 2, 3, 5]), 'Fibonacci sequence'; +ok !is-linear-recurrence([4, 2, 4, 5, 7]), 'Even-odd mismatch'; +ok is-linear-recurrence([4, 1, 2, -3, 8]), 'Custom sequence'; + +# Main +say is-linear-recurrence([4, 1, 2, -3, 8]) ?? "true" !! "false"; |
