Can I set up a Finite-State Machine using Python?

Can I set up a Finite-State Machine using Python?
I'm looking to input a string and have the program throw a boolean whereas True means the string is contained in the FSM.

Other urls found in this thread:

stackoverflow.com/questions/2101961/python-state-machine-design#answer-2103398
twitter.com/SFWRedditGifs

>Can I set up a Finite-State Machine using Python?
You have to implement it, but yes.

FSMs are bad because they mutate state

The best program is one completely free of side-effects.

t. functional programmer

That's a strawman of how functional programmers advocate state and immutable logic.

Doesn't python have unchangeable states?
Ok, how? I know how to tell if a string is accepted IRL, i have no clue how to tell a program that.

What if someone were to write a function that generates a new state-vector from a current state combined with an input? Would that tickle your fancy?

FSMs are one of the nicest, easy to reason about ways to implement a lot of stuff imho

Bumpan because I need to understand this.

I don't really see the point of making your own FSM for string inputs when you already have implemented regexes in the language.

Me too. Regex is the perfect solution for searching a substring within a string.

I don't either. It's mandatory to take the test in 2 weeks though.
Even so, I didn't know I have regexes already implemented on Python.

Super easy..

I don't know python, but here's a Ruby solution.
Python way should be pretty similar..

(For the "switch"-statement you would probably use if-else-statements in python..)

def number_in_FSM? (a)

# the state
state = "e0"

# ignore empty strings
return "fuck you!" if a.empty?

# get the elements as array
a = a.split

# check each element
a.each do |i|
# which state are we in ?
case state
when "e0"
if a[i] == 1
state = "e1"
elsif a[i] == 2
state = "e2"
else
return "not in the FSM, sorry.."
end

when "e1"
# some logic here
when "e2"
# some logic here
end
end

end

The principle is not difficult is it?

Of course you could also write a "state" class with the decision logic inside. This would be more robust, but a little bit verbose if the FSM is small. It depends on what you want with it..


By the way:
Google "state machine python", there are a lot of solutions out there..

OK, I found this solution in the internet, it's a FSM for a traffic lights.
>stackoverflow.com/questions/2101961/python-state-machine-design#answer-2103398


It's pretty neat!

Damn, I have to learn me some Python someday..

# trafficLight.pystate

# define state machine
statemachine TrafficLight:
Red -> Green
Green -> Yellow
Yellow -> Red

# define some class level constants
Red.carsCanGo = False
Yellow.carsCanGo = True
Green.carsCanGo = True

Red.delay = wait(20)
Yellow.delay = wait(3)
Green.delay = wait(15)


And here's how to use it:

import statemachine
import trafficLight

tl = trafficLight.Red()
for i in range(6):
print tl, "GO" if tl.carsCanGo else "STOP"
tl.delay()
tl = tl.next_state()

In python:
def numFSM(a):
state='e0'
if a == '':
print('nigger')
else :
a = a.split()
for i in a:
if state == 'e0':
if i == 1:
state = 'e1'
elif i == 2:
state = 'e2'
else:
print('not in the FSM you nigger')
elif state == 'e1':
pass
elif state == 'e2':
pass

Thanks, short and beautiful.

>short and beautiful.
That code is the exact opposite of beautiful.

Remind me again why we allow Haskell fags to shit all over this board with their uneducated opinions?

I don't think he was serious m8

>That code is the exact opposite of beautiful.

It's not that bad.
I look at it and can immediately understand it.

If you put some comments in it it would pass as a "quick and dirty" solution..

Of course it would be better to have some kind of Enumeration of the states, but this depends on the size and purpose..

What is wrong with that picture?

def numFSM(a):
state='e2'
if a == '':
print('[x] Error')
else :
a = a.split()
case0 = {i: 'e'+str(i) for i in range(0, len(a))}
#case1 = {#...}
#case2 = {#...}
while a :
for x in range(0, len(a)):
if state == 'e0':
state = case0[x]
print(state)
elif state == 'e1':
#state = case1[x]
print('case1')
elif state == 'e2':
#state = case2[x]
print('case2')

You might want to split it up into checking for transitions and actually acting according to the transitions:

// some pseudo code

loop:

// check transitions
transition = notrans

if x: transition = xtrans

else if y: transition = ytrans

// change state

if transition = xtrans: doChangeStateX()

else if transition = ytrans: doChangeStateY()

else: doChangeStateDefault()

You are retarded.