diff options
Diffstat (limited to 'challenge-234/packy-anderson/python')
| -rwxr-xr-x | challenge-234/packy-anderson/python/ch-1.py | 66 | ||||
| -rwxr-xr-x | challenge-234/packy-anderson/python/ch-2.py | 35 |
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 |
