Skip to content

Commit 3867864

Browse files
committed
Provide stats on how many states the machine used.
1 parent a42c82c commit 3867864

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

vim_turing_machine/turing_machine.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ def initialize_machine(self, tape):
4040
self._tape = list(tape)[:] # Copy the initial tape since we mutate it
4141
self._cursor_position = 0
4242
self._current_state = INITIAL_STATE
43+
self._num_steps = 0
4344

4445
def get_state_transition(self):
4546
try:
@@ -81,6 +82,12 @@ def step(self):
8182

8283
def final_state(self):
8384
print('Program complete. Final state: {}'.format(self._current_state))
85+
print(
86+
'The program completed in {} steps using a machine with {} transitions'.format(
87+
self._num_steps,
88+
len(self._state_transitions)
89+
)
90+
)
8491
self.print_tape()
8592
raise StopIteration
8693

@@ -90,14 +97,12 @@ def run(self, initial_tape, max_steps=None):
9097
if self._debug:
9198
self.print_tape()
9299

93-
num_steps = 0
94-
95100
try:
96101
while(True):
97102
self.step()
98-
num_steps += 1
103+
self._num_steps += 1
99104

100-
if max_steps is not None and num_steps >= max_steps:
105+
if max_steps is not None and self._num_steps >= max_steps:
101106
raise TooManyStepsException
102107
except StopIteration:
103108
pass

0 commit comments

Comments
 (0)