aboutsummaryrefslogtreecommitdiff
path: root/challenge-321/packy-anderson/python
diff options
context:
space:
mode:
authorPacky Anderson <packy@cpan.org>2025-05-16 00:37:33 -0400
committerPacky Anderson <packy@cpan.org>2025-05-16 00:40:00 -0400
commitd2d3f658384be159cedb6d4288524a14cfd1d3ea (patch)
tree331f8b7068857935ce0608436ae27a554983665a /challenge-321/packy-anderson/python
parent8ec526706ce12f3249b2392009f54125bbcdc11f (diff)
downloadperlweeklychallenge-club-d2d3f658384be159cedb6d4288524a14cfd1d3ea.tar.gz
perlweeklychallenge-club-d2d3f658384be159cedb6d4288524a14cfd1d3ea.tar.bz2
perlweeklychallenge-club-d2d3f658384be159cedb6d4288524a14cfd1d3ea.zip
Challenge 321 solutions by Packy Anderson
* Raku that maybe looks like Raku, but mostly like Perl * Perl * Python that definitely looks like Perl * Elixir that looks kinda like Elixir 1 blog post
Diffstat (limited to 'challenge-321/packy-anderson/python')
-rwxr-xr-xchallenge-321/packy-anderson/python/ch-1.py46
-rwxr-xr-xchallenge-321/packy-anderson/python/ch-2.py28
2 files changed, 74 insertions, 0 deletions
diff --git a/challenge-321/packy-anderson/python/ch-1.py b/challenge-321/packy-anderson/python/ch-1.py
new file mode 100755
index 0000000000..a39a0b68ec
--- /dev/null
+++ b/challenge-321/packy-anderson/python/ch-1.py
@@ -0,0 +1,46 @@
+#!/usr/bin/env python
+
+from collections import Counter
+
+def distinctAverages(nums):
+ nums.sort() # list.sort() works in place
+ explain, step = "", 0
+ averages = Counter()
+ while nums:
+ min, max = nums.pop(0), nums.pop(-1)
+ avg = (min + max) / 2
+ # since in Python, this will always produce a real number,
+ # we need to do a little more work to make sure avg is an
+ # integer when it has no fractional component
+ if avg == int(avg):
+ avg = int(avg)
+ step += 1
+ explain += (
+ f"Step {step}: Min = {min}, Max = {max}, Avg = {avg}\n"
+ )
+ averages[avg] += 1
+ count = len(list(averages))
+ explain += (
+ f"\nThe count of distinct average is {count}."
+ )
+ return count, explain
+
+def int_join(joiner, arr):
+ return joiner.join(map(lambda i: str(i), arr))
+
+def solution(nums):
+ print(f'Input: @nums = ({int_join(", ", nums)})')
+ count, explain = distinctAverages(nums)
+ print(f'Output: {count}\n\n{explain}')
+
+print('Example 1:')
+solution([1, 2, 4, 3, 5, 6])
+
+print('\nExample 2:')
+solution([0, 2, 4, 8, 3, 5])
+
+print('\nExample 3:')
+solution([7, 3, 1, 0, 5, 9])
+
+print('\nExample 4:')
+solution([1, 9, 2, 6, 3, 4]) \ No newline at end of file
diff --git a/challenge-321/packy-anderson/python/ch-2.py b/challenge-321/packy-anderson/python/ch-2.py
new file mode 100755
index 0000000000..e1f1bc7c82
--- /dev/null
+++ b/challenge-321/packy-anderson/python/ch-2.py
@@ -0,0 +1,28 @@
+#!/usr/bin/env python
+
+import re
+
+def backspaceCompare(str1, str2):
+ s1 = re.sub(r'.\#', "", str1)
+ while s1 != str1:
+ str1 = s1
+ s1 = re.sub(r'.\#', "", str1)
+ s2 = re.sub(r'.\#', "", str2)
+ while s2 != str2:
+ str2 = s2
+ s2 = re.sub(r'.\#', "", str2)
+ return s1 == s2
+
+def solution(str1, str2):
+ print(f'Input: $str1 = "{str1}"')
+ print(f' $str2 = "{str2}"')
+ print(f'Output: { backspaceCompare(str1, str2) }')
+
+print('Example 1:')
+solution("ab#c", "ad#c")
+
+print('\nExample 2:')
+solution("ab##", "a#b#")
+
+print('\nExample 3:')
+solution("a#b", "c")