Answered step by step
Verified Expert Solution
Question
1 Approved Answer
This question can be answered after studying Section 2 7 . 2 . It assesses the learning outcomes: Understand the Turing Machine model of computation.
This question can be answered after studying Section It assesses the learning outcomes:
Understand the Turing Machine model of computation.
Explain how an algorithm or data structure works, in order to communicate with relevant stakeholders.
We want a Turing machine that checks if a string is a palindrome, ie is the same as when read backwards. The input tape consists of zero or more zeros and ones, followed by blanks. The output is if the input is a palindrome, otherwise You may modify the input symbols.
For example, if the input is then the output is whereas input leads to output
Qa marks
Write the transition table for the Turing machine. Organise the transitions by state or by the order they're executed Section Use descriptive state names.
Suggestions: Write and test a machine that handles evenlength inputs and then handles oddlength inputs. 'Remove' ie blank out input symbols as you process them.
You should add tests to check your Turing machine, but you won't be awarded any marks for your tests. You don't have to remove your tests before submitting this TMA.
In palindrometests, set the debug parameter toTrueif you want to see the configurations your Turing machine goes through. Make sure youset it back toFalseand run the cell again before submittingyour TMA.
RIGHT
LEFT
STAY
MAXSTEPS
def runTMtm:dict, tape:list, debug:bool list:
Run Turing machine tm on tape and return the resulting output.
The machine runs from state 'start' until it halts or has done MAXSTEPS.
The output is the tape's content from the head onwards.
If debug is True, print each configuration.
Preconditions:
tm maps state symbol pairs to symbol movement, state triples
states are represented by strings
symbols are of any hashable type
movement is one of RIGHT, LEFT, STAY
tape is a list of symbols
the blank symbol is represented as None
head
if tape :
tape None
symbol tapehead
state 'start'
step
if debug:
printstep state, tape:head symbol, tapehead:
while step MAXSTEPS and state symbol in tm:
actions tmstate symbol
tapehead actions # write symbol may be the same
head head actions # move left, right or stay
state actions # next state may be the same
step step
if head :
printMoved left past the start of the tape'
head
step MAXSTEPS # force loop to finish
elif head lentape:
tape.appendNone # extend tape when needed
symbol tapehead
if debug:
printstep state, tape:head symbol, tapehead:
output tapehead:
while output and output None:
output.pop
return output
from typing import Callable
def testfunction: Callable, testtable: list None:
Test the function with the testtable. Report failed tests.
Preconditions: each element of testtable is a list or tuple with
a string the test case name
one or more values the inputs to the function
the expected output value
for testcase in testtable:
name testcase
inputs testcase:
expected testcase
actual functioninputs
if actual expected:
printname 'FAILED: actual, 'instead of expected
printTests finished.
Complete the code below:
palindrome
palindrometests
# case, TM input tape, debug, output tape
palindrome palindrome, False,
not palindrome', palindrome, False,
testrunTM palindrometests
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started