diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-12-06 10:47:13 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-12-06 10:47:13 +0000 |
| commit | 40df386111c052c9c5a9f2117b3ce7b519a690bc (patch) | |
| tree | 4c57e04c9e413cb0724601ac04f8a1759d537b13 | |
| parent | c456d18d80546e1d9071b3a9f7e899648c5a6665 (diff) | |
| parent | 3e582d97344601727f66530135ac13d122d13f29 (diff) | |
| download | perlweeklychallenge-club-40df386111c052c9c5a9f2117b3ce7b519a690bc.tar.gz perlweeklychallenge-club-40df386111c052c9c5a9f2117b3ce7b519a690bc.tar.bz2 perlweeklychallenge-club-40df386111c052c9c5a9f2117b3ce7b519a690bc.zip | |
Merge pull request #9206 from packy/master
Challenge 246 solutions by Packy Anderson
| -rw-r--r-- | challenge-246/packy-anderson/README.md | 68 | ||||
| -rw-r--r-- | challenge-246/packy-anderson/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-246/packy-anderson/perl/ch-1.pl | 16 | ||||
| -rwxr-xr-x | challenge-246/packy-anderson/perl/ch-2.pl | 45 | ||||
| -rwxr-xr-x | challenge-246/packy-anderson/python/ch-1.py | 13 | ||||
| -rwxr-xr-x | challenge-246/packy-anderson/python/ch-2.py | 50 | ||||
| -rwxr-xr-x | challenge-246/packy-anderson/raku/ch-1.raku | 14 | ||||
| -rwxr-xr-x | challenge-246/packy-anderson/raku/ch-2.raku | 42 |
8 files changed, 222 insertions, 27 deletions
diff --git a/challenge-246/packy-anderson/README.md b/challenge-246/packy-anderson/README.md index 9634569207..3a7012c898 100644 --- a/challenge-246/packy-anderson/README.md +++ b/challenge-246/packy-anderson/README.md @@ -7,17 +7,13 @@ Sample output ``` $ raku/ch-1.raku -Example 1: -Input: @int = (8, 1, 2, 2, 3) -Output: (4, 0, 1, 1, 3) - -Example 2: -Input: @int = (6, 5, 4, 8) -Output: (2, 1, 0, 3) - -Example 3: -Input: @int = (2, 2, 2) -Output: (0, 0, 0) +Output: +1 +6 +15 +28 +31 +45 ``` * [Task 2](raku/ch-2.raku) @@ -26,8 +22,19 @@ Sample output ``` $ raku/ch-2.raku Example 1: -Input: @int = (2, 1, 4) -Output: 141 +Input: @a = (1, 1, 2, 3, 5) +Found integer values for P (1) and Q (1) +Output: True + +Example 2: +Input: @a = (4, 2, 4, 5, 7) +Values for P (0.5) and Q (1) for first four elements are not integers +Output: False + +Example 3: +Input: @a = (4, 1, 2, -3, 8) +Found integer values for P (1) and Q (-2) +Output: True ``` ## Perl @@ -37,17 +44,13 @@ Output: 141 Sample output ``` $ perl/ch-1.pl -Example 1: -Input: @arr = (8, 1, 2, 2, 3) -Output: (4, 0, 1, 1, 3) - -Example 2: -Input: @arr = (6, 5, 4, 8) -Output: (2, 1, 0, 3) - -Example 3: -Input: @arr = (2, 2, 2) -Output: (0, 0, 0) +Output: +3 +7 +26 +27 +29 +44 ``` * [Task 2](perl/ch-2.pl) @@ -56,8 +59,19 @@ Sample output ``` $ perl/ch-2.pl Example 1: -Input: @int = (2, 1, 4) -Output: 141 +Input: @a = (1, 1, 2, 3, 5) +Found integer values for P (1) and Q (1) +Output: true + +Example 2: +Input: @a = (4, 2, 4, 5, 7) +Values for P (0.5) and Q (1) for first four elements are not integers +Output: false + +Example 3: +Input: @a = (4, 1, 2, -3, 8) +Found integer values for P (1) and Q (-2) +Output: true ``` ## Guest Language: Python @@ -66,4 +80,4 @@ Output: 141 ## Blog Post -[Perl Weekly Challenge: Count... just a little bit smaller...](https://packy.dardan.com/b/EK) +[Perl Weekly Challenge: 25 or 6 out of four... ty-nine](https://packy.dardan.com/b/F5) diff --git a/challenge-246/packy-anderson/blog.txt b/challenge-246/packy-anderson/blog.txt new file mode 100644 index 0000000000..c6f6e9fc54 --- /dev/null +++ b/challenge-246/packy-anderson/blog.txt @@ -0,0 +1 @@ +https://packy.dardan.com/b/F5
\ No newline at end of file diff --git a/challenge-246/packy-anderson/perl/ch-1.pl b/challenge-246/packy-anderson/perl/ch-1.pl new file mode 100755 index 0000000000..dea633447d --- /dev/null +++ b/challenge-246/packy-anderson/perl/ch-1.pl @@ -0,0 +1,16 @@ +#!/usr/bin/env perl +use v5.38; + +use List::Util qw( shuffle ); + +sub sixOutOfFourtyNine { + return sort { $a <=> $b } ( shuffle(1 .. 49) )[0 .. 5]; +} + +sub solution { + my @arr = sixOutOfFourtyNine(); + say join("\n", @arr); +} + +say "Output:"; +solution();
\ No newline at end of file diff --git a/challenge-246/packy-anderson/perl/ch-2.pl b/challenge-246/packy-anderson/perl/ch-2.pl new file mode 100755 index 0000000000..7323b696e8 --- /dev/null +++ b/challenge-246/packy-anderson/perl/ch-2.pl @@ -0,0 +1,45 @@ +#!/usr/bin/env perl +use v5.38; + +sub findPandQ { + my @a = @_; + my $p = ($a[2] * $a[2] - $a[1] * $a[3]) + / ($a[0] * $a[2] - $a[1] * $a[1]); + my $q = ($a[0] * $a[3] - $a[2] * $a[1]) + / ($a[0] * $a[2] - $a[1] * $a[1]); + return($p, $q); +} + +sub isLinearRecurranceOfSecondOrder { + my @a = @_; + my ($p1, $q1) = findPandQ(@a[0 .. 3]); + my ($p2, $q2) = findPandQ(@a[1 .. 4]); + if ($p1 != $p2 || $q1 != $q2) { + say "Values for P ($p1, $p2) and Q ($q1, $q2) " + . "are not consistent across all five elements"; + return 0; + } + if ($p1 != int($p1) || $q1 != int($q1)) { + say "Values for P ($p1) and Q ($q1) for first " + . "four elements are not integers"; + return 0; + } + say "Found integer values for P ($p1) and Q ($q1)"; + return 1; +} + +sub solution { + my @a = @_; + say 'Input: @a = (' . join(', ', @a) . ')'; + my $result = isLinearRecurranceOfSecondOrder(@a); + say 'Output: ' . ($result ? 'true' : 'false'); +} + +say "Example 1:"; +solution(1, 1, 2, 3, 5); + +say "\nExample 2:"; +solution(4, 2, 4, 5, 7); + +say "\nExample 3:"; +solution(4, 1, 2, -3, 8);
\ No newline at end of file diff --git a/challenge-246/packy-anderson/python/ch-1.py b/challenge-246/packy-anderson/python/ch-1.py new file mode 100755 index 0000000000..035378a255 --- /dev/null +++ b/challenge-246/packy-anderson/python/ch-1.py @@ -0,0 +1,13 @@ +#!/usr/bin/env python + +from random import sample + +def sixOutOfFourtyNine(): + return sorted(sample(range(1, 49), 6)) + +def solution(): + arr = sixOutOfFourtyNine() + print( "\n".join(map(str, arr)) ) + +print('Output') +solution() diff --git a/challenge-246/packy-anderson/python/ch-2.py b/challenge-246/packy-anderson/python/ch-2.py new file mode 100755 index 0000000000..0ac4fae9eb --- /dev/null +++ b/challenge-246/packy-anderson/python/ch-2.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python + +def findPandQ(a): + p = ( + (a[2] * a[2] - a[1] * a[3]) + / + (a[0] * a[2] - a[1] * a[1]) + ) + q = ( + (a[0] * a[3] - a[2] * a[1]) + / + (a[0] * a[2] - a[1] * a[1]) + ) + return(p, q) + + +def isLinearRecurranceOfSecondOrder(a): + (p1, q1) = findPandQ(a[0:4]) + (p2, q2) = findPandQ(a[1:5]) + if p1 != p2 or q1 != q2: + print(f'Values for P ({p1}, {p2}) ', end='') + print(f'and Q ({q1}, {q2}) ', end='') + print(f'are not consistent across all five elements') + return False + if p1 != int(p1) or q1 != int(q1): + print(f'Values for P ({p1}) ', end='') + print(f'and Q ({q1}) ', end='') + print(f'for first four elements are not integers') + return False + + print(f'Found integer values for P ({int(p1)}) ', end='') + print(f'and Q ({int(q1)})') + return True + +def comma_join(a): + return ', '.join(map(str, a)) + +def solution(a): + print(f'Input: @a = ({comma_join(a)})') + result = isLinearRecurranceOfSecondOrder(a) + print(f'Output: {result}') + +print('Example 1:') +solution([1, 1, 2, 3, 5]) + +print('\nExample 2:') +solution([4, 2, 4, 5, 7]) + +print('\nExample 3:') +solution([4, 1, 2, -3, 8])
\ No newline at end of file diff --git a/challenge-246/packy-anderson/raku/ch-1.raku b/challenge-246/packy-anderson/raku/ch-1.raku new file mode 100755 index 0000000000..000f5b469f --- /dev/null +++ b/challenge-246/packy-anderson/raku/ch-1.raku @@ -0,0 +1,14 @@ +#!/usr/bin/env raku +use v6; + +sub sixOutOfFourtyNine { + return (1 .. 49).pick(6).sort; +} + +sub solution { + my @arr = sixOutOfFourtyNine(); + say @arr.join("\n"); +} + +say "Output:"; +solution();
\ No newline at end of file diff --git a/challenge-246/packy-anderson/raku/ch-2.raku b/challenge-246/packy-anderson/raku/ch-2.raku new file mode 100755 index 0000000000..40f1028e5c --- /dev/null +++ b/challenge-246/packy-anderson/raku/ch-2.raku @@ -0,0 +1,42 @@ +#!/usr/bin/env raku +use v6; + +sub findPandQ(@a) { + my $p = (@a[2] * @a[2] - @a[1] * @a[3]) + / (@a[0] * @a[2] - @a[1] * @a[1]); + my $q = (@a[0] * @a[3] - @a[2] * @a[1]) + / (@a[0] * @a[2] - @a[1] * @a[1]); + return($p, $q); +} + +sub isLinearRecurranceOfSecondOrder(@a) { + my ($p1, $q1) = findPandQ(@a[0 .. 3]); + my ($p2, $q2) = findPandQ(@a[1 .. 4]); + if ($p1 != $p2 || $q1 != $q2) { + say "Values for P ($p1, $p2) and Q ($q1, $q2) " + ~ "are not consistent across all five elements"; + return False; + } + if ($p1 != $p1.Int || $q1 != $q1.Int) { + say "Values for P ($p1) and Q ($q1) for first " + ~ "four elements are not integers"; + return False; + } + say "Found integer values for P ($p1) and Q ($q1)"; + return True; +} + +sub solution(*@a) { + say 'Input: @a = (' ~ @a.join(', ') ~ ')'; + my $result = isLinearRecurranceOfSecondOrder(@a); + say 'Output: ' ~ $result; +} + +say "Example 1:"; +solution(1, 1, 2, 3, 5); + +say "\nExample 2:"; +solution(4, 2, 4, 5, 7); + +say "\nExample 3:"; +solution(4, 1, 2, -3, 8);
\ No newline at end of file |
