图灵机代码高尔夫
好了大家,今天的目标是构建一个图灵机模拟器。对于那些不知道它是什么的人,请参阅维基百科文章。我们今天使用的状态表位于作为该页面一部分的正式定义< /a>.
该代码将采用“0”和“1”字符串字符序列、表示机器启动字符的整数和表示程序状态的整数(无特定顺序),并输出最终结果对字符串的操作以及最终位置。示例:
示例 1:
1010 state A(0)
^ (3)
1011 state B(1)
^ (2)
1011 state B(1)
^ (1)
1111 state A(0)
^ (2)
1111 state C(0)
^ (3)
1111 HALT
^ (2)
示例 2:
110100 state B(1)
^ (3)
110100 state B(1)
^ (2)
111100 state A(0)
^ (3)
111100 state C(2)
^ (4)
111110 state B(1)
^ (5)
1111110 state A(0)
^ (6, tape has been extended to right)
1111111 state B(1)
^ (5)
1111111 state B(1)
^ (4)
1111111 state B(1)
^ (3)
1111111 state B(1)
^ (2)
1111111 state B(1)
^ (1)
1111111 state B(1)
^ (0)
01111111 state B(1)
^ (0, tape has been extended to left)
11111111 state A(0)
^ (1)
11111111 state C(2)
^ (2)
11111111 HALT
^ (1)
其他:
- 您的代码必须通过根据需要扩展字符串来正确处理写入磁带上“空白”的尝试。
- 由于指定的状态机未指定任何类型的“空白磁带”操作,因此将所有空白值视为 0。
- 您必须仅计算处理具有初始状态的字符串的评估的方法,如何输出该数据取决于您。
- 在磁带上向右移动是向上递增(字符串位置 0 一直在左侧),状态 0 是 A,状态 1 是 B,状态 2 是 C。
(希望)最终编辑: 对于我在这个问题上造成的混乱和麻烦,我表示最诚挚的歉意:我误读了我列出的提供的状态表,并将其弄反了。希望您能原谅我浪费了您的时间;这完全是无意的!
Ok guys, today's goal is to build a Turing machine simulator. For those that don't know what it is, see the Wikipedia article. The state table we are using today is found at the end of the Formal Definition that's part of that page.
The code will take a sequence of "0" and "1" string characters, an integer representing the character that the machine starts with, and an integer representing the state of the program (in no particular order), and output the final result of the operations on the string, as well as the final position. Examples:
Example 1:
1010 state A(0)
^ (3)
1011 state B(1)
^ (2)
1011 state B(1)
^ (1)
1111 state A(0)
^ (2)
1111 state C(0)
^ (3)
1111 HALT
^ (2)
Example 2:
110100 state B(1)
^ (3)
110100 state B(1)
^ (2)
111100 state A(0)
^ (3)
111100 state C(2)
^ (4)
111110 state B(1)
^ (5)
1111110 state A(0)
^ (6, tape has been extended to right)
1111111 state B(1)
^ (5)
1111111 state B(1)
^ (4)
1111111 state B(1)
^ (3)
1111111 state B(1)
^ (2)
1111111 state B(1)
^ (1)
1111111 state B(1)
^ (0)
01111111 state B(1)
^ (0, tape has been extended to left)
11111111 state A(0)
^ (1)
11111111 state C(2)
^ (2)
11111111 HALT
^ (1)
Misc:
- Your code must properly handle attempts to write into "blank spaces" on the tape, by extending the string as necessary.
- Since the state machine specified does not specify any sort of "blank tape" action, treat all blank values as 0.
- You must count only the method that handles evaluation of a string with initial state, how you output that data is up to you.
- Moving right on the tape is incrementing up (string position 0 is all the way at the left), state 0 is A, state 1 is B, and state 2 is C.
(hopefully) final edit:
I offer my most sincere apologies as to the confusion and trouble I've caused with this question: I misread the supplied state table I listed, and got it backwards. I hope you'll forgive me for wasting your time; it was entirely unintentional!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
Python - 133 个字符
至少必须击败 perl 一段时间:)
Python - 172 个字符
测试用例
Python - 133 Characters
Have to beat perl for a while at least :)
Python - 172 Characters
testcases
C -
2824498 个字符(包括所有内循环变量和表声明)
C -
2824498 chars(including all inner-loop var and table declarations)
Perl function 101 char
找到这个很有趣。两个技巧。它对磁带使用哈希值,你知道吗?哈希是可自动扩展的,因此无需再关心磁带边界。另一个技巧是将所访问的单元格的读取和写入结合起来。只需更改内部约定 0,空格表示 0,任何其他值表示 1。这两个技巧意味着对输出进行一些简单的解码,但我相信这是可以的。我也没有计算我的函数中的最后一个分号,因为 gnibbler 没有计算他的高尔夫球脚本中的分号。
如果有人感兴趣我也可以发布我的其他尝试。它们有点长,但使用了有趣的技巧。例如,一个是基于正则表达式的,直接使用磁带作为字符串,另一个是一种 bit-fu。
Perl function 112 char
我只计算了该函数,它按照指定的顺序接受一个字符串、一个状态编号和一个位置。该函数以数组形式返回新的磁带状态。
另一个变体106个字符
目前还不清楚这个变体是否作弊。它给出正确的结果并自动延长磁带(无固定限制),但为了避免测试是否有必要延长磁带,它会在每一步中执行此操作并调整索引。
另一个变体 98 字符
这个也在合并中,但方式不同。它只是使用全局变量在函数内部传递参数。因此,您可以在函数外部而不是内部设置变量。因此从函数体中删除了 14 个字符。
Perl function 101 char
This one was fun to find. Two tricks. It use a hash for the tape, and know what ? A hash is auto-extensible, so no need any more to care about tape boundaries. The other trick is for combining both read and write of the cell accessed. Just had to change internal conventions 0 and space means 0 and any other value means 1. These two tricks implies some trivial decoding of output, but I believe it's ok. I also not counted the final semi-colon in my function as gnibbler didn't counted his in his golfscript.
If someone is interested I can also post my other tries. They are a bit longer but uses fun tricks. One is regex based for instance and works directly with tape as string another one is a kind of bit-fu.
Perl function 112 char
I counted the function only and it takes a string, a state num and a position in that order as specified. The function returns new tape state as an array.
Another variant 106 char
It is not clear if this one is cheating or not. It gives correct results and automatically extends tape (no fixed limit), but to avoid testing if it is necessary or not to extend tape it does so every step and adjust index.
Another variant 98 char
This one is also on the merge but in a different way. It just use globals to pass parameters inside the function. Hence you set your variables outside the function instead of inside. Thus removing 14 characters from the function body.
C# - 157 个字符
该方法采用
List
作为磁带,因此只要内存允许,就可以对其进行扩展。断言:
如果我们作弊并从一开始就分配一个足够大的数组,107 个字符:
C# - 157 characters
The method takes a
List<int>
as tape, so it can be expanded as long as memory allows it.Assertion:
If we cheat and allocate a large enough array from start, 107 characters:
Perl 142 个字符(不包括在命令行和最终打印上读取参数。好吧,大部分代码是 Beaver 程序,引擎本身只有 46 个字符。
我更改了输入格式以将状态放在字符串中的位置。我不知道根本不用感到内疚,否则当 head 脱离字符串时,大多数代码将进行边界管理,即使在这个版本中,字符串边界管理也需要 17 个字符......诀窍是记住您可以将图灵机表示为马尔可夫。链...我用正则表达式做了什么。
注意:事实上,这还不是真正的高尔夫,而只是一个天真的第一次尝试。我可能会带着一些很短的东西回来。
Perl 142 char (not counting reading args on command line and final print. Well, most of code is the beaver program, the engine itself is only 46 char.
I changed input format to put the state at it's position in the string. I don't feel guilty at all as otherwise most of code was going to be border management when head was out of string. Even in this version string border management cost 17 chars... The trick is just to remember you can express turing machines as Markov chains... what I did with regexes.
Note: as a matter of fact this is not really golfed yet but just a naive first attempt. I may come back with something real short.
Golfscript - 102 个字符
106 个字符
113 个字符
读取
整个程序从 stdin示例
Golfscript - 102 Characters
106 Characters
113 Characters
Whole program reading from stdin
examples
澄清一下,这个程序完全按照维基百科文章中的描述模拟 Busy Beaver 图灵机,而不是 OP(OP 已切换 R 和 L)
Python 255 char
To clarify, this program simulates the Busy Beaver Turing Machine exactly as described in the wikipedia article, not the OP (the OP has R and L switched)
Python 255 char
Perl,97(确实是 96,因为最后的“;”对于子块是可选的)
想法:
$_ 变量包含 0 和 1,除了头部下方。
头下,
A 状态下的 0 给出 2,
A 状态下的 1 给出 3,
B 状态下的 0 给出 4,
B 状态下的 1 给出 5,
C 状态下的 0 给出 6,
C 状态下的 1 给出 7。
因此,按照第一个示例,“1010”(位置 3,状态 A)给出“1051”,然后“1411”、“1131”、“1117”(1111,状态 C,位置 3)并停止(加上将磁带移至右侧)
Perl, 97 (96 indeed, because final ";" is optional for sub block)
The idea :
$_ variable contains 0s and 1s except under the head.
Under the head,
0 in A state gives 2,
1 in A state gives 3,
0 in B state gives 4,
1 in B state gives 5,
0 in C state gives 6,
1 in C state gives 7.
so following the first example "1010" (pos 3, state A) gives "1051" then "1411", "1131", "1117" (1111, state C, pos 3) and stop (plus move tape to right)
Lua:
半高尔夫版本:
压缩版本有441个字符:
以磁带、指令指针、状态的形式传递参数,如下所示:
Lua:
Semi-golfed version:
Compacted version weighing in at 441 characters:
Pass the arguments in form of tape, instruction pointer, state, like the following:
Lua,232
现在使用表查找。
这只是 RCIX 的答案 重新打高尔夫球,332 个字符。
...
运算符分配输入参数and
和or
代替if
语句,当更短else
假设有效的输入/状态Lua, 232
Now using table lookup.
This is just RCIX's answer re-golfed, 332 characters.
...
operator to assign input paramsand
andor
instead ofif
statements when shorterelseif
with justelse
by assuming valid input/statesF# - 275 个字符
好吧,所以绝对不是最短的,但学习。如果有人可以帮助让 String.mapi 使用
函数
,而不是有趣的匹配
,我将不胜感激,我不断收到“模式鉴别器 x 未定义” '。有人知道有一个网站详细介绍了在 lambda 中使用function
关键字的规则吗?用法
这里是一个扩展版本,以提高可读性:
我还试图想出一种更好的方法来处理字符串的操作,而不是
String.mapi
。欢迎并鼓励意见和建议(请有建设性的)。F# - 275 characters
Okay, so definitely not the shortest but learning. If anyone can assist on getting the String.mapi to use a
function
rather then thefun match with
I would appreciate it, I keep getting 'The pattern discriminator x is not defined'. Anyone know of a site that details the rules of using thefunction
keyword in a lambda?Usage
Here is an expanded version for readability:
I'm also trying to think of a better way to handle the manipulation of the string, something other then the
String.mapi
. Comments and suggestions (constructive please) welcome and encouraged.Ruby,129
(删除缩进后)
Ruby, 129
(when indent removed)