Download code

Jump to: navigation, search

Back to Markov_algorithm_simulator_(Python)

Download for Windows: single file, zip

Download for UNIX: single file, zip, tar.gz, tar.bz2

markov.py

 1 # The authors of this work have released all rights to it and placed it
 2 # in the public domain under the Creative Commons CC0 1.0 waiver
 3 # (http://creativecommons.org/publicdomain/zero/1.0/).
 4 # 
 5 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 # 
13 # Retrieved from: http://en.literateprograms.org/Markov_algorithm_simulator_(Python)?oldid=19173
14 
15 import string
16 successor = [("aL", "La", False),
17              ("a0", "0a", False),
18              ("a" , "b" , False),
19              ("Lb", "b0", False),
20              ("0b", "L" , True ),
21              ("b" , "L" , True ),
22              (""  , "a" , False)]
23 def contains(s, sub):
24     return s.find(sub) != -1
25 def find_rule(a, w):
26     try:
27         return filter(lambda (l,r,b): contains(w, l), a)[0]
28     except IndexError:
29         raise ValueError, "not found"
30 def apply_rule((l,r,b), s):
31     return s.replace(l, r, 1)
32 def apply_alg(a, w):
33     l,r,b = r = find_rule(a, w)
34     return apply_rule(r, w), b
35 def run(a, w):
36     result = []
37     flag = False                      # whether Halting rule was applied
38     try:
39         while not flag:               # Normal rule was applied
40             result.append(w)
41             w, flag = apply_alg(a, w) # apply a rule
42         result.append(w)
43     except ValueError:                # No rule was applied
44         pass
45     return result
46 


hijacker
hijacker
hijacker
hijacker