Skip to content

Instantly share code, notes, and snippets.

@ytaki0801
Created January 3, 2023 04:46

Revisions

  1. TAKIZAWA Yozo created this gist Jan 3, 2023.
    20 changes: 20 additions & 0 deletions w23tm-2stacksLCR.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    from time import sleep

    # (state,symbol) => (state,symbol,head)
    delta = {(0,0):(1,1,+1), (0,1):(0,2,-1), (0,2):(0,1,-1),
    (1,0):(0,2,-1), (1,1):(1,2,+1), (1,2):(0,0,+1)}
    ds = {0: ' ', 1: '.', 2: '*'}

    state, stackL, C, stackR = 0, [0]*29, 0, [0]*50

    while len(stackL) > 0 and len(stackR) > 0:
    state, C, head = delta[(state,C)]
    if head == +1:
    stackL.insert(0, C)
    C = stackR[0]; stackR = stackR[1:]
    else:
    stackR.insert(0, C)
    C = stackL[0]; stackL = stackL[1:]
    for s in list(reversed(stackL))+[C]+stackR : print(ds[s], end='')
    print()
    sleep(0.005)