-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1_texteditor.py
More file actions
62 lines (50 loc) · 1.56 KB
/
Copy path1_texteditor.py
File metadata and controls
62 lines (50 loc) · 1.56 KB
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
# Text Editor
"""
Given a string s representing characters typed into an editor, with "<-" representing a backspace,
return the current state of the editor.
Example 1
Input
s = "abc<-z"
Output
"abz"
Explanation
The "c" got deleted by the backspace.
Example 2
Input
s = "<-x<-z<-"
Output
""
Explanation
All characters are deleted. Also note you can type backspace when the editor is empty as well.
"""
import unittest
def text_editor(s):
temp_list=[]
final_list=[]
result_string=""
for i in s:
temp_list.append(i)
for i in range(len(temp_list)):
if (temp_list[i]!='<' and temp_list[i]!="-"):
final_list.append(temp_list[i])
elif(temp_list[i]=="-" and temp_list[i-1]!="<"):
final_list.append(temp_list[i])
elif(temp_list[i]=="<" and temp_list[i+1]!="-"):
final_list.append(temp_list[i])
elif(temp_list[i]=="<" and temp_list[i+1]=="-"):
if(len(final_list)>=1):
final_list.pop()
result_string="".join(final_list)
result_string=str(result_string)
return result_string
class TestTextEditor(unittest.TestCase):
def test_1(self):
self.assertEqual(text_editor("abc<-z"), "abz")
def test_2(self):
self.assertEqual(text_editor("<-x<-z<-"), "")
def test_3(self):
self.assertEqual(text_editor("ab<c<--"), "ab<-")
def test_4(self):
self.assertEqual(text_editor("ab<c<--<def<-<-<--"), "ab<-<-")
if __name__ == '__main__':
unittest.main(verbosity=2)