freeCodeCamp/curriculum/challenges/chinese/08-coding-interview-prep/rosetta-code/execute-a-markov-algorithm....

8.7 KiB
Raw Blame History

title id challengeType videoUrl localeTitle
Execute a Markov algorithm 59e09e6d412c5939baa02d16 5 执行马尔可夫算法

Description

任务:

马尔可夫算法创建解释器。

规则具有以下语法:

:: = | +*

:: ={ }

:: = - > [。]

:: = | [ ]

每行有一条规则。

如果有的话 (期间)出席之前 ,那么这是一个终止规则,在这种情况下,解释器必须停止执行。

规则集由一系列规则和可选注释组成。

规则集

对条目使用以下测试:

规则集1
此规则文件是从Wikipedia中提取的
http://en.wikipedia.org/wiki/Markov_AlgorithmA  - > apple
B  - >包
S  - >店
T  - >
商店 - >我的兄弟
一个从未使用过的 - >。终止规则

示例文本:

I bought a B of As from T S.

应该生成输出:

I bought a bag of apples from my brother.

规则集2

终止规则的测试

从维基百科的规则略有修改 - >苹果
B  - >包
S  - > .shop
T  - >
商店 - >我的兄弟
一个从未使用过的 - >。终止规则

示例文本:

I bought a B of As from T S.

应该生成:

I bought a bag of apples from T shop.

规则集3

如果没有转义特殊的正则表达式字符,这将测试正确的替换顺序并可能捕获简单的基于正则表达式的替换例程。

 BNF语法测试规则A  - > apple
WWWW  - >用
行李 - >  - >。*
B  - >包
 - >。*  - >钱
W  - > WW
S  - > .shop
T  - >
商店 - >我的兄弟
一个从未使用过的 - >。终止规则

示例文本:

I bought a B of As W my Bgage from T S.

应该生成:

I bought a bag of apples with my money from T shop.

规则集4

这将测试规则扫描的正确顺序,并可能捕获以错误顺序扫描的替换例程。它实现了一般的一元乘法引擎。 (请注意,在此实现中,输入表达式必须放在下划线中。)

 ##一元乘法引擎,用于测试马尔可夫算法实现
## Donal Fellows。
一元加法引擎_ + 1  - > _1 +
1 + 1  - > 11+
通过将乘法的分裂转换为普通的
addition1 - >1
 - >+
_ - > _
通过复制左侧右侧乘以1 * 1  - > x@ y进行一元乘法
1x  - > xX
X - > 1,1
X1  - > 1X
_x  - > _X
x  - >X
y1  - > 1y
y_  - > _
apply1 @ 1  - > x@ y的下一阶段
1 @ _  - > @_
@ _  - >_
++  - > +
添加终止清除_1  - > 1
1 + _  - > 1
_ + _  - >

示例文本:

_1111*11111_

应该生成输出:

11111111111111111111

规则集5

一台简单的图灵机

实施三态繁忙的海狸

磁带由0和1组成状态为ABC和H对于Halt并且通过在头部所在的字符之前写入状态字母来指示磁头位置。

必须在输入中给出机器操作的初始磁带的所有部分。

除了证明Markov算法是Turing-complete之外它还让我抓住了C ++实现中的一个错误,这个错误没有被前四个规则集捕获。

图灵机:三态繁忙的海狸
#sta A符号0 =>写1向右移动新状态BA0  - > 1B
状态A符号1 =>写1向左移动新状态C0A1  - > C01
1A1  - > C11
状态B符号0 =>写1向左移动新状态A0B0  - > A01
1B0  - > A11
状态B符号1 =>写1向右移动新状态BB1  - > 1B
状态C符号0 =>写1向左移动新状态B0C0  - > B01
1C0  - > B11
状态C符号1 =>写1向左移动停止0C1  - > H01
1C1  - > H11

这个规则集应该转向

000000A000000

00011H1111000

Instructions

Tests

