-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtape.py
More file actions
53 lines (41 loc) · 1.02 KB
/
Copy pathtape.py
File metadata and controls
53 lines (41 loc) · 1.02 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
class Tape():
def __init__(self):
self.__tape = []
self.__head = 0
@property
def tape(self):
return self.__tape
@tape.setter
def tape(self, value):
self.__tape = value
@property
def head(self):
return self.__head
@head.setter
def head(self, value):
self.__head = value
def add_word(self, word):
self.tape = list(word)
def __str__(self):
return "".join(self.tape).strip("_").replace("_", " ").strip()
def move_head(self, direction):
if direction == "l":
self.head -= 1
if self.head == -1:
self.head = 0
self.tape.insert(0, "_")
elif direction == "r":
self.head += 1
if self.head == len(self.tape):
self.tape.append("_")
def edit(self, commands):
if commands[0] != "*":
if 0 <= self.head < len(self.tape):
self.tape[self.head] = commands[0]
elif len(self.tape) == 0:
self.tape.insert(0, commands[0])
elif self.head >= len(self.tape):
self.tape.append(commands[0])
self.move_head(commands[1])
def get_symbol(self):
return self.tape[self.head]