Skip to main content

TM for f(w) = wwᴿ

Short answer

The TM marks the rightmost unmarked symbol of w (x for 0, y for 1), appends a copy (a for 0, b for 1) after the right #, adds a new #, and repeats leftwards. When the left # is reached, marks are converted back. For #01#, output is #0110#.

Design a TM for f(w) = wwᴿ, w ∈ {0,1}*. If your input is #01#, the TM should give #0110#.

Idea. The tape holds #w#. Process w from its right end towards the left: take the rightmost unmarked symbol of w, mark it, and append a copy at the end of the tape (just before a new right-end #). The appended symbols then appear in the order of wᴿ. Use marks x, y for processed 0, 1 and a, b for appended 0, 1. At the end, convert the marks back.

M has states q0 ... q2, qA, qB, qW, qC and halting state qa, tape alphabet {0, 1, #, x, y, a, b, ⊔}.

StateReadWriteMoveNext
q0#sameRq1
q10 / 1sameRq1
q1#sameLq2
q20xRqA
q21yRqB
q2x / y / a / bsameLq2
q2#sameRqC
qA0 / 1 / x / y / a / bsameRqA
qA#aRqW
qB0 / 1 / x / y / a / bsameRqB
qB#bRqW
qW⊔#Lq2
qCx / a0RqC
qCy / b1RqC
qC#sameSqa
  • q0, q1: go to the right-hand #, then back one cell into w.
  • q2: scan left for the rightmost unmarked 0/1 (skipping x, y, a, b).
  • qA / qB: carry a 0 / 1 to the end of the tape; write a / b over the old # and a new # after it (qW).
  • qC: reached the left #; sweep right and convert x, a to 0 and y, b to 1.

Processing #01# (expected #0110#)

[q0]#01#
#[q1]01#
#0[q1]1#
#01[q1]#
#0[q2]1#
#0y[qB]#
#0yb[qW]
#0y[q2]b#
#0[q2]yb#
#[q2]0yb#
#x[qA]yb#
#xy[qA]b#
#xyb[qA]#
#xyba[qW]
#xyb[q2]a#
#xy[q2]ba#
#x[q2]yba#
#[q2]xyba#
[q2]#xyba#
#[qC]xyba#
#0[qC]yba#
#01[qC]ba#
#011[qC]a#
#0110[qC]#
#0110[qa]#

The result is #0110# = # w wᴿ # for w = 01.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Theory of Computation chapter 4 questions →