-
Notifications
You must be signed in to change notification settings - Fork 0
/
abbreviation_test.py
158 lines (131 loc) · 4.82 KB
/
abbreviation_test.py
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# https://www.hackerrank.com/challenges/abbr
import unittest
from functools import cache
import sys
sys.setrecursionlimit(4000)
# This approach doesn't work for all cases because it assumes that if a letter
# in 'a' can be capitalized to match with the corresponding letter in 'b', then
# it should be capitalized. However, in reality, we may still be able to remove
# the letter if there's a subsequent capitalized version in 'a'. i.e. the
# problem branches.
def abbreviation_attempt1(a, b):
i = 0
j = 0
while i < len(a) and j < len(b):
if a[i] == b[j]:
i += 1
j += 1
elif a[i].upper() == b[j] and a[i]:
i += 1
j += 1
elif a[i].islower() and a[i]:
i += 1
else:
return "NO"
# Remaining letters in 'a' cannot be less than in 'b'
if len(a) - i < len(b) - j:
return "NO"
# Process remaining letters in 'a'
while i < len(a):
if a[i].islower():
i += 1
else:
return "NO"
return "YES"
@cache
def is_abbreviation(a, b):
# '' == ''
if a == "" and b == "":
return True
# 'Y' < 'XY'
elif len(a) < len(b):
return False
a_last_letter = a[-1]
b_last_letter = b[-1] if len(b) > 0 else None
# X == X
if a_last_letter == b_last_letter:
return is_abbreviation(a[:-1], b[:-1])
# x == X
elif a_last_letter.upper() == b_last_letter:
return is_abbreviation(a[:-1], b[:-1]) or is_abbreviation(a[:-1], b)
# x != Y
elif a_last_letter.islower():
return is_abbreviation(a[:-1], b)
# X != Y
else:
return False
def abbreviation(a, b):
if is_abbreviation(a, b):
return "YES"
else:
return "NO"
class AbbreviationAttempt1(unittest.TestCase):
def test_trailing_small_letters_in_a(self):
a = "daBcde"
b = "ABC"
received = abbreviation_attempt1(a, b)
expected = "YES"
self.assertEqual(received, expected)
def test_trailing_small_letter_in_a_whose_capital_exists_in_b(self):
a = "daBcb"
b = "ABC"
received = abbreviation_attempt1(a, b)
expected = "YES"
self.assertEqual(received, expected)
def test_trailing_small_letters_in_a_that_were_capitalised(self):
a = "daBcda"
b = "ABC"
received = abbreviation_attempt1(a, b)
expected = "YES"
self.assertEqual(received, expected)
def test_trailing_capital_letters_in_a(self):
a = "daBcdA"
b = "ABC"
received = abbreviation_attempt1(a, b)
expected = "NO"
self.assertEqual(received, expected)
def test_trailing_letters_in_b(self):
a = "AfPZN"
b = "APZNC"
received = abbreviation_attempt1(a, b)
expected = "NO"
self.assertEqual(received, expected)
class AbbreviationAttempt2(unittest.TestCase):
def test_trailing_small_letters_in_a(self):
a = "daBcde"
b = "ABC"
received = abbreviation(a, b)
expected = "YES"
self.assertEqual(received, expected)
def test_trailing_small_letter_in_a_whose_capital_exists_in_b(self):
a = "daBcb"
b = "ABC"
received = abbreviation(a, b)
expected = "YES"
self.assertEqual(received, expected)
def test_trailing_small_letters_in_a_that_were_capitalised(self):
a = "daBcda"
b = "ABC"
received = abbreviation(a, b)
expected = "YES"
self.assertEqual(received, expected)
def test_trailing_capital_letters_in_a(self):
a = "daBcdA"
b = "ABC"
received = abbreviation(a, b)
expected = "NO"
self.assertEqual(received, expected)
def test_trailing_letters_in_b(self):
a = "AfPZN"
b = "APZNC"
received = abbreviation(a, b)
expected = "NO"
self.assertEqual(received, expected)
def test_performance(self):
a = "OKXAJYPrZKNRQCLFKXJPJBXAEHYKXLIEIHLQYZPUNGELNKOZHsVLCPCXYSBURPRAWBAXBZBFBhCWBRNsTUNZIKriNYYPFXRNODZPCKNTTPCCZqvajmjtgxjjeafposdovrrzhkwesukvmlwbdzzlvvzyigpfvrhytcxpmbdytlkfzvnjnddpxxotsjfeuxdvmihwadpigjejtuyyrocbtlklkbsndmrpmekreqbbbznnyayzhvyjwlfqmxperliiforxddhwvectqoryxgvomhtjgizwdokbkrsbohupmkwcleetukcivqdbedyajoyglnaqzicuikmrjclfokypugvfgdfbdwpnccztmxwnyxdqrccrgoajwpyeeooesjvluyqdygiovqsrpudcjpriirnophewxcejanrbuqndxikaudmcaynpqmqpbhxwmfwuwwbvalglktmbbnleagsncvqgyduqclvnipxjkctqzatziewlccbyrukvnwuamahovdouwakuokwucexhmqvsilmsjgkqqwenmnxcyvjwjwqmflcpsjdjvagreajsmqjnpqjombmrhnvexfjsldjvapitqyajbypzqbrkladxfqiwsbwbm"
b = "OKXAJYPZKNRQCLFKXJPJBXAEHYKXLIEIHLQYZPUNGELNKOZHVLCPCXYSBURPRAWBAXBZBFBCWBRNTUNZIKNYYPFXRNODZPCKNTTPCCZ"
received = abbreviation(a, b)
expected = "YES"
self.assertEqual(received, expected)
if __name__ == "__main__":
unittest.main()