FSM for Three Moment Delay in Ruby

state = "q000"i = 0

inputstring = "00111000101"

while i < inputstring.length do

  symbol = inputstring[i].chr.to_i

  #state 000   if state == "q000" then

    if symbol == 0 then       state = "q000"    else       state = "q001"

    end

    puts state + ": 0"

  #state 001  elsif state == "q001" then

    if symbol == 0 then       state = "q010"     else       state = "q011"     end

    puts state + ": 0"

  #state 011  elsif state == "q011" then

    if symbol == 0 then       state = "q110"     else       state = "q111"

    end

    puts state + ": 0"

  #state 110  elsif state == "q110" then

    if symbol == 0 then       state = "q100"    else       state = "q101"     end

    puts state + ": 1"

  #state 101  elsif state == "q101" then

    if symbol == 0 then       state = "q010"     else       state = "q011"     end

    puts state + ": 1"

  #state 010  elsif state == "q010" then

    if symbol == 0 then       state = "q100"     else       state = "q101"     end

    puts state + ": 0"

  #state 100  elsif state == "q100" then

    if symbol == 0 then       state = "q000"    else       state = "q001"          end

    puts state + ": 1"

  #state 111  elsif state == "q111" then

    if symbol == 0 then       state = "q110"     else       state = "q111"

    end

    puts state + ": 1"  end

  i+=1

end

Comments are closed.