aboutsummaryrefslogtreecommitdiff
path: root/challenge-294/packy-anderson/python
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 /challenge-294/packy-anderson/python
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
Diffstat (limited to 'challenge-294/packy-anderson/python')
-rwxr-xr-xchallenge-294/packy-anderson/python/ch-1.py35
1 files changed, 35 insertions, 0 deletions
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])