tests:
  - text: <code>markov</code>是一个功能。
    testString: 'assert(typeof markov === "function", "<code>markov</code> is a function.");'
  - text: '<code>markov([&quot;A -&gt; apple&quot;,&quot;B -&gt; bag&quot;,&quot;S -&gt; shop&quot;,&quot;T -&gt; the&quot;,&quot;the shop -&gt; my brother&quot;,&quot;a never used -&gt; .terminating rule&quot;],&quot;I bought a B of As from T S.&quot;)</code>应该回复“我从我兄弟那里买了一袋苹果”。'
    testString: 'assert.deepEqual(markov(rules[0],tests[0]),outputs[0],"<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.".");'
  - text: '<code>markov([&quot;A -&gt; apple&quot;,&quot;B -&gt; bag&quot;,&quot;S -&gt; .shop&quot;,&quot;T -&gt; the&quot;,&quot;the shop -&gt; my brother&quot;,&quot;a never used -&gt; .terminating rule&quot;],&quot;I bought a B of As from T S.&quot;)</code>应该回来“我从T商店买了一袋苹果。”'
    testString: 'assert.deepEqual(markov(rules[1],tests[1]),outputs[1],"<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.".");'
  - text: '<code>markov([&quot;A -&gt; apple&quot;,&quot;WWWW -&gt; with&quot;,&quot;Bgage -&gt; -&gt;.*&quot;,&quot;B -&gt; bag&quot;,&quot;-&gt;.* -&gt; money&quot;,&quot;W -&gt; WW&quot;,&quot;S -&gt; .shop&quot;,&quot;T -&gt; the&quot;,&quot;the shop -&gt; my brother&quot;,&quot;a never used -&gt; .terminating rule&quot;],&quot;I bought a B of As W my Bgage from T S.&quot;)</code>应该返回”我从T商店用我的钱买了一袋苹果。“'
    testString: 'assert.deepEqual(markov(rules[2],tests[2]),outputs[2],"<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.".");'
  - text: '<code>markov([&quot;_+1 -&gt; _1+&quot;,&quot;1+1 -&gt; 11+&quot;,&quot;1! -&gt; !1&quot;,&quot;,! -&gt; !+&quot;,&quot;_! -&gt; _&quot;,&quot;1*1 -&gt; x,@y&quot;,&quot;1x -&gt; xX&quot;,&quot;X, -&gt; 1,1&quot;,&quot;X1 -&gt; 1X&quot;,&quot;_x -&gt; _X&quot;,&quot;,x -&gt; ,X&quot;,&quot;y1 -&gt; 1y&quot;,&quot;y_ -&gt; _&quot;,&quot;1@1 -&gt; x,@y&quot;,&quot;1@_ -&gt; @_&quot;,&quot;,@_ -&gt; !_&quot;,&quot;++ -&gt; +&quot;,&quot;_1 -&gt; 1&quot;,&quot;1+_ -&gt; 1&quot;,&quot;_+_ -&gt; &quot;],&quot;_1111*11111_&quot;)</code>应返回”11111111111111111111“。'
    testString: 'assert.deepEqual(markov(rules[3],tests[3]),outputs[3],"<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".");'
  - text: '<code>markov([&quot;A0 -&gt; 1B&quot;,&quot;0A1 -&gt; C01&quot;,&quot;1A1 -&gt; C11&quot;,&quot;0B0 -&gt; A01&quot;,&quot;1B0 -&gt; A11&quot;,&quot;B1 -&gt; 1B&quot;,&quot;0C0 -&gt; B01&quot;,&quot;1C0 -&gt; B11&quot;,&quot;0C1 -&gt; H01&quot;,&quot;1C1 -&gt; H11&quot;],&quot;&quot;)</code>应返回”00011H1111000“。'
    testString: 'assert.deepEqual(markov(rules[4],tests[4]),outputs[4],"<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".");'

Challenge Seed

function markov (rules,test) {
  // Good luck!
}

Solution

// solution required