Execute a Markov algorithm: Difference between revisions
Content added Content deleted
m (→{{header|J}}: note implementation passes all test, including stretch goals) |
(→{{header|Python}}: Add the last stretch goal to the assertions.) |
||
Line 525: | Line 525: | ||
the shop -> my brother |
the shop -> my brother |
||
a never used -> .terminating rule |
a never used -> .terminating rule |
||
''' |
|||
grammar4 = '''\ |
|||
### Unary Multiplication Engine, for testing Markov Algorithm implementations |
|||
### By Donal Fellows. |
|||
# Unary addition engine |
|||
_+1 -> _1+ |
|||
1+1 -> 11+ |
|||
# Pass for converting from the splitting of multiplication into ordinary |
|||
# addition |
|||
1! -> !1 |
|||
,! -> !+ |
|||
_! -> _ |
|||
# Unary multiplication by duplicating left side, right side times |
|||
1*1 -> x,@y |
|||
1x -> xX |
|||
X, -> 1,1 |
|||
X1 -> 1X |
|||
_x -> _X |
|||
,x -> ,X |
|||
y1 -> 1y |
|||
y_ -> _ |
|||
# Next phase of applying |
|||
1@1 -> x,@y |
|||
1@_ -> @_ |
|||
,@_ -> !_ |
|||
++ -> + |
|||
# Termination cleanup for addition |
|||
_1 -> 1 |
|||
1+_ -> 1 |
|||
_+_ -> |
|||
''' |
''' |
||
Line 530: | Line 561: | ||
text2 = "I bought a B of As W my Bgage from T S." |
text2 = "I bought a B of As W my Bgage from T S." |
||
text3 = '_1111*11111_' |
|||
def extractreplacements(grammar): |
def extractreplacements(grammar): |
||
Line 552: | Line 585: | ||
assert replace(text1, extractreplacements(grammar2)) \ |
assert replace(text1, extractreplacements(grammar2)) \ |
||
== 'I bought a bag of apples from T shop.' |
== 'I bought a bag of apples from T shop.' |
||
# Stretch |
# Stretch goals |
||
assert replace(text2, extractreplacements(grammar3)) \ |
assert replace(text2, extractreplacements(grammar3)) \ |
||
== 'I bought a bag of apples with my money from T shop.' |
== 'I bought a bag of apples with my money from T shop.' |
||
assert replace(text3, extractreplacements(grammar4)) \ |
|||
== '11111111111111111111' |
|||
</lang> |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |