aboutsummaryrefslogtreecommitdiff
path: root/challenge-266/sgreen/python/ch-1.py
blob: bc0372dc4cbaa30f04f15c1ff94c5cd4bc89e8a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env python3

from collections import defaultdict
import sys


def uncommon_words(*strings) -> list:
    """Find words that only occur once in one string, and don't appear in others

    Params:
        strings: One or more strings

    Returns:
        list: Words that meat the criteria
    """
    freq = defaultdict(int)

    # Compute the frequency of each word
    for string in strings:
        for word in string.split(' '):
            freq[word] += 1

    # Return the word(s) that appear only once
    return [w for w in freq if freq[w] == 1]


def main():
    result = uncommon_words(*sys.argv[1:])

    if len(result) == 0:
        # If there is no result, show an empty string
        print("('')")
    else:
        print("('" + "', '".join(result) + "')")


if __name__ == '__main__':
    main()