aboutsummaryrefslogtreecommitdiff
path: root/challenge-145/iangoodnight/python/ch-2.py
blob: 2f84c74e41fc70ebe003220ab2df4347881fdcce (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/python3
# ch-1.py

# > https://theweeklychallenge.org/blog/perl-weekly-challenge-145/
#
#  ## Task2 > Palindromic Tree
#  ===========================
#
#  You are given a string `$s`.
#
#  Write a script to create a `Palindromic Tree` for the given string
#
#  I found this [blog] explaining `Palindromic Tree` in detail.
#
#  **Example 1:**
#
#  ```
#  Input: $s = 'redivider'
#  Output: r redivider e edivide d divid i ivi v
#  ```
#
#  **Example 2:**
#
#  ```
#  Input: $s = 'deific'
#  Output: d e i ifi f c
#  ```
#
#  **Example 3:**
#
#  ```
#  Input: $s = 'rotors'
#  Output: r rotor o oto t s
#  ```
#
#  **Example 4:**
#
#  ```
#  Input: $s = 'challenge'
#  Output: c h a l ll e n g
#  ```
#
#  **Example 5:**
#
#  ```
#  Input: $s = 'champion'
#  Output: c h a m p i o n
#  ```
#
#  **Example 6**
#
#  ```
#  Input: $s = 'christmas'
#  Output: c h r i s t m a
#  ```
#
#  [blog]: https://medium.com/@alessiopiergiacomi/eertree-or-palindromic-tree-82453e75025b

import sys
from pathlib import Path

###############################################################################
# PWC Solution ################################################################
###############################################################################


def is_palindrome(string=''):
    gnirts = string[::-1]
    return gnirts == string


def eertree(string):
    palindromes = []
    length = len(string)
    for cursor in range(length):
        for i in range(cursor, length):
            chars = i + 1
            substring = string[cursor:chars]
            if substring not in palindromes and is_palindrome(substring):
                palindromes.append(substring)
    return ' '.join(palindromes)


###############################################################################
# Utilities ###################################################################
###############################################################################


def parse_test_case(filepath):
    with open(filepath) as f:
        lines = f.read().splitlines()
        filtered = list(filter(lambda l: not l.strip().startswith('#'), lines))
        string, test = filtered
        return [string.strip(), test.strip()]


def print_params(path, string, test):
    print(f"{path}:")
    print(f"Input: $s = {string}")
    print(f"Output: {test}")


def test_solution(solution, string, test):
    result = solution(string)
    if test == result:
        print("\u001b[32mPassed \u2690\u001b[0m\n")
        return True
    else:
        print("\u001b[31mFailed \u2715\u001b[0m\n")
        return False


def print_and_run(target):
    string, test = parse_test_case(target)
    print_params(target, string, test)
    test_solution(eertree, string, test)


def run_test_suite(directory):
    for child in Path(directory).iterdir():
        if child.is_file():
            print_and_run(child)


###############################################################################
# Main ########################################################################
###############################################################################


try:
    target = sys.argv[1]
except IndexError:
    target = '../test_cases/ch-2/'

path = Path(target)

if path.is_file():
    print_and_run(target)
elif path.is_dir():
    run_test_suite(target)
else:
    print(f"No tests found at: {target}")