Domanda

eachconfig = [['a', ['blank', ['p1', 'r', 'r'], ['b']]], ['b', ['blank', ['p0'], ['c']]], ['c', ['blank', ['r', 'r'], ['a']]]]
def turingmachine(data):
    a = 0
    tape = []
    finalmc = data[0][0]
    for z in range(1,40):
        tape.append(' ')
    mcName = []
    m = 0
    for emc in range(1,len(data)+1):
        goal = data[m][0]
        mcName.append(goal)
        m+=1
    mcNumber = [h-1 for h in range(1,len(mcName)+1)]
    mcNameNumber = dict(zip(mcName,mcNumber))

    d = 0
    tapeposition = d
    tapescan = tape[d]
    for process in range(1,len(tape)):
        b = 0
        c = 0

        cconfig = data[a][b]
        if cconfig == finalmc:
            b += 1
            scannedsymbol = data[a][b][c]
            if isinstance(scannedsymbol, str):
                if scannedsymbol.lower() in ('any', 'blank'):
                    c += 1
                    operations = data[a][b][c]
                    for cycle in operations:
                        if cycle[0] in ('p','P'):
                            tape[tapeposition] = cycle[1:]
                        elif cycle[0] in ('r', 'R'):
                            tapeposition += 1
                        elif cycle[0] in ('l', 'L'):
                            tapeposition -= 1
                        elif cycle[0] in ('e', 'E'):
                            tape[tapeposition] == ' '   
                    finalmc = data[a][b][-1][0]
                    a = mcNameNumber[finalmc]
        return tape



print turingmachine(eachconfig)

My efficiency may not be amazing in this code, nor is it complete, it's merely a framework for the set data in the current 'eachconfig' list. With that said, though, the output of the program is still just [1, ' ', ' ', ' ', ...], where it should be [1, ' ' , 0 , ' ' , 1 , ' ' , 0 , ...]

I've read over the code dozens of times, I've bug checked by printing out each line as it goes, and I've discovered the problem being that it only iterates through the loop once(the loop in question is the for process in range(1,len(data)+1): loop, the loops before that one are merely for initializing), but I can't figure out why. Why is this happening?

È stato utile?

Soluzione 2

You return tape in inner cycle, the one with process in range(...), so only one iteration happens. Your tape also is not long enough, you make several operations per each tape available space, I also didn't find any "termination" symbol, so it never will be long enough with such for conditions.

Here is "fixed" code:

eachconfig = [['a', ['blank', ['p1', 'r', 'r'], ['b']]], ['b', ['blank', ['p0'], ['c']]], ['c', ['blank', ['r', 'r'], ['a']]]]
def turingmachine(data):
    a = 0
    tape = []
    finalmc = data[0][0]
    for z in range(1,40):
        tape.append(' ')
    mcName = []
    m = 0
    for emc in range(1,len(data)+1):
        goal = data[m][0]
        mcName.append(goal)
        m+=1
    mcNumber = [h-1 for h in range(1,len(mcName)+1)]
    mcNameNumber = dict(zip(mcName,mcNumber))

    d = 0
    tapeposition = d
    tapescan = tape[d]
    for process in range(1,len(tape) - 8):
        b = 0
        c = 0

        cconfig = data[a][b]
        if cconfig == finalmc:
            b += 1
            scannedsymbol = data[a][b][c]
            if isinstance(scannedsymbol, str):
                if scannedsymbol.lower() in ('any', 'blank'):
                    c += 1
                    operations = data[a][b][c]
                    for cycle in operations:
                        if cycle[0] in ('p','P'):
                            tape[tapeposition] = cycle[1:]
                        elif cycle[0] in ('r', 'R'):
                            tapeposition += 1
                        elif cycle[0] in ('l', 'L'):
                            tapeposition -= 1
                        elif cycle[0] in ('e', 'E'):
                            tape[tapeposition] == ' '
                    finalmc = data[a][b][-1][0]
                    a = mcNameNumber[finalmc]
    return tape

print turingmachine(eachconfig)

Altri suggerimenti

Simple answer: because your return statement is inside the for loop. So, probably just an indentation error.

But this code is, I must say, pretty un-Pythonic. You're doing lots of iterating through ranges, which should always be a red flag: in Python you usually want to iterate through an actual thing, not a specially-constructed range. In any case, since you're not even using the loop variable (process), you should consider if a for loop is the right structure at all.

Plus, as volcano points out in the comments, most of your setup code can be shortened significantly.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top