Question

1) Write a TM program to sort a nonempty string of A's and B. Assume the...

1) Write a TM program to sort a nonempty string of A's and B. Assume the tape head starts on the leftmost character. For example if the tape contains BAABABB to start with, then when the machine halts AAABBBB will be left on the tape. Machine will be in the halt state (H) when done with tape head on the first nonblank character. Please give me a code

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer: Sorting refers to arrange the symbols of a given string in a given order. A Turing Machine program to sort a given string will work as per the steps given below:

  1. Find the leftmost character
  2. do
    1. Find a 'B' at the leftmost position in string
    2. Try to find an 'A' right on 'B'.
    3. If such 'A' is found, then replace it with 'B'.
  3. while a character is available to read in string

Based on this, we can specify Turing Machine Program as follows:

For this, we assume that '$' represents the sentinel.

--------------------------------------------------

  1. Find leftmost character
    1. If leftmost character is A or B, write A or B on tape accordingly and move left.
    2. If leftmost character is $, write $ on tape and move right.
  2. Try to find a ‘B’ in leftmost position:
    1. If character read is ‘A’, write ‘A’ on tape and move right.
    2. If character read is ‘B’, write ‘B’ on tape and move right.
    3. If $ is found, write ‘$’ on tape, move left and stop.
  3. Try to find an ‘A’ on right of ‘B’, replace ‘A’ with ‘B’, then move left:
    1. If character read is ‘A’, write ‘B’ on tape and move left.
    2. If character read is ‘B’, write ‘B’ on tape and move right.
    3. If character read in ‘$’, write ‘$’ on tape, move left and stop.
  4. Replace ‘B’ with ‘A’ after replace ‘A’ with ‘B’:
    1. If character read is ‘B’, write ‘A’ on tape and move left.

---------------------------------------------------

Add a comment
Know the answer?
Add Answer to:
1) Write a TM program to sort a nonempty string of A's and B. Assume the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Rarisition written in the format of the Turing Machine simulator is a special state H which means...

    rarisition written in the format of the Turing Machine simulator is a special state H which means halt. For the given Below is a Turing machine program where each line is a transition writen current state, read symbol, new state, write symbol, drection e-d. wmeans to state 4, write a 1 and move the tape head left. Notc there is a special state a os on the leftmost n nanks , write the resulting bitstring when the TM reaches the...

  • 02-) Given a string from the language L(0+1) on the tape, give the program and draw...

    02-) Given a string from the language L(0+1) on the tape, give the program and draw the state diagram of a Turing Machine that can output on the tape: · 0: If the number of 0's > the number of l's 1: If the number of 1's the number of O's N: If the number of l's-the number of 0's Assume null string has an equal number of 0's and 1's. Use the following format for your instructions (5-tuple): <Current...

  • Write a TM (Turing Machine) program that adds two base two numbers. Input is two nonnegative inte...

    Write a TM (Turing Machine) program that adds two base two numbers. Input is two nonnegative integers in base 2 with a plus sign between them. Output is their sum in base 2, with nothing else left on the tape. So first decrement, then increment. Use an online Turing Machine Simulator to see how it works. Use the following language for the program: current state, read symbol, next state, write symbol, direction [directions are (< left, > right, s stay)]...

  • . Please design a standard TM (i.e., a single semi-infinite tape, deterministic) for the laın gua...

    . Please design a standard TM (i.e., a single semi-infinite tape, deterministic) for the laın guage of all palindromes over alphabet {a, b} . Please give both the high-level description (text description or pseudo-code algorithm) and the low-level state transition diagram. Please analyze how many steps (one transition is considered as a step) it takes to accept an input palindrome with n symbols . Please design a deterministic Turing machine with two semi-infinite tapes for the same language. Please give...

  • Create a program that performs the following operations: 1. Prompt for and accept a string of...

    Create a program that performs the following operations: 1. Prompt for and accept a string of up to 80 characters from the user. • The memory buffer for this string is created by: buffer: .space 80 #create space for string input The syscall to place input into the buffer looks like: li $v0,8 # code for syscall read_string la $a0, buffer #tell syscall where the buffer is li $a1, 80 # tell syscall how big the buffer is syscall 2....

  • This is for C++ Write a program that reads in a sequence of characters entered by...

    This is for C++ Write a program that reads in a sequence of characters entered by the user and terminated by a period ('.'). Your program should allow the user to enter multiple lines of input by pressing the enter key at the end of each line. The program should print out a frequency table, sorted in decreasing order by number of occurences, listing each letter that ocurred along with the number of times it occured. All non-alphabetic characters must...

  • Write Java code to implement a FSM machine that recognizes the language for the alphabet {a,b,c} ...

    Write Java code to implement a FSM machine that recognizes the language for the alphabet {a,b,c} consisting of all strings that contain two consecutive c's and end with b.                   Your FSM program should include the following three static methods (Java) or functions (C):                                           a.    int nextState(int state, char symbol)                                  A state-transition function that returns the next state based on the                                  current state and an input symbol. This function should also return                                  -1 when an invalid input character is detected.                                  State...

  • Write a program that uses a recursive function to determine whether a string is a character-unit...

    Write a program that uses a recursive function to determine whether a string is a character-unit palindrome. Moreover, the options for doing case sensitive comparisons and not ignoring spaces are indicated with flags. For example "A nut for a jar of tuna" is a palindrome if spaces are ignored and not otherwise. "Step on no pets" is a palindrome whether spaces are ignored or not, but is not a palindrome if it is case sensitive. Background Palindromes are character sequences...

  • In either Java or Python 3, write a program that simulates a deterministic FSM. It will read from two input files. The first is a file describing an FSM The first line contains the alphabet as a seri...

    In either Java or Python 3, write a program that simulates a deterministic FSM. It will read from two input files. The first is a file describing an FSM The first line contains the alphabet as a series of characters separated by a single space - The second line contains the number of states as an integer k 2 1; states will be numbered 0,1,..., k -1. The start state is always state O The third line contains a series...

  • In either Java or Python 3, write a program that simulates a deterministic FSM. It will read from two input files. The first is a file describing an FSM The first line contains the alphabet as a seri...

    In either Java or Python 3, write a program that simulates a deterministic FSM. It will read from two input files. The first is a file describing an FSM The first line contains the alphabet as a series of characters separated by a single space - The second line contains the number of states as an integer k 2 1; states will be numbered 0,1,..., k -1. The start state is always state O The third line contains a series...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT