aboutsummaryrefslogtreecommitdiff
path: root/challenge-234/packy-anderson/python
diff options
context:
space:
mode:
authorPacky Anderson <packy@cpan.org>2023-09-12 00:22:11 -0400
committerPacky Anderson <packy@cpan.org>2023-09-12 00:22:11 -0400
commitd705af9bed193d783669de62dec8fc3a5d4cc1eb (patch)
tree209fab38619d9f17b39b3c3d18def767782a3e16 /challenge-234/packy-anderson/python
parent3c9efcab101672b5ecfa042ca258f93d81474928 (diff)
downloadperlweeklychallenge-club-d705af9bed193d783669de62dec8fc3a5d4cc1eb.tar.gz
perlweeklychallenge-club-d705af9bed193d783669de62dec8fc3a5d4cc1eb.tar.bz2
perlweeklychallenge-club-d705af9bed193d783669de62dec8fc3a5d4cc1eb.zip
Challenge 234 solutions by Packy Anderson
Solutions in: * Perl * Raku * Python One blog post
Diffstat (limited to 'challenge-234/packy-anderson/python')
-rwxr-xr-xchallenge-234/packy-anderson/python/ch-1.py66
-rwxr-xr-xchallenge-234/packy-anderson/python/ch-2.py35
2 files changed, 101 insertions, 0 deletions
diff --git a/challenge-234/packy-anderson/python/ch-1.py b/challenge-234/packy-anderson/python/ch-1.py
new file mode 100755
index 0000000000..ddfc986b12
--- /dev/null
+++ b/challenge-234/packy-anderson/python/ch-1.py
@@ -0,0 +1,66 @@
+#!/usr/bin/env python
+
+from collections import Counter
+
+def charFrequency(word):
+ # https://docs.python.org/3/library/collections.html#counter-objects
+ freq = Counter()
+ for c in word:
+ freq[c] += 1
+ return freq
+
+def commonCharacters(words):
+ # get the character freqencies for each word
+ freq = list(map(charFrequency, words))
+
+ # grab the character frequency map for the first word
+ first = freq.pop(0)
+
+ # make a copy of the dictionary since we'll
+ # be modifying it in the loop
+ first_orig = dict(first)
+
+ # now check the characters in the first word against
+ # the characters in all the subsequent words
+ for subsequent in freq:
+ for c in first_orig:
+ if c not in subsequent:
+ # this character isn't in subsequent words,
+ # so let's remove it from the frequency map
+ # of the first word
+ first.pop(c)
+ else:
+ # the character IS in subsequent words,
+ # so let's set the frequency count to be
+ # the minimum count found in those words
+ first[c] = min(first[c], subsequent[c])
+
+ # now we generate a list of characters in the order they
+ # appear in the first word
+ output = []
+ # once again, loop over the characters in the first word
+ for c in words[0]:
+ if c not in first:
+ continue
+ if first[c] > 1:
+ first[c] -= 1
+ else:
+ first.pop(c)
+ output.append(c)
+ return output
+
+def solution(words):
+ quoted = '"' + '", "'.join(words) + '"'
+ print(f'Input: @words = ({quoted})')
+ output = commonCharacters(words)
+ quoted = '"' + '", "'.join(output) + '"'
+ print(f'Output: ({quoted})')
+
+print("Example 1:")
+solution(["java", "javascript", "julia"])
+
+print("\nExample 2:")
+solution(["bella", "label", "roller"])
+
+print("\nExample 3:")
+solution(["cool", "lock", "cook"]) \ No newline at end of file
diff --git a/challenge-234/packy-anderson/python/ch-2.py b/challenge-234/packy-anderson/python/ch-2.py
new file mode 100755
index 0000000000..9024561157
--- /dev/null
+++ b/challenge-234/packy-anderson/python/ch-2.py
@@ -0,0 +1,35 @@
+#!/usr/bin/env python
+
+def findTriplets(ints):
+ solutions = []
+ for i in range(0, len(ints) - 3 ):
+ for j in range(i + 1, len(ints) - 2):
+ for k in range(j + 1, len(ints) - 1):
+ if (ints[i] != ints[j] and
+ ints[j] != ints[k] and
+ ints[i] != ints[k]):
+ solutions.append([i, j, k])
+ return solutions
+
+def solution(ints):
+ intlist = ", ".join([ str(i) for i in ints ])
+ print(f'Input: @ints = ({intlist})')
+ solutions = findTriplets(ints)
+ print(f'Output: {len(solutions)}')
+ if solutions:
+ print("")
+ for triplet in solutions:
+ i, j, k = triplet
+ print(
+ f"({i}, {j}, {k}) because " +
+ f"{ints[i]} != {ints[j]} != {ints[k]}"
+ )
+
+print("Example 1:")
+solution([4, 4, 2, 4, 3])
+
+print("\nExample 2:")
+solution([1, 1, 1, 1, 1])
+
+print("\nExample 3:")
+solution([4, 7, 1, 10, 7, 4, 1, 1]) \ No newline at end of file