aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPacky Anderson <packy@cpan.org>2024-11-09 14:12:36 -0500
committerPacky Anderson <packy@cpan.org>2024-11-09 14:12:36 -0500
commitad4f5b8918ffd7fee7fe14fb032ed21db34eee8a (patch)
tree9f425e75a983a07b47b27bbffcb360b002e32c4d
parent3e3453d23c7de1c054de694f4a3dbe86599d038b (diff)
downloadperlweeklychallenge-club-ad4f5b8918ffd7fee7fe14fb032ed21db34eee8a.tar.gz
perlweeklychallenge-club-ad4f5b8918ffd7fee7fe14fb032ed21db34eee8a.tar.bz2
perlweeklychallenge-club-ad4f5b8918ffd7fee7fe14fb032ed21db34eee8a.zip
Challenge 294 solutions by Packy Anderson
* Raku that maybe looks like Raku, but mostly like Perl * Perl * Python that definitely looks like Perl 1 Blog post
-rw-r--r--challenge-294/packy-anderson/README.md10
-rw-r--r--challenge-294/packy-anderson/blog.txt1
-rwxr-xr-xchallenge-294/packy-anderson/perl/ch-1.pl41
-rwxr-xr-xchallenge-294/packy-anderson/python/ch-1.py35
-rwxr-xr-xchallenge-294/packy-anderson/raku/ch-1.raku36
5 files changed, 114 insertions, 9 deletions
diff --git a/challenge-294/packy-anderson/README.md b/challenge-294/packy-anderson/README.md
index 4694b984a0..68a3b38d3f 100644
--- a/challenge-294/packy-anderson/README.md
+++ b/challenge-294/packy-anderson/README.md
@@ -3,23 +3,15 @@
## Raku
* [Task 1](raku/ch-1.raku)
-* [Task 2](raku/ch-2.raku)
## Perl
* [Task 1](perl/ch-1.pl)
-* [Task 2](perl/ch-2.pl)
## Guest Language: Python
* [Task 1](python/ch-1.py)
-* [Task 2](python/ch-2.py)
-
-## Guest Language: Elixir
-
-* [Task 1](elixir/ch-1.exs)
-* [Task 2](elixir/ch-2.exs)
## Blog Post
-[Perl Weekly Challenge: Oh, oh, Domino!](https://packy.dardan.com/b/So)
+[Perl Weekly Challenge: No music, only numbers](https://packy.dardan.com/b/T1)
diff --git a/challenge-294/packy-anderson/blog.txt b/challenge-294/packy-anderson/blog.txt
new file mode 100644
index 0000000000..6ffd0776be
--- /dev/null
+++ b/challenge-294/packy-anderson/blog.txt
@@ -0,0 +1 @@
+https://packy.dardan.com/b/T1 \ No newline at end of file
diff --git a/challenge-294/packy-anderson/perl/ch-1.pl b/challenge-294/packy-anderson/perl/ch-1.pl
new file mode 100755
index 0000000000..e65b808ec8
--- /dev/null
+++ b/challenge-294/packy-anderson/perl/ch-1.pl
@@ -0,0 +1,41 @@
+#!/usr/bin/env perl
+use v5.40;
+
+sub consecutiveSequence(@ints) {
+ my @count;
+ my $max = shift @ints; # start with the first value as max
+ $count[ $max ]++; # count that occurence
+ foreach my $val ( @ints ) { # count remaining value occurrences
+ $count[ $val ]++;
+ $max = $val if $val > $max; # find the max value
+ }
+ my $maxSeq = 1;
+ my $seq = 0;
+ foreach my $i ( 0 .. $max ) {
+ if (defined($count[$i]) && $count[$i] > 0) {
+ $seq++; # we've found a consecutive value
+ if ($seq > $maxSeq) {
+ $maxSeq = $seq; # keep track of the longest
+ }
+ }
+ else {
+ $seq = 0; # the sequence broke
+ }
+ }
+ # we want sequences longer than one value
+ return $maxSeq > 1 ? $maxSeq : -1;
+}
+
+sub solution($ints) {
+ say 'Input: @ints = (' . join(', ', @$ints) . ')';
+ say 'Output: ' . consecutiveSequence(@$ints);
+}
+
+say "Example 1:";
+solution([10, 4, 20, 1, 3, 2]);
+
+say "\nExample 2:";
+solution([0, 6, 1, 8, 5, 2, 4, 3, 0, 7]);
+
+say "\nExample 3:";
+solution([10, 30, 20]);
diff --git a/challenge-294/packy-anderson/python/ch-1.py b/challenge-294/packy-anderson/python/ch-1.py
new file mode 100755
index 0000000000..d34ab19d41
--- /dev/null
+++ b/challenge-294/packy-anderson/python/ch-1.py
@@ -0,0 +1,35 @@
+#!/usr/bin/env python
+
+from collections import Counter
+
+def consecutiveSequence(ints):
+ count = Counter(ints) # count the values
+ maxVal = max(ints) # find the max value
+ maxSeq = 1
+ seq = 0
+ for i in range(maxVal + 1): # because range(n) goes from 0 to n-1
+ if (count[i] > 0):
+ seq += 1 # we've found a consecutive value
+ if seq > maxSeq:
+ maxSeq = seq # keep track of the longest
+ else:
+ seq = 0 # the sequence broke
+
+ # we want sequences longer than one value
+ return maxSeq if maxSeq > 1 else -1
+
+def comma_join(arr):
+ return ', '.join(map(lambda i: str(i), arr))
+
+def solution(ints):
+ print(f'Input: @ints = ({comma_join(ints)})')
+ print(f'Output: { consecutiveSequence(ints) }')
+
+print('Example 1:')
+solution([10, 4, 20, 1, 3, 2])
+
+print('\nExample 2:')
+solution([0, 6, 1, 8, 5, 2, 4, 3, 0, 7])
+
+print('\nExample 3:')
+solution([10, 30, 20])
diff --git a/challenge-294/packy-anderson/raku/ch-1.raku b/challenge-294/packy-anderson/raku/ch-1.raku
new file mode 100755
index 0000000000..00b57119e4
--- /dev/null
+++ b/challenge-294/packy-anderson/raku/ch-1.raku
@@ -0,0 +1,36 @@
+#!/usr/bin/env raku
+use v6;
+
+sub consecutiveSequence(@ints) {
+ my %count = @ints.Bag; # count the occurences
+ my $max = @ints.max; # find the max value
+ my $maxSeq = 1;
+ my $seq = 0;
+ for 0 .. $max -> $i {
+ if (%count{$i}.defined && %count{$i} > 0) {
+ $seq++; # we've found a consecutive value
+ if ($seq > $maxSeq) {
+ $maxSeq = $seq; # keep track of the longest
+ }
+ }
+ else {
+ $seq = 0; # the sequence broke
+ }
+ }
+ # we want sequences longer than one value
+ return $maxSeq > 1 ?? $maxSeq !! -1;
+}
+
+sub solution(@ints) {
+ say 'Input: @ints = (' ~ @ints.join(', ') ~ ')';
+ say 'Output: ' ~ consecutiveSequence(@ints);
+}
+
+say "Example 1:";
+solution([10, 4, 20, 1, 3, 2]);
+
+say "\nExample 2:";
+solution([0, 6, 1, 8, 5, 2, 4, 3, 0, 7]);
+
+say "\nExample 3:";
+solution([10, 30, 20]);