有限オートマトンの実装
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FiniteAutomata: | |
def __init__(self, alphas, transitions): | |
self.__state = 0 | |
self.__alphas = set(alphas) | |
self.__transitions = transitions | |
def reset(self): | |
self.__state = 0 | |
def readone(self, ch): | |
self.__state = self.__transitions[self.__state][ch] | |
def read(self, s): | |
for ch in s: | |
self.readone(ch) | |
def accepted(self): | |
return self.__transitions[self.__state].get('accepted', False) | |
if __name__ == '__main__': | |
fa = FiniteAutomata('01', [ | |
{'0': 0, '1': 1, 'accepted': True}, | |
{'0': 1, '1': 0}, | |
{'0': 2, '1': 1}, | |
]) | |
for n in range(31): | |
fa.read('{:b}'.format(n)) | |
print(n, '{:b}'.format(n), fa.accepted()) | |
fa.reset() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment