Donald E. Knuth in Python, cont'd
mwilson at the-wire.com
mwilson at the-wire.com
Wed Apr 11 10:38:22 EDT 2012
Antti J Ylikoski wrote:
>
> I wrote about a straightforward way to program D. E. Knuth in Python,
> and received an excellent communcation about programming Deterministic
> Finite Automata (Finite State Machines) in Python.
[ ... ]
> #You can adjust that for your needs. Sometimes I have the states return
> #a (next_state, output) tuple. You could use a distinguished done()
> #state, or just use None for that. I wrote the example above as global
> #functions, but more commonly these would be methods of some StateMachine
> #class.
> #
> # ------------------------------------------------------------------------
> #
> # The program calculates correctly after D. E. Knuth but it has the
> # actual
> # yearly calendar Easter dates wrong. See Knuth's text.
> #
[ ... ]
> def Easter(Year):
> global G, Y, C, X, Z, D, N, E
> Y = Year
> nextState = E1
> continueLoop = 1
> while continueLoop:
> nextState = nextState()
> if nextState is None:
> break
def Easter (year):
global Y
Y = year
nextState = E1
while nextState is not None:
nextState = nextState()
would be a little cleaner. As you say, to be really clean, make a class.
Mel.
More information about the Python-list
mailing list