Skip to main content

This package refers to the topic of automata theory, which includes DFA, NDFA, Mealy machines, Moore machines, Finite state machine and Turing machine.

Project description

For more information, see the GitHub Repository.

AutomaPy

Supported Python versions Total downloads

The package contains a set of tools and algorithms for theoretical computer science, which could include automata theory as well as other topics.

Installation

pip install AutomaPy

Examples of How To Use (AutomaPy)

1. Create a program that implements a machine that accepts strings ending with '101'.

from AutomaPy.examples import EndingWithOneZeroOne

dfa = EndingWithOneZeroOne()

print(dfa.check_string("101"))

The code creates a DFA object and defines four states: A, B, C, and D. The transitions between these states are defined by dictionaries that map input symbols (0 or 1) to the next state. The initial state is set to A, and the final state is set to D.

The code then uses the endingWithOneZeroOne() method of the DFA object to check if the given strings end with the pattern "101". This method takes a string as input and returns True if the DFA accepts the string (i.e., if it ends in the final state), and False otherwise.

The first call to endingWithOneZeroOne() with the input string "101" returns True because the DFA transitions from state A to state B on the first "1", then from state B to state C on the "0", and finally from state C to state D on the "1", which is the final state.

The second call to endingWithOneZeroOne() with the input string "01101" also returns True because the DFA follows the same transitions as in the first case, but with an additional "0" input that takes it from state A to state C.

The third call to endingWithOneZeroOne() with the input string "011011" returns False because the DFA transitions from state A to state C on the first "0", then from state C to state A on the second "1", and finally from state A to state B on the third "1", which is not the final state.

2. Design a Program for creating machine that accepts three consecutive one.

from AutomaPy.examples import ThreeConsecutiveOne

dfa = ThreeConsecutiveOne()

print(dfa.check_string("111")) # True
print(dfa.check_string("11100")) # True
print(dfa.check_string("1100")) # False

The code creates a DFA object and defines eight states: A, B, C, D, E, F, G, and H. The transitions between these states are defined by dictionaries that map input symbols (0 or 1) to the next state. The initial state is set to A, and the final states are set to D, F, G, and H.

The code then uses the threeConsecutiveOne() method of the DFA object to check if the given strings contain three consecutive ones. This method takes a string as input and returns True if the DFA accepts the string (i.e., if it ends in one of the final states), and False otherwise.

The first call to threeConsecutiveOne() with the input string "010111" returns True because the DFA transitions from state A to state B on the first "0", then from state B to state C on the first "1", then from state C to state D on the second "1", and finally to one of the final states (F, G, or H) on the third "1".

The second call to threeConsecutiveOne() with the input string "01011" returns False because the DFA transitions from state A to state B on the first "0", then from state B to state C on the first "1", then from state C to state D on the second "1", but it does not reach a final state because there is no third "1".

The third call to threeConsecutiveOne() with the input string "010101" returns False because the DFA transitions from state A to state B on the first "0", then from state B to state A on the first "1", then from state A to state B on the second "0", then from state B to state A on the second "1", and finally from state A to state B on the third "0", which is not a valid transition according to the DFA definition.

3. Write a program for tokenization of given input.

from AutomaPy import Tokenizer

tokenizer = Tokenizer()

print(tokenizer.tokenize("This is an example of tokenization."))

The tokenize() method of the Tokenizer object is then used to tokenize a given string. This method takes a string as input and returns a list of tokens, where each token is a tuple that contains the token type and the token value.

In the given code, the tokenize method is called with the input string "This is an example of tokenization.". The method uses the predefined rules to split the input string into tokens, which are returned as a list of tuples. Each tuple contains a token type and the corresponding token value. For example, the first tuple in the list might be ('WORD', 'This'), indicating that the token is a word and its value is "This".

4. Design a program for accepting decimal number divisible by 2.

from AutomaPy.examples import DecimalNumberDivisibleByTwo

dfa = DecimalNumberDivisibleByTwo()

