aboutsummaryrefslogtreecommitdiff
path: root/challenge-329/sgreen/python/ch-2.py
blob: a06379efe241eaf580111e61e63a3ad180ce5738 (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

import sys

def is_nice_string(s: str) -> bool:
    # Check that every letter has a matching letter in the opposite case
    for letter in s:
        if letter.islower() and letter.upper() not in s:
            return False
        if letter.isupper() and letter.lower() not in s:
            return False
    return True


def nice_string(input_string: str) -> str|None:
    solution = None
    # Calculate every combination of starting and ending positions
    for start in range(len(input_string)-1):
        for end in range(start+2, len(input_string)+1):
            # See if this is a nice string
            extract = input_string[start:end]
            if is_nice_string(extract):
                # If it is longer than the current nice string, update the solution variable
                if solution is None or len(solution) < len(extract):
                    solution = extract

    return solution


def main():
    result = nice_string(sys.argv[1])
    if result is None:
        result = ''
    print('"'+result+'"')


if __name__ == '__main__':
    main()