In: Computer Science
Python.Python.Python. Write a functional program over the automata that accepts odd length binary digits and rejects those that are not. It must include a function called q0 ,q1, accept and reject(). CANNOT USE STRING. OR LENTH.
101 accepted (3 binary digits)
10610 rejected (the number 6 is not a binary number)
101111 rejected (6 digit binary number)
11111 accepted (5 binary digits)
Solution:
Code:
#
# Function to find a odd length binary digits..
#
def isOddLengthBinary(value):
digit = 0;
#Variable to store single digit
length = 0; #Variable to
store length
while (value)
: #Traversing the
each digit of value
digit = value % 10
#Fetching the single digit..
length = length +
1 #Counting the length..
if (digit > 1 or
digit < 0): #Invalid case
return 0
else:
value = value / 10; #Fetch next digit
if (length % 2 == 0):
return
0 #Even length..
else:
return
1 #Return success..
def main():
print("\n\n")
while(1):
#Read the value from the
user..
value = input(" Enter
any integer value : ")
if(isOddLengthBinary(int(value))):
#Func call..
print ("\n Accepted\n") #True case
else:
print ("\n Rejected\n") #Fail case
if __name__ =='__main__':
main()
#calling main function..
-----------------------------------------------------
Code screenshot:
-----------------------------------
Sample Output:
-------------------------------------
Kindly upvote if you are satisfied with this solution