opurag

FAFL

Jun 4th, 2023
935
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.40 KB | None | 0 0
  1. #Design a Program for creating machine that accepts three consecutive one.
  2. myinput = input("Enter a string of 0 & 1: ")
  3. if len(myinput)<3:
  4.  print("Not Accepted (string length is too small)")
  5. elif len(myinput)==3:
  6.  if myinput=="111":
  7.   print("Accepted")
  8.  else:
  9.   print("Not Accepted")
  10. else:
  11.  count1 = 0
  12.  for i in myinput:
  13.   if count1 == 3:
  14.     break
  15.   if i == "1":
  16.     count1 +=1
  17.   else:
  18.     count1 = 0
  19.  if count1 == 3:
  20.   print("Accepted")
  21.  else:
  22.   print("Not Accepted")
  23.  
  24.  
  25. #2. Design a Program for creating machine that accepts the string always ending with 101.
  26. myinput = input("Enter a string of 0 & 1: ")
  27. if len(myinput)<3:
  28.  print("Not Accepted (string length is too small)")
  29. elif len(myinput)==3:
  30.  if myinput=="101":
  31.   print("Accepted")
  32.  else:
  33.   print("Not Accepted")
  34. else:
  35.  myinput = int(myinput)
  36.  mynum = 0
  37.  for i in range (0,3):
  38.   temp = int(myinput%10)
  39.   mynum = mynum*10 + temp
  40.   myinput = int(myinput/10)
  41.  if mynum == 101:
  42.   print("Accepted")
  43.  else:
  44.   print("Not Accepted")
  45.  
  46. #3. Design a Program for Mode 3 Machine
  47. def binaryToDecimal(n):
  48.  return int(n,2)
  49. # Driver code
  50. myinput = input("Enter the binary value: ")
  51. decimal_number = binaryToDecimal(myinput)
  52. print (decimal_number)
  53. if decimal_number % 3 == 0:
  54.  print("Mod3 is accepted")
  55. else:
  56.  print("Mod3 is Not Accepted")
  57.  
  58. #4. Design a program for accepting decimal number divisible by 2.
  59. myinput = int(input("Enter a number: "))
  60. if myinput%2 == 0:
  61.  print("Given number is divisible by 2")
  62. else:
  63.  print("Given number is not divisible by 2")
  64.  
  65. #5. Design a program for creating a machine which accepts string having equal no. of 1's and O's
  66. myinput = input("Enter a string of 0's & 1's: ")
  67. if len(myinput)<2:
  68.  print("Not Accepted string length is too small")
  69. else:
  70.  count0 = 0
  71.  count1 = 0
  72.  for i in myinput:
  73.   if i=="0":
  74.     count0 += 1
  75.   if i=="1":
  76.     count1 += 1
  77.  
  78.  if count0 == count1:
  79.   print("Accepted")
  80.  else:
  81.   print("Not Accepted")
  82.  
  83. #6. Design a program for creating a machine which count number of 1's and O's in a given string.
  84. myinput = input("Enter a string of 0's & 1's: ")
  85. count0 = 0
  86. count1 = 0
  87. for i in myinput:
  88.  if i=="0":
  89.   count0 += 1
  90.  if i=="1":
  91.   count1 += 1
  92. print("Total no. of 0's are:", count0)
  93. print("Total no. of 1's are:", count1)
  94.  
  95. #7. Design a Program to find 2's complement of a given binary number
  96. def binaryToDecimal(n):
  97.     return int(n, 2)
  98.  
  99. # Function to convert Decimal number To Binary number
  100. def decimalToBinary(n):
  101.     if n > 1:
  102.         decimalToBinary(n // 2)
  103.     print(n % 2, end='')
  104.  
  105. myinput = input("Enter the binary value: ")
  106. oncecomp = ""
  107. for i in myinput:
  108.     if i == "0":
  109.         oncecomp = oncecomp + "1"
  110.     else:
  111.         oncecomp = oncecomp + "0"
  112. print("1's Complement of the given binary number is:", oncecomp)
  113.  
  114. decnumber = binaryToDecimal(oncecomp)
  115. decnumber += 1
  116. print("2's Complement of the given binary number is: ", end='')
  117. decimalToBinary(decnumber)
  118.  
  119. #8. Program to increment the given binary number by 1
  120.  
  121. # Function to convert Binary number to Decimal number
  122. def binaryToDecimal(n):
  123.     return int(n, 2)
  124.  
  125. # Function to convert Decimal number To Binary number
  126. def decimalToBinary(n):
  127.     if n > 1:
  128.         decimalToBinary(n // 2)
  129.     print(n % 2, end='')
  130.  
  131. myinput = input("Enter the binary value: ")
  132. decimal_number = binaryToDecimal(myinput)
  133. decimal_number += 1
  134. print("After incrementing by 1: ", end='')
  135. decimalToBinary(decimal_number)
  136.  
Advertisement
Add Comment
Please, Sign In to add comment