2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								id: 59e09e6d412c5939baa02d16
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								title: Execute a Markov algorithm
							 
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								challengeType: 5
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								forumTopicId: 302260
							 
						 
					
						
							
								
									
										
										
										
											2021-01-13 03:31:00 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								dashedName: execute-a-markov-algorithm
							 
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-12-16 00:37:30 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# --description--
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Create an interpreter for a [Markov Algorithm ](https://en.wikipedia.org/wiki/Markov algorithm "wp: Markov algorithm" ).
							 
						 
					
						
							
								
									
										
										
										
											2020-12-16 00:37:30 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Rules have the syntax:
							 
						 
					
						
							
								
									
										
										
										
											2020-12-16 00:37:30 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-01-09 02:59:18 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< pre > [ruleset] ::= (([comment] | [rule]) [newline]+)* 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								[comment] ::= # {[any character]}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								[rule] ::= [pattern] [whitespace] -> [whitespace] [.] [replacement]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								[whitespace] ::= ([tab] | [space]) [[whitespace]]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / pre >  
						 
					
						
							
								
									
										
										
										
											2020-12-16 00:37:30 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								There is one rule per line.
							 
						 
					
						
							
								
									
										
										
										
											2020-12-16 00:37:30 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								If there is a `.`  (period) present before the \[replacement], then this is a terminating rule in which case the interpreter must halt execution.
							 
						 
					
						
							
								
									
										
										
										
											2020-12-16 00:37:30 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								A ruleset consists of a sequence of rules, with optional comments.
							 
						 
					
						
							
								
									
										
										
										
											2020-12-16 00:37:30 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Rulesets
							 
						 
					
						
							
								
									
										
										
										
											2020-12-16 00:37:30 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Use the following tests on entries:
							 
						 
					
						
							
								
									
										
										
										
											2020-12-16 00:37:30 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								**Ruleset 1:**
							 
						 
					
						
							
								
									
										
										
										
											2020-12-16 00:37:30 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< pre > # This rules file is extracted from Wikipedia: 
						 
					
						
							
								
									
										
										
										
											2020-12-16 00:37:30 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# <code>http://en.wikipedia.org/wiki/Markov_Algorithm</code>
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								A -> apple
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								B -> bag
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								S -> shop
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								T -> the
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								the shop -> my brother
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								a never used -> .terminating rule
							 
						 
					
						
							
								
									
										
										
										
											2020-12-16 00:37:30 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< / pre >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-07-15 13:04:11 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Sample text of `I bought a B of As from T S.`  should generate the output `I bought a bag of apples from my brother.` 
							 
						 
					
						
							
								
									
										
										
										
											2021-01-09 02:59:18 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								**Ruleset 2:**
							 
						 
					
						
							
								
									
										
										
										
											2021-01-09 02:59:18 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								A test of the terminating rule
							 
						 
					
						
							
								
									
										
										
										
											2021-01-09 02:59:18 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< pre > # Slightly modified from the rules on Wikipedia 
						 
					
						
							
								
									
										
										
										
											2020-12-16 00:37:30 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								A -> apple
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								B -> bag
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								S -> .shop
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								T -> the
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								the shop -> my brother
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								a never used -> .terminating rule
							 
						 
					
						
							
								
									
										
										
										
											2020-12-16 00:37:30 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< / pre >  
						 
					
						
							
								
									
										
										
										
											2021-01-09 02:59:18 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-07-15 13:04:11 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Sample text of `I bought a B of As from T S.`  should generate `I bought a bag of apples from T shop.` 
							 
						 
					
						
							
								
									
										
										
										
											2021-01-09 02:59:18 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								**Ruleset 3:**
							 
						 
					
						
							
								
									
										
										
										
											2021-01-09 02:59:18 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								This tests for correct substitution order and may trap simple regexp based replacement routines if special regexp characters are not escaped.
							 
						 
					
						
							
								
									
										
										
										
											2021-01-09 02:59:18 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-12-16 00:37:30 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< pre > # BNF Syntax testing rules 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								A -> apple
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								WWWW -> with
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Bgage -> ->.*
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								B -> bag
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								->.* -> money
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								W -> WW
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								S -> .shop
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								T -> the
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								the shop -> my brother
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								a never used -> .terminating rule
							 
						 
					
						
							
								
									
										
										
										
											2021-01-09 02:59:18 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< / pre >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-07-15 13:04:11 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Sample text of `I bought a B of As W my Bgage from T S.`  should generate `I bought a bag of apples with my money from T shop.` 
							 
						 
					
						
							
								
									
										
										
										
											2021-01-09 02:59:18 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								**Ruleset 4:**
							 
						 
					
						
							
								
									
										
										
										
											2021-01-09 02:59:18 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								This tests for correct order of scanning of rules, and may trap replacement routines that scan in the wrong order. It implements a general unary multiplication engine. (Note that the input expression must be placed within underscores in this implementation.)
							 
						 
					
						
							
								
									
										
										
										
											2021-01-09 02:59:18 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< pre > ### Unary Multiplication Engine, for testing Markov Algorithm implementations 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								### By Donal Fellows.
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# Unary addition engine
  
						 
					
						
							
								
									
										
										
										
											2021-01-09 02:59:18 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								_+1 -> _1+
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								1+1 -> 11+
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# Pass for converting from the splitting of multiplication into ordinary
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# addition
  
						 
					
						
							
								
									
										
										
										
											2021-01-09 02:59:18 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								1! -> !1
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								,! -> !+
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								_! -> _
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# Unary multiplication by duplicating left side, right side times
  
						 
					
						
							
								
									
										
										
										
											2021-01-09 02:59:18 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								1*1 -> x,@y 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								1x -> xX
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								X, -> 1,1
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								X1 -> 1X
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								_x -> _X
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								,x -> ,X
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								y1 -> 1y
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								y_ -> _
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# Next phase of applying
  
						 
					
						
							
								
									
										
										
										
											2021-01-09 02:59:18 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								1@1  -> x,@y 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								1@_  -> @_ 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								,@_  -> !_
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								++ -> +
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# Termination cleanup for addition
  
						 
					
						
							
								
									
										
										
										
											2021-01-09 02:59:18 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								_1 -> 1
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								1+_ -> 1
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								_+_ ->
							 
						 
					
						
							
								
									
										
										
										
											2020-12-16 00:37:30 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< / pre >  
						 
					
						
							
								
									
										
										
										
											2021-01-09 02:59:18 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-07-15 13:04:11 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Sample text of `_1111*11111_`  should generate the output `11111111111111111111` 
							 
						 
					
						
							
								
									
										
										
										
											2021-01-09 02:59:18 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								**Ruleset 5:**
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								A simple [Turing machine ](http://en.wikipedia.org/wiki/Turing_machine "link: http://en.wikipedia.org/wiki/Turing_machine" ), implementing a three-state [busy beaver ](http://en.wikipedia.org/wiki/Busy_beaver "link: http://en.wikipedia.org/wiki/Busy_beaver" ).
							 
						 
					
						
							
								
									
										
										
										
											2021-01-09 02:59:18 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								The tape consists of `0` s and `1` s, the states are `A` , `B` , `C`  and `H`  (for `H` alt), and the head position is indicated by writing the state letter before the character where the head is. All parts of the initial tape the machine operates on have to be given in the input.
							 
						 
					
						
							
								
									
										
										
										
											2021-01-09 02:59:18 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Besides demonstrating that the Markov algorithm is Turing-complete, it also made me catch a bug in the C++ implementation which wasn't caught by the first four rulesets.
							 
						 
					
						
							
								
									
										
										
										
											2021-01-09 02:59:18 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< pre > # Turing machine: three-state busy beaver 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								#
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# state A, symbol 0 => write 1, move right, new state B
  
						 
					
						
							
								
									
										
										
										
											2021-01-09 02:59:18 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								A0 -> 1B
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# state A, symbol 1 => write 1, move left, new state C
  
						 
					
						
							
								
									
										
										
										
											2021-01-09 02:59:18 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								0A1 -> C01
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								1A1 -> C11
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# state B, symbol 0 => write 1, move left, new state A
  
						 
					
						
							
								
									
										
										
										
											2021-01-09 02:59:18 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								0B0 -> A01
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								1B0 -> A11
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# state B, symbol 1 => write 1, move right, new state B
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								B1 -> 1B
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# state C, symbol 0 => write 1, move left, new state B
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								0C0 -> B01
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								1C0 -> B11
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# state C, symbol 1 => write 1, move left, halt
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								0C1 -> H01
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								1C1 -> H11
							 
						 
					
						
							
								
									
										
										
										
											2021-01-09 02:59:18 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< / pre >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-07-15 13:04:11 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								This ruleset should turn `000000A000000`  into `00011H1111000` 
							 
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-12-16 00:37:30 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# --hints--
  
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								`markov`  should be a function. 
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-12-16 00:37:30 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								assert(typeof markov === 'function');
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								`markov(["A -> apple","B -> bag","S -> shop","T -> the","the shop -> my brother","a never used -> .terminating rule"],"I bought a B of As from T S.")`  should return "I bought a bag of apples from my brother.". 
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2020-12-16 00:37:30 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								assert.deepEqual(markov(rules[0], tests[0]), outputs[0]);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								`markov(["A -> apple","B -> bag","S -> .shop","T -> the","the shop -> my brother","a never used -> .terminating rule"],"I bought a B of As from T S.")`  should return "I bought a bag of apples from T shop.". 
						 
					
						
							
								
									
										
										
										
											2020-12-16 00:37:30 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								assert.deepEqual(markov(rules[1], tests[1]), outputs[1]);
							 
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								`markov(["A -> apple","WWWW -> with","Bgage -> ->.*","B -> bag","->.* -> money","W -> WW","S -> .shop","T -> the","the shop -> my brother","a never used -> .terminating rule"],"I bought a B of As W my Bgage from T S.")`  should return "I bought a bag of apples with my money from T shop.". 
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-12-16 00:37:30 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								assert.deepEqual(markov(rules[2], tests[2]), outputs[2]);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								`markov(["_+1 -> _1+","1+1 -> 11+","1! -> !1",",! -> !+","_! -> _","1*1 -> x,@y","1x -> xX","X, -> 1,1","X1 -> 1X","_x -> _X",",x -> ,X","y1 -> 1y","y_ -> _","1@1 -> x,@y","1@_ -> @_",",@_ -> !_","++ -> +","_1 -> 1","1+_ -> 1","_+_ -> "],"_1111*11111_")`  should return "11111111111111111111". 
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-12-16 00:37:30 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								assert.deepEqual(markov(rules[3], tests[3]), outputs[3]);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								`markov(["A0 -> 1B","0A1 -> C01","1A1 -> C11","0B0 -> A01","1B0 -> A11","B1 -> 1B","0C0 -> B01","1C0 -> B11","0C1 -> H01","1C1 -> H11"],"")`  should return "00011H1111000". 
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2020-12-16 00:37:30 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								assert.deepEqual(markov(rules[4], tests[4]), outputs[4]);
							 
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2020-08-13 17:24:35 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-01-13 03:31:00 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# --seed--
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								## --after-user-code--
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								let rules=[["A -> apple","B -> bag","S -> shop","T -> the","the shop -> my brother","a never used -> .terminating rule"],
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            ["A -> apple","B -> bag","S -> .shop","T -> the","the shop -> my brother","a never used -> .terminating rule"],
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            ["A -> apple","WWWW -> with","Bgage -> ->.*","B -> bag","->.* -> money","W -> WW","S -> .shop","T -> the","the shop -> my brother","a never used -> .terminating rule"],
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            ["_+1 -> _1+","1+1 -> 11+","1! -> !1",",! -> !+","_ ! -> _","1*1 -> x,@y ","1x -> xX","X, -> 1,1","X1 -> 1X","_x -> _X",",x -> ,X","y1 -> 1y","y_  -> _","1@1 -> x,@y","1@_  -> @_ ",",@_  -> !_","++ -> +","_1 -> 1","1+_ -> 1","_+_ -> "],
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            ["A0 -> 1B","0A1 -> C01","1A1 -> C11","0B0 -> A01","1B0 -> A11","B1 -> 1B","0C0 -> B01","1C0 -> B11","0C1 -> H01","1C1 -> H11"]];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								let tests=["I bought a B of As from T S.",
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            "I bought a B of As from T S.",
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            "I bought a B of As W my Bgage from T S.",
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            "_1111*11111_",
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            "000000A000000"];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								let outputs=["I bought a bag of apples from my brother.",
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            "I bought a bag of apples from T shop.",
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            "I bought a bag of apples with my money from T shop.",
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            "11111111111111111111",
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            "00011H1111000"]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-01-13 03:31:00 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								## --seed-contents--
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								function markov(rules,test) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-12-16 00:37:30 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# --solutions--
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-01-13 03:31:00 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								function markov(rules,test) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    let pattern = new RegExp("^([^#]*?)\\s+->\\s+(\\.?)(.*)");
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    let origTest = test;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    let captures = [];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    rules.forEach(function(rule){
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        let m = pattern.exec(rule);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        for (let j = 0; j <  m.length ;  j + + ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            m[j] = m[j + 1];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        captures.push(m);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    });
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    test = origTest;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    let copy = test;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    for (let j = 0; j <  captures.length ;  j + + )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        let c = captures[j];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        test = test.replace(c[0], c[2]);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        if (c[1]==".")
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            break;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        if (test!=copy) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            j = -1;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            copy = test;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return test;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```