Srivaths 219301465 Automata

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Name: Srivaths Gondi Automata and theory regno: 219301465

A1)
Code to Implement a Turing machine that takes as input a Number N and adds
1 to it in binary.

def add_one_turing(input_string):
input_list = list(input_string)
i = len(input_list) - 1
while i >= 0:
if input_list[i] == '1':
input_list[i] = '0'
i -= 1
else:
input_list[i] = '1'
break
if i < 0:
input_list.insert(0, '1')
return ''.join(input_list)

print(add_one_turing('1001'))
print(add_one_turing('1111'))

Output:
1010
100000
Name: Srivaths Gondi Automata and theory regno: 219301465

A2) To Design a Turing machine which take the input S={a,b} and perform reverse
of string.
Code:

def reverse_turing(input_string):
input_list = list(input_string)
i=0
while i < len(input_list):
symbol = input_list[i]
input_list[i] = '#'
j = len(input_list) - 1
while input_list[j] == '#':
j -= 1
input_list.append(symbol)
i += 1
return ''.join(input_list[input_list.index('#')+1:])
print(reverse_turing('aabb'))

OUTPUT:
‘bbaa’

You might also like