代码高尔夫:莫里斯数列
挑战
按字符数计算的最短代码将输出 莫里斯数字序列。 莫里斯数字序列,也称为看即说序列是一个数字序列,开头如下:
1, 11, 21, 1211, 111221, 312211, ...
您可以无限地生成序列(即,您不必生成特定的数字)。
I/O 期望
该程序不需要接受任何输入(但接受输入会带来奖励点,从而提供从任意起始点或数字开始的选项)。至少你的程序必须从1
开始。
输出至少应为以下序列:
1
11
21
1211
111221
312211
...
额外学分
如果您要获得额外学分,则需要执行以下操作:
$ morris 1
1
11
21
1211
111221
312211
...
$ morris 3
3
13
1113
3113
132113
...
The Challenge
The shortest code by character count that will output the Morris Number Sequence. The Morris Number Sequence, also known as the Look-and-say sequence is a sequence of numbers that starts as follows:
1, 11, 21, 1211, 111221, 312211, ...
You can generate the sequence infinitely (i.e, you don't have to generate a specific number).
I/O Expectations
The program doesn't need to take any input (but bonus points for accepting input and thereby providing the option to start from any arbitrary starting point or number). At the very least your program must start from 1
.
Output is at the very least expected to be the sequence:
1
11
21
1211
111221
312211
...
Extra Credit
If you're going for extra credit, you would need to do something like this:
$ morris 1
1
11
21
1211
111221
312211
...
$ morris 3
3
13
1113
3113
132113
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
GolfScript - 41(额外学分:40)
什么?
获取序列中下一个数字的过程:将当前数字转换为字符串,附加换行符并循环遍历字符。对于每个数字,如果前一个数字
P
相同,则递增计数器c
。否则,将c
和P
添加到下一个数字中,然后更新这些变量。我们附加的换行符允许将最后一组数字添加到下一个数字。确切的细节可以通过查看 GolfScript 文档来获得。 (请注意,
|
用作变量。)GolfScript - 41 (extra credit: 40)
What?
The procedure for getting the next number in the sequence: Convert the current number to a string, append a newline and loop over the characters. For each digit, if the previous digit
P
is the same, increment the counterc
. Otherwise, addc
andP
to what will be next number, then update these variables. The newline we append allows the last group of digits to be added to the next number.The exact details can be obtained examining the GolfScript documentation. (Note that
|
is used as a variable.)Haskell:
1158885这是无限序列。我知道它可以改进很多 - 我对 Haskell 还很陌生。
短一点,内联mapM并迭代:
Haskell:
1158885This is the infinite sequence. I know it can be improved a lot - I'm fairly new to Haskell.
Bit shorter, inlining mapM and iterate:
Perl(46 个字符)
额外学分(52 个字符)
Perl (46 characters)
Extra Credit (52 characters)
Javascript
10097允许中断序列(通过单击“取消”),这样我们就不会锁定用户代理并固定 CPU。它还允许从任何正整数开始(额外学分)。
实例:http://jsbin.com/izeqo/2
Javascript
10097Allows interrupting the sequence (by clicking "Cancel") so we don't lock the user-agent and peg the CPU. It also allows starting from any positive integer (extra credit).
Live Example: http://jsbin.com/izeqo/2
Perl,46 个字符
额外学分,51 个字符:
Perl, 46 characters
Extra credit, 51 characters:
Mathematica -
625350 个字符 - 包含额外学分编辑:40个字符...但从右到左:(
奇怪的是,如果我们从右到左读取序列(即1,11,12,1121,..),40个字符就足够了
这是因为Tally 生成一个列表 {elem,counter} !
编辑:50 个字符
剖析:(向上阅读注释)
编辑:重构
结束编辑 ///
不需要/为了清晰起见添加了空格
我第一次使用“Riffle”进行调用
,将 {1,2,3} 和 {a,b,c} 组合成 {1,a,2,b,3,c} :)
Mathematica -
625350 chars - Extra credit includedEdit: 40 chars ... but right to left :(
Curiously if we read the sequence right to left (i.e. 1,11,12,1121, ..), 40 chars is enough
That is because Tally generates a list {elem,counter} !
Edit: 50 chars
Dissection: (read comments upwards)
Edit: refactored
End edit ///
Spaces not needed / added for clarity
Invoke with
My first time using "Riffle", to combine {1,2,3} and {a,b,c} into {1,a,2,b,3,c} :)
以下是我使用 LINQ 的 C# 尝试以及 Code Golf 的首次尝试:
C# -
205194211198 字节,含额外积分(包括 C# 样板)<代码>使用 System.Linq;class C{static void Main(string[]a){var v=a[0];for(;;){var r="";while(v!=""){int i=v.TakeWhile(d=>d==v[0]).Count();r+=i;r+=v[0];v=v.Remove(0,i);}System.Console. WriteLine(r);v=r;}}}
可读版本:
示例输出:
Here's my C# attempt using LINQ and first attempt at Code Golf:
C# -
205194211198 bytes with extra credit (including C# boilerplate)using System.Linq;class C{static void Main(string[]a){var v=a[0];for(;;){var r="";while(v!=""){int i=v.TakeWhile(d=>d==v[0]).Count();r+=i;r+=v[0];v=v.Remove(0,i);}System.Console.WriteLine(r);v=r;}}}
Readable version:
Sample output:
Python, 97
102115空白应该是制表符:
例如:
Python, 97
102115Whitespace is supposed to be tabs:
E.g.:
Perl,67 个字符
,包括
-l
标志。Perl,72 个字符,有额外学分
Perl, 67 characters
including
-l
flag.Perl, 72 characters with extra credit
这是我的实现(在 Prolog 中):
Prolog with DCG(174 个字符):
普通的 prolog,代码更具可读性(225 个字符):
输出 Morris 序列:
米(D)。其中 D 是“起始”数字。
Here goes my implementation (in Prolog):
Prolog with DCGs (174 chars):
Plain vanilla prolog, code much more readeable (225 chars):
To output the Morris sequence:
m(D). where D is the 'starting' digit.
Ruby — 52
任务太简单了,也太没用了……
Ruby — 52
Task is too simple, and too perlish...
C、128个字符
使用静态缓冲区,保证会导致分段错误
C, 128 characters
uses a static buffer, guaranteed to cause segmentation fault
如果一个字符串只包含数字 1-3,并且不包含任何超过三个数字的串,则称该字符串为“Morris-ish”。如果初始字符串是 Morris 式的,则从它迭代生成的所有字符串也将同样是 Morris 式的。同样,如果初始字符串不是 Morris-ish,则生成的字符串将不会是 Morris-ish,除非大于 10 的数字被视为数字组合(例如,如果 11111111111 被视为折叠成三个,而不是“11”,并且一个)。
鉴于这一观察,以 Morris 式种子开始的每次迭代都可以按照以下查找/替换操作序列来完成:
请注意,在上述替换之前,序列是 Morris 式的,每个生成对的第二个字符将不同于前后对的第二个字符;因此,序列中不可能有超过三个相同的字符。
我想知道有多少不相交的莫里斯式序列?
Call a string "Morris-ish" if it contains nothing but digits 1-3, and does not contain any runs of more than three of a digit. If the initial string is Morris-ish, all strings iteratively generated from it will likewise be Morris-ish. Likewise, if the initial string is not Morris-ish then no generated string will be Morris-ish unless numbers greater than ten are regarded as combinations of digits (e.g. if 11111111111 is regarded as collapsing into three ones, rather than an "eleven" and a one).
Given that observation, every iteration starting with a Morris-ish seed can be done as the following sequence of find/replace operations:
Note that a sequence is Morris-ish before the above substitution, the second character of each generated pair will be different from the second character of the preceding and following pairs; it is thus not possible to have more than three identical characters in sequence.
I wonder how many disjoint Morris-ish sequences there are?
Java
我第一次尝试“代码高尔夫”,我只是在 IB CS 课程的一部分中将其组合在一起:
238 压缩
压缩:
正确格式化:
Java
My first attempt at a 'Code-Golf' I just threw this together during part of my IB CS class:
238 condensed
Condensed:
Properly formatted:
Perl(额外学分),47 个字符
Perl (extra credit), 47 chars
J,44 个字符,带额外积分
(([:,#;.1@{:,.{:#{.)@(,:0<1,[:|2-/\]))^: (<9)
不幸的是,这仅生成 9 次迭代,但迭代计数
<9
可以调整为任何值。将其设置为a:
会生成无限序列,但显然这无法打印。用法:
J, 44 characters with extra credit
(([:,#;.1@{:,.{:#{.)@(,:0<1,[:|2-/\]))^:(<9)
Unfortunately this only generates 9 iterations, but the iteration count
<9
can be tweaked to be anything. Setting it toa:
generates an infinite sequence but obviously this can't be printed.Usage:
Delphi
Delphi 是一种糟糕的高尔夫语言,但仍然:
这是211 字节(并且按原样编译)。
Delphi
Delphi is a terrible golfing language, but still:
This is 211 bytes (and it compiles as it stands).
PHP:111
我第一次尝试代码高尔夫,对结果非常满意。
给予:
PHP 额外学分:118
给予:
PHP: 111
My first attempt ever on a code golf, quite happy with the result.
Gives:
PHP with extra credit: 118
Gives:
Java - 167 个字符(含信用)
(122 个无类/主样板)
带换行符:
Java - 167 chars (with credit)
(122 without class/main boilerplate)
With newlines:
这是我第一次尝试编程高尔夫,所以请不要对我太严厉!
PHP,
128122112 字节,带开始标记122116106 字节 不带开始标记标签和前导空格。(很遗憾,我必须将
$a
初始化为字符串,这花费了我 2 个额外字节,否则我无法在其上使用索引符号。)输出
PHP (额外学分),
133127117 字节,带开始标记127121111 字节无需打开标记和前导空格。
输出
PHP(额外学分),不带开始和结束标签
Here's my very first attempt at code golf, so please don't be too hard on me!
PHP,
128122112 bytes with opening tag122116106 bytes without opening tag and leading whitespace.(Quite a pity I have to initialize
$a
as a string though, costing me 2 extra bytes, otherwise I can't use index notation on it.)Output
PHP (extra credit),
133127117 bytes with opening tag127121111 bytes without opening<?php
tag and leading whitespace.Output
PHP (extra credit), ungolfed with opening and closing tags
C++,310 个字符。
正确缩进:
C++, 310 characters.
Correctly indented:
Python - 117
我的 python-fu 并不强,所以我为此做了很多谷歌搜索。 :)
这个想法是使用 zip 生成 (a[i],a[i+1]) 对的列表,当 a[i]!=a[i+1] 时使用内部推导式插入空格,将结果列表连接到字符串,并按空格分割,在此分割列表上使用另一种理解方式,将每个元素替换为元素的游程长度和第一个字符,最后连接以按顺序获取下一个值。
Python - 117
My python-fu is not strong, so I did a lot of googling for this. :)
The idea is to use zip to generate a list of (a[i],a[i+1]) pairs, use the inner comprehension to insert a space when a[i]!=a[i+1], join the resulting list to a string, and split on spaces, use another comprehension on this split list to replace each element with the run length of the element, and the first character, and finally join to get the next value in sequence.
C w/ Extra Credit,242(或 184)
如果省略包含,您还可以保存大约 60 个字符,gcc 仍会编译并带有警告。
C w/ Extra Credit, 242 (or 184)
You can save another ~60 characters if you omit the includes, gcc will still compile with warnings.
Python - 98 个字符
奖励 106 个字符
Python - 98 chars
106 chars for the bonus
C#,204 字节(额外学分为 256)
我第一次尝试 code Golf
可读版本,与其他人的不同之处在于我使用了 Linq 的 Aggregate 函数
C#, 204 bytes (256 with extra credit)
My first attempt at code golf
Readable version, the difference from others is that I use Linq's Aggregate function
Common Lisp -
124 122115 个字符格式:
Common Lisp -
124 122115 CharsWith formatting:
F# - 135
格式化代码
仍然希望我能找到更好的方法来打印列表或使用 string/bigint 代替。
F# - 135
Formatted Code
Still hopeful I can find a better way to print the list or use string/bigint instead.
PHP 72 字节
该脚本可能需要进一步优化。但由于我们在 PHPGolf ({http://www.phpgolf.org/?p=challenges&challenge_id=28}) 中得到了完全相同的序列,所以我保持这种方式。
PHP 72 bytes
This script might be further optmized. But since we've got at PHPGolf ({http://www.phpgolf.org/?p=challenges&challenge_id=28}) exactly the same sequence, I keep it this way.
Python - 92 个字符
98 个额外学分
无限输出。我建议将输出重定向到文件,然后快速按 Ctrl+C。
对于额外积分版本,替换
为
Python - 92 characters
98 with extra credit
Outputs infinitely. I recommend redirecting output to a file, and quickly hitting Ctrl+C.
For the extra credit version, replace
with
C - 120 个字符
129 个额外信用
换行符只是为了可读性而存在。
当出现段错误时(至少 15 次迭代后),它会停止。如果您的 C 库使用缓冲 I/O,那么您可能在段错误之前看不到任何输出。如果是这样,请使用以下代码进行测试:
这会在每个输出后添加一个
fflush
。如果没有打高尔夫球,它看起来像这样:
您可以在 ideone 上看到它的运行情况。
加上额外的积分,代码如下所示:
C - 120 characters
129 with extra credit
The newline is there only for readability's sake.
This stops when it segfaults (after at least 15 iterations). If your C libraries use buffered I/O, then you may not see any output before the segfault. If so, test with this code:
This adds an
fflush
after every output.Ungolfed, it would look something like this:
You can see it in action on ideone.
With the extra credit, the code looks like this: