#๐Ÿ”’ Turing machine binary addition

5 messages ยท Page 1 of 1 (latest)

lilac cape
#
i = "#1101_101#"

class turingAddition:

    def __init__(self, input):
        self.input = input
        self.pos = 0
        self.state = "q0"
        self.direction = "R"
        if self.input[self.pos] == "#":
            self.pos += 1
            self.q1(self.pos)

    def q1(self, pos):
        self.state = "q1"
        while self.input[pos] != "#":
            pos += 1

        self.q2(self.pos)

    def q2(self, pos):
        self.state = "q2"
        self.direction = "L"
        pos -= 1
        if self.input[pos] == "1":
            self.input[pos] = "0"
            return self.q4(self.pos)

        if self.input[pos] == "0":
            while self.input[pos] != "1" or self.input[pos] != "_":
                pos -= 1
                if self.input[pos] == "1":
                    return self.q3(self.pos)

                elif self.input[pos] == "_":
                    print(self.state, self.input, self.direction)


    def q3(self, pos):
        self.state = "q3"
        self.direction = "R"
        while self.input[pos] != "#":
            pos += 1
            self.input[pos] = "1"

        return self.q4(self.pos)

    def q4(self, pos):
        self.state = "q4"
        self.direction = "L"
        while self.input[pos] != "_":
            pos += 1

        return self.q5(self.pos)

    def q5(self, pos):
        while self.input[pos] == "1":
            self.input[pos] = "0"
            if self.input[pos] == "#":
                self.input = self.input[0] + "1" + self.input[1:]
                return self.q6()
            pos -= 1

        if self.input[pos] == "0":
            self.input[pos] = "1"
            return self.q6(self.pos)

    def q6(self, pos):
        while self.input[pos] != "#":
            pos -= 1
        return self.q1(self.pos)

turingAddition(i)

Hello, I have tried to recreate a turing machine that performs binary addition. However, it doesn't work and I don't know why. This is my first OOP project, don't judge

zealous raftBOT
#

@lilac cape

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

neon path
# lilac cape ```python i = "#1101_101#" class turingAddition: def __init__(self, input)...

I haven't look though it fully, but this looks sus to me ```py
def q1(self, pos):
self.state = "q1"
while self.input[pos] != "#":
pos += 1

self.q2(self.pos)
despite doing work with `pos`, you never use it again, and instead use `self.pos`. It looks like in general all your code has both `self.pos` and `pos`, which seems very confusing. You should probably just use `self.pos` almost everywhere, and not have your functions take a `pos` argument.
zealous raftBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.