-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathunderscorefyingString.py
103 lines (88 loc) · 3.29 KB
/
underscorefyingString.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
def getLocations(string, substring):
locations = []
for i in range(0, len(string)):
stidx = i
edidx = i+len(substring)
current = ''.join(string[stidx:edidx])
if current == substring:
locations.append([stidx, edidx])
return locations
def collapseLocations(locations):
if len(locations) == 0:
return []
collapseLocations = [locations[0]]
for i in range(1, len(locations)):
if locations[i][0] <= locations[i-1][1]:
collapseLocations[-1][1] = locations[i][1]
continue
collapseLocations.append(locations[i])
return collapseLocations
def underscorifySubstring(string, substring):
locations = getLocations(string, substring)
collapsedLocations = collapseLocations(locations)
if len(collapsedLocations)==0:
return string
tmp = list(string)
for i in range(len(tmp)):
if i < len(collapsedLocations):
tmp.insert(collapsedLocations[i][0]+i, '_')
for i in range(len(tmp)):
if i < len(collapsedLocations):
tmp.insert(collapsedLocations[i][1]+(i*2)+1, '_')
return ''.join(tmp)
# # "_test_this is a _testtest_ to see if _testestest_ it works"
# # "testthis is a testtest to see if testestest it works"
# # [[0,3],[14,17],[18,21],[31,41]]
# def collapse(locations):
# if not locations:
# return locations
# newLocations = [locations[0]]
# previous = newLocations[0]
# for i in range(1, len(locations)):
# current = locations[i]
# if current[0] <= previous[1]:
# previous[1] = current[1]
# else:
# newLocations.append(current)
# previous = current
# return newLocations
# def getLocations(string: str, substring: str):
# locations: list = []
# startIdx: int = 0
# while startIdx < len(string):
# nextIdx: int = string.find(substring, startIdx)
# if nextIdx != -1:
# locations.append([nextIdx, nextIdx+len(substring)])
# startIdx = nextIdx+1
# else:
# break
# return locations
# def underscorify(string, locations):
# locationsIdx = 0
# stringIdx = 0
# inBetweenUnderScores = False
# finalChars = []
# i = 0
# while stringIdx < len(string) and locationsIdx < len(locations):
# if stringIdx == locations[locationsIdx][i]:
# finalChars.append("_")
# inBetweenUnderScores = not inBetweenUnderScores
# if not inBetweenUnderScores:
# locationsIdx += 1
# i = 0 if i == 1 else 1
# finalChars.append(string[stringIdx])
# stringIdx += 1
# if locationsIdx < len(locations):
# finalChars.append('_')
# elif stringIdx < len(string):
# finalChars.append(string[stringIdx:])
# return "".join(finalChars)
# def underscorifySubstring(string, substring):
# locations = collapse(getLocations(string, substring))
# return underscorify(string, locations)
print(underscorifySubstring(
"testthis is a testtest to see if testestest it works", 'test'))
print(underscorifySubstring(
"testthis is a testest to see if testestes it works", 'test'))
print(underscorifySubstring("testthis is a test to see if it works", 'test'))
print(underscorifySubstring("tttttttttttttatt", "tt"))