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
|
#!/usr/bin/ruby -w
# ch-2.rb
# > 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
################################################################################
# PWC Solution #################################################################
################################################################################
def palindrome?(string)
reversed = string.reverse
string == reversed
end
def eertree(string)
palindromes = []
string.chars.each_index do |cursor|
string[cursor..string.length - 1].chars.each_index do |idx|
chars = idx + 1
substring = string[cursor, chars]
next unless palindrome?(substring)
palindromes.push substring unless palindromes.include? substring
end
end
palindromes.join ' '
end
################################################################################
# Utilities ####################################################################
################################################################################
def parse_test_case(filepath)
lines = File.read(filepath).split("\n").reject { |line| line.match(/^\s*#/) }
input, test = lines
input.strip!
test.strip!
[input, test]
end
def print_params(path, input, test)
puts "#{path}:"
puts "Input: $s = #{input}"
puts "Output: #{test}"
end
def test_solution(solution, input, test)
result = solution.call(input)
if test == result
puts "\e[32mPassed \u2690\e[0m\n\n"
else
puts "\e[31mFailed \u2715\e[0m\n\n"
end
end
def print_and_run(target)
input, test = parse_test_case(target)
print_params(target, input, test)
test_solution(method(:eertree), input, test)
end
def run_test_suite(dir)
tests = Dir.entries(dir).select { |f| File.file? File.join(dir, f) }
tests.sort.each { |test| print_and_run(File.join(dir, test)) }
end
################################################################################
# Main #########################################################################
################################################################################
target = ARGV[0] || '../test_cases/ch-2'
print_and_run(target) if File.file?(target)
run_test_suite(target) if File.directory?(target)
|