A Finite State Automation in Ruby

state = 0
i = 0

inputstring = "10011101100"

while i < inputstring.length do

  symbol = inputstring[i].chr.to_i

  if state == 0 then

    if symbol == 1 then state = 1 else state = 0 end

  elsif state == 1 then    if symbol == 1 then state = 2 else state = 1 end

  elsif state == 2 then    if symbol == 1 then state = 0 else state = 2 end

  end

  i+=1

  if state == 0 then puts "yes" else puts "no" end

end

Comments are closed.