print(dfa.check_string("111")) # True
print(dfa.check_string("11100")) # True
print(dfa.check_string("1001")) # False

The code creates a DFA that recognizes decimal numbers divisible by 2 using the AutomaPy package. The DFA has two states ("A" and "B") and transitions based on input symbols "0" and "1". The decimalNumberDivisibleByTwo() method simulates the DFA on a decimal number represented as a string and returns True if the DFA reaches a final state after consuming the entire string, indicating that the number is divisible by 2. The method is called with three different input strings and returns True for the first two and False for the last one, based on whether the input string represents a decimal number that is divisible by 2 or not.

5. Design a program for creating a machine which accepts string having equal no. of 1’s and 0’s.

from AutomaPy.examples import EqualNumberOfOneZero

dfa = EqualNumberOfOneZero()

print(dfa.check_string("111000")) # True
print(dfa.check_string("111001")) # False
print(dfa.check_string("1100")) # True

The code creates a DFA that recognizes strings containing an equal number of 1s and 0s using the AutomaPy package. The DFA has two states ("A" and "B") and transitions based on input symbols "0" and "1". The equalNumberOfOneZero() method simulates the DFA on an input string and returns True if the DFA reaches a final state after consuming the entire string, indicating that the string has an equal number of 1s and 0s. The method is called with three different input strings and returns True for the first and last one and False for the second one, based on whether the input string has an equal number of 1s and 0s or not.

6. Design a program for creating a machine which count number of 1's and 0's in a given string.

from AutomaPy.examples import CountNumberOfOneZero

dfa = CountNumberOfOneZero()

print(dfa.check_string("111000")) # True
print(dfa.check_string("111001")) # False
print(dfa.check_string("1100")) # True

The countNumberOfOneZero() method in the code counts the number of occurrences of "0" and "1" symbols in the input string and returns the count as a string.

For example, if the input string is "0101", the method will return "The number of 0's is 2 and the number of 1's is 2". Similarly, if the input string is "011111", the method will return "The number of 0's is 1 and the number of 1's is 5".

7. Design a program for Turing machine that’s accepts the even number of 1's.

from AutomaPy.examples import TuringMachineEvenOnes

dfa = TuringMachineEvenOnes()

print(dfa.check_string("101111")) #False because 5 is odd (there is 5 1's)
print(dfa.check_string("101")) #True because 2 is even (there is 2 1's)

The turingMachineEvenOnes() method checks whether a binary input string has an even number of 1's using the Turing machine defined in the example.

It returns a boolean indicating whether the input string is accepted by the Turing machine, which is True if the string has an even number of 1's and False otherwise.

You can enter following command to see what functions this package have.

AutomaPy --help

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

AutomaPy-1.3.4.tar.gz (7.9 kB view details)

Uploaded Source

Built Distribution

AutomaPy-1.3.4-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file AutomaPy-1.3.4.tar.gz.

File metadata

  • Download URL: AutomaPy-1.3.4.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.0

File hashes

Hashes for AutomaPy-1.3.4.tar.gz
Algorithm Hash digest
SHA256 7dab753bb4708936489b3e6d0d8d54247e95de9f0b06287384c64509ac18f6a0
MD5 9d3b31bc1fee18cfc090acc330b007de
BLAKE2b-256 6f61994f05377563ed26c529f00638812c2da9f101ef8837f6511d002c39abdd

See more details on using hashes here.

File details

Details for the file AutomaPy-1.3.4-py3-none-any.whl.

File metadata

  • Download URL: AutomaPy-1.3.4-py3-none-any.whl
  • Upload date:
  • Size: 9.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.0

File hashes

Hashes for AutomaPy-1.3.4-py3-none-any.whl
Algorithm Hash digest
SHA256 0983877314dab59c22194b94f6905939383f73ebd4e1608520d8ebbf405e85df
MD5 a0f8a1aa63079ca4341606946c8c115b
BLAKE2b-256 f4302b33bc03c2b71cc56bc71660d6ab1e09f8114005a5605c82110abb73550b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page