aboutsummaryrefslogtreecommitdiff
path: root/challenge-267/packy-anderson/python
diff options
context:
space:
mode:
authorPacky Anderson <packy@cpan.org>2024-04-30 00:42:46 -0400
committerPacky Anderson <packy@cpan.org>2024-04-30 00:42:46 -0400
commit05b3dc1c0f81ab61ae1f4f1032af69509b312aac (patch)
treeac2af8254a34e19cf4aa18607c656b802aa66e7a /challenge-267/packy-anderson/python
parentd8ae12d13bdb74b6f9ce88dd3c8732dfe3984e18 (diff)
downloadperlweeklychallenge-club-05b3dc1c0f81ab61ae1f4f1032af69509b312aac.tar.gz
perlweeklychallenge-club-05b3dc1c0f81ab61ae1f4f1032af69509b312aac.tar.bz2
perlweeklychallenge-club-05b3dc1c0f81ab61ae1f4f1032af69509b312aac.zip
Challenge 267 solutions by Packy Anderson
* Raku that maybe looks like Raku * Perl * Python that definitely looks like Perl 1 Blog post
Diffstat (limited to 'challenge-267/packy-anderson/python')
-rwxr-xr-xchallenge-267/packy-anderson/python/ch-1.py33
-rwxr-xr-xchallenge-267/packy-anderson/python/ch-2.py48
2 files changed, 81 insertions, 0 deletions
diff --git a/challenge-267/packy-anderson/python/ch-1.py b/challenge-267/packy-anderson/python/ch-1.py
new file mode 100755
index 0000000000..034e7ce8fd
--- /dev/null
+++ b/challenge-267/packy-anderson/python/ch-1.py
@@ -0,0 +1,33 @@
+#!/usr/bin/env python
+
+from functools import reduce
+
+def productSign(ints):
+ product = reduce(lambda a, b: a * b, ints)
+ sign = 0 if product == 0 else int(product / abs(product))
+
+ explain = (
+ 'The product ' + ' × '.join(map(lambda i: str(i), ints))
+ + ' => ' + str(product)
+ )
+ if (sign < 0): explain += " < 0"
+ if (sign > 0): explain += " > 0"
+
+ return (sign, explain)
+
+def comma_join(arr):
+ return ', '.join(map(lambda i: str(i), arr))
+
+def solution(ints):
+ print(f'Input: @arr = ({comma_join(ints)})')
+ sign, explain = productSign(ints)
+ print(f'Output: {sign}\n{explain}')
+
+print('Example 1:')
+solution([-1, -2, -3, -4, 3, 2, 1])
+
+print('\nExample 2:')
+solution([1, 2, 0, -2, -1])
+
+print('\nExample 3:')
+solution([-1, -1, 1, -1, 2]) \ No newline at end of file
diff --git a/challenge-267/packy-anderson/python/ch-2.py b/challenge-267/packy-anderson/python/ch-2.py
new file mode 100755
index 0000000000..72c6903c31
--- /dev/null
+++ b/challenge-267/packy-anderson/python/ch-2.py
@@ -0,0 +1,48 @@
+#!/usr/bin/env python
+
+def lineCounts(strvar, widths):
+ (lines, last_line, last_width, explain) = (0, '', 0, '')
+ # we can't do a range of characters, but we can do a range
+ # of the ASCII values of the characters
+ letters = [ chr(c) for c in range(ord('a'), ord('z')+1) ]
+ width = dict( zip(letters, widths) )
+ for c in strvar:
+ if last_width + width[c] > 100:
+ lines += 1
+ explain += f"\nLine {lines}: {last_line} "
+ explain += f"({last_width} pixels)"
+ (last_line, last_width) = (c, width[c])
+ else:
+ last_line += c
+ last_width += width[c]
+ lines += 1
+ explain += f"\nLine {lines}: {last_line} "
+ explain += f"({last_width} pixels)"
+ return (lines, last_width, explain)
+
+def comma_join(arr):
+ return ', '.join(map(lambda i: str(i), arr))
+
+def solution(strvar, widths):
+ print(f'Input: $str = "{strvar}"')
+ print(f' @widths = ({comma_join(widths)})')
+ (lines, last_width, explain) = lineCounts(strvar, widths)
+ print(f'Output: ({lines}, {last_width}){explain}')
+
+print('Example 1:')
+solution(
+ "abcdefghijklmnopqrstuvwxyz",
+ [10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10]
+)
+
+print('\nExample 2:')
+solution(
+ "bbbcccdddaaa",
+ [4,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10]
+)
+
+print('\nExample 3:')
+solution(
+ "thequickbrownfoxjumpedoverthelazydog",
+ [7,8,7,8,7,5,8,8,4,4,8,4,12,8,8,8,8,5,6,4,8,8,12,8,8,7]
+) \ No newline at end of file