2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								title: Execute a Markov algorithm
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								id: 59e09e6d412c5939baa02d16
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								challengeType: 5
							 
						 
					
						
							
								
									
										
										
										
											2020-05-21 17:31:25 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								isHidden: false
							 
						 
					
						
							
								
									
										
										
										
											2019-08-05 09:17:33 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								forumTopicId: 302260
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								## Description
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								< section  id = 'description' > 
							 
						 
					
						
							
								
									
										
										
										
											2019-03-02 17:42:56 +09:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								Create an interpreter for a < a  href = "https://en.wikipedia.org/wiki/Markov algorithm"  title = "wp: Markov algorithm"  target = "_blank" > Markov Algorithm< / a > .
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								Rules have the syntax:
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								< pre > 
							 
						 
					
						
							
								
									
										
										
										
											2019-03-02 17:42:56 +09:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								[ruleset] ::= (([comment] | [rule]) [newline]+)*
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								[comment] ::= # {[any character]}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								[rule] ::= [pattern] [whitespace] -> [whitespace] [.] [replacement]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								[whitespace] ::= ([tab] | [space]) [[whitespace]]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								< / pre > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								There is one rule per line.
							 
						 
					
						
							
								
									
										
										
										
											2019-06-14 20:04:16 +09:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								If there is a < code > .< / code >  (period) present before the  [replacement], then this is a terminating rule in which case the interpreter must halt execution.
							 
						 
					
						
							
								
									
										
										
										
											2019-03-02 17:42:56 +09:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								A ruleset consists of a sequence of rules, with optional comments.
							 
						 
					
						
							
								
									
										
										
										
											2019-07-19 11:04:45 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								< span  style = "font-size: 1.5rem" > Rulesets< / span > 
							 
						 
					
						
							
								
									
										
										
										
											2019-03-02 17:42:56 +09:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								Use the following tests on entries:
							 
						 
					
						
							
								
									
										
										
										
											2019-06-14 20:04:16 +09:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								< strong > Ruleset 1:< / strong > 
							 
						 
					
						
							
								
									
										
										
										
											2019-03-02 17:42:56 +09:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								< pre > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								# This rules file is extracted from Wikipedia:
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								# http://en.wikipedia.org/wiki/Markov_Algorithm
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								A -> apple
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								B -> bag
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								S -> shop
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								T -> the
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								the shop -> my brother
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								a never used -> .terminating rule
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								< / pre > 
							 
						 
					
						
							
								
									
										
										
										
											2019-03-02 17:42:56 +09:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								Sample text of:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								< code > I bought a B of As from T S.< / code > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								Should generate the output:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								< code > I bought a bag of apples from my brother.< / code > 
							 
						 
					
						
							
								
									
										
										
										
											2019-06-14 20:04:16 +09:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								< strong > Ruleset 2:< / strong > 
							 
						 
					
						
							
								
									
										
										
										
											2019-03-02 17:42:56 +09:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								A test of the terminating rule
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								< pre > 
							 
						 
					
						
							
								
									
										
										
										
											2019-03-02 17:42:56 +09:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								# Slightly modified from the rules on Wikipedia
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								A -> apple
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								B -> bag
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								S -> .shop
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								T -> the
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								the shop -> my brother
							 
						 
					
						
							
								
									
										
										
										
											2019-03-02 17:42:56 +09:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								a never used -> .terminating rule
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								< / pre > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								Sample text of:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								< code > I bought a B of As from T S.< / code > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								Should generate:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								< code > I bought a bag of apples from T shop.< / code > 
							 
						 
					
						
							
								
									
										
										
										
											2019-06-14 20:04:16 +09:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								< strong > Ruleset 3:< / strong > 
							 
						 
					
						
							
								
									
										
										
										
											2019-03-02 17:42:56 +09:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								This tests for correct substitution order and may trap simple regexp based replacement routines if special regexp characters are not escaped.
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								< pre > 
							 
						 
					
						
							
								
									
										
										
										
											2019-07-19 11:04:45 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								# BNF Syntax testing rules
 
							 
						 
					
						
							
								
									
										
										
										
											2019-03-02 17:42:56 +09:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								A -> apple
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								WWWW -> with
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								Bgage -> ->.*
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								B -> bag
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								->.* -> money
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								W -> WW
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								S -> .shop
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								T -> the
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								the shop -> my brother
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								a never used -> .terminating rule
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								< / pre > 
							 
						 
					
						
							
								
									
										
										
										
											2019-03-02 17:42:56 +09:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								Sample text of:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								< code > I bought a B of As W my Bgage from T S.< / code > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								Should generate:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								< code > I bought a bag of apples with my money from T shop.< / code > 
							 
						 
					
						
							
								
									
										
										
										
											2019-06-14 20:04:16 +09:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								< strong > Ruleset 4:< / strong > 
							 
						 
					
						
							
								
									
										
										
										
											2019-03-02 17:42:56 +09: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.)
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								< pre > 
							 
						 
					
						
							
								
									
										
										
										
											2019-03-02 17:42:56 +09:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								### Unary Multiplication Engine, for testing Markov Algorithm implementations
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								### By Donal Fellows.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								# Unary addition engine
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								_+1 -> _1+
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								1+1 -> 11+
							 
						 
					
						
							
								
									
										
										
										
											2019-03-02 17:42:56 +09:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								# Pass for converting from the splitting of multiplication into ordinary
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								# addition
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								1! -> !1
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								,! -> !+
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								_! -> _
							 
						 
					
						
							
								
									
										
										
										
											2019-03-02 17:42:56 +09:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								# Unary multiplication by duplicating left side, right side times1*1 -> x,@y
 
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								1x -> xX
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								X, -> 1,1
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								X1 -> 1X
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								_x -> _X
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								,x -> ,X
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								y1 -> 1y
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								y_ -> _
							 
						 
					
						
							
								
									
										
										
										
											2019-03-02 17:42:56 +09:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								# Next phase of applying1@1 -> x,@y
 
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								1@_  -> @_ 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								,@_  -> !_
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								++ -> +
							 
						 
					
						
							
								
									
										
										
										
											2019-03-02 17:42:56 +09:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								# Termination cleanup for addition_1 -> 1
 
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								1+_ -> 1
							 
						 
					
						
							
								
									
										
										
										
											2018-10-08 01:01:53 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								_+_ ->
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								< / pre > 
							 
						 
					
						
							
								
									
										
										
										
											2019-03-02 17:42:56 +09:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								Sample text of:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								< code > _1111*11111_< / code > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								should generate the output:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								< code > 11111111111111111111< / code > 
							 
						 
					
						
							
								
									
										
										
										
											2019-06-14 20:04:16 +09:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								< strong > Ruleset 5:< / strong > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								A simple < a  href = "http://en.wikipedia.org/wiki/Turing_machine"  title = "link: http://en.wikipedia.org/wiki/Turing_machine"  target = "_blank" > Turing machine< / a > , implementing a three-state < a  href = "http://en.wikipedia.org/wiki/Busy_beaver"  title = "link: http://en.wikipedia.org/wiki/Busy_beaver"  target = "_blank" > busy beaver< / a > .
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								The tape consists of < code > 0< / code > s and < code > 1< / code > s, the states are < code > A< / code > , < code > B< / code > , < code > C< / code >  and < code > H< / code >  (for < code > H< / code > 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.
							 
						 
					
						
							
								
									
										
										
										
											2019-03-02 17:42:56 +09: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.
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								< pre > 
							 
						 
					
						
							
								
									
										
										
										
											2019-07-19 11:04:45 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								# Turing machine: three-state busy beaver
 
							 
						 
					
						
							
								
									
										
										
										
											2019-03-02 17:42:56 +09:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								#
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								# state A, symbol 0 => write 1, move right, new state B
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								A0 -> 1B
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								# state A, symbol 1 => write 1, move left, new state C
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								0A1 -> C01
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								1A1 -> C11
							 
						 
					
						
							
								
									
										
										
										
											2019-03-02 17:42:56 +09:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								# state B, symbol 0 => write 1, move left, new state A
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								0B0 -> A01
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								1B0 -> A11
							 
						 
					
						
							
								
									
										
										
										
											2019-03-02 17:42:56 +09: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
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								1C0 -> B11
							 
						 
					
						
							
								
									
										
										
										
											2019-03-02 17:42:56 +09:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								# state C, symbol 1 => write 1, move left, halt
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								0C1 -> H01
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								1C1 -> H11
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								< / pre > 
							 
						 
					
						
							
								
									
										
										
										
											2019-03-02 17:42:56 +09:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								This ruleset should turn
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								< code > 000000A000000< / code > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								into
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								< code > 00011H1111000< / code > 
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								< / section > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								## Instructions
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								< section  id = 'instructions' > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								< / section > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								## Tests
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								< section  id = 'tests' > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```yml
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								tests:
							 
						 
					
						
							
								
									
										
										
										
											2019-11-20 07:01:31 -08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								  -  text: < code > markov</ code >  should be a function.
							 
						 
					
						
							
								
									
										
										
										
											2019-07-26 05:24:52 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								    testString: assert(typeof markov === 'function');
							 
						 
					
						
							
								
									
										
										
										
											2018-10-20 21:02:47 +03:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								  -  text: < code > 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.")</ code >  should return "I bought a bag of apples from my brother.".
							 
						 
					
						
							
								
									
										
										
										
											2019-07-26 05:24:52 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								    testString: assert.deepEqual(markov(rules[0],tests[0]),outputs[0]);
							 
						 
					
						
							
								
									
										
										
										
											2018-10-20 21:02:47 +03:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								  -  text: < code > 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.")</ code >  should return "I bought a bag of apples from T shop.".
							 
						 
					
						
							
								
									
										
										
										
											2019-07-26 05:24:52 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								    testString: assert.deepEqual(markov(rules[1],tests[1]),outputs[1]);
							 
						 
					
						
							
								
									
										
										
										
											2018-10-20 21:02:47 +03:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								  -  text: < code > 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.")</ code >  should return "I bought a bag of apples with my money from T shop.".
							 
						 
					
						
							
								
									
										
										
										
											2019-07-26 05:24:52 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								    testString: assert.deepEqual(markov(rules[2],tests[2]),outputs[2]);
							 
						 
					
						
							
								
									
										
										
										
											2018-10-20 21:02:47 +03:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								  -  text: < code > 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_")</ code >  should return "11111111111111111111".
							 
						 
					
						
							
								
									
										
										
										
											2019-07-26 05:24:52 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								    testString: assert.deepEqual(markov(rules[3],tests[3]),outputs[3]);
							 
						 
					
						
							
								
									
										
										
										
											2018-10-20 21:02:47 +03:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								  -  text: < code > markov(["A0 -> 1B","0A1 -> C01","1A1 -> C11","0B0 -> A01","1B0 -> A11","B1 -> 1B","0C0 -> B01","1C0 -> B11","0C1 -> H01","1C1 -> H11"],"")</ code >  should return "00011H1111000".
							 
						 
					
						
							
								
									
										
										
										
											2019-07-26 05:24:52 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								    testString: assert.deepEqual(markov(rules[4],tests[4]),outputs[4]);
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								< / section > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								## Challenge Seed
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								< section  id = 'challengeSeed' > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								< div  id = 'js-seed' > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2019-03-02 17:42:56 +09:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								function markov(rules,test) {
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								  // Good luck!
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								< / div > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								< / section > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								## Solution
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								< section  id = 'solution' > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								function markov(rules,test) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    let pattern = new RegExp("^([^#]*?)\\s+->\\s+(\\.?)(.*)");
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    let origTest = test;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    let captures = [];
							 
						 
					
						
							
								
									
										
										
										
											2018-10-08 01:01:53 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								    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;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								// tail:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								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"];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								< / section >