用最少的代码字符创建、排序和打印 100 个随机整数的列表
您可以编写最少的代码来创建、排序(升序)和打印 100 个随机正整数的列表? 我所说的最少代码量是指整个源文件中包含的字符,因此开始缩小。
我有兴趣使用任何和所有编程语言查看答案。 让我们尝试为每种语言保留一个答案,编辑前一个以更正或简化。 如果无法编辑,可以评论吗?
What is the least amount of code you can write to create, sort (ascending), and print a list of 100 random positive integers? By least amount of code I mean characters contained in the entire source file, so get to minifying.
I'm interested in seeing the answers using any and all programming languages. Let's try to keep one answer per language, edit the previous to correct or simplify. If you can't edit, comment?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
J: 中的 10 个字符
解释:
/:~
对数组进行排序(从技术上讲,将列表排序的排列向量应用于自身)x ? limit
返回 x 个小于 limit9e9
(9000000000) 的随机数,这是一个可以用 3 个字符表示的合理上限。 !9(9 阶乘)较小,但需要少一个字符。10 characters in J:
explanation:
/:~
sorts an array (technically, applies a lists sorted permutation vector to itself)x ? limit
returns x random numbers less than limit9e9
(9000000000) is a reasonable upper limit expressible in 3 characters. !9 (9 factorial) is smaller, but requires one less character.PHP 中的 xkcd 样式:
xkcd style in PHP:
Linux,命令行:
Linux, command line:
我的条目:
或者,根据亚当在评论中的说法:
My entry:
or, per Adam in the comments:
C#
编辑:制作完整的程序。 假设可以删除换行符和空格,但为了清晰起见保留了:)
编辑:变得更短......我敢于有人改进这个......我已经尝试了一个小时。
编辑:我认为这有点短。
编辑:我认为这更短。 呃,让我停下来。
编辑:多一行,少一个字符。 有争议的...
Explanation
A[100]
- 任何旧事物的数组 - 在本例中为 A(这是一个很好的短名称)。 内容完全被忽略,重要的是数组的大小。.Select(i=>r.Next())
- 生成 r.Next() 的 100 个可枚举值。.OrderBy(i=>i)
- 按顺序对前一个进行排序。.ToList()
- 将排序后的 int 枚举转换为 List,这样我们就可以使用 ForEach。ForEach(Console.WriteLine)
- 调用 Console.WriteLine 100 次,传入列表中的每个整数值。C#
EDIT: made complete program. assumes newlines and spaces could be removed, but left in for clarity :)
EDIT: made even shorter.... I dare someone to improve this one... I've tried for an hour.
EDIT: I think that's a bit shorter.
EDIT: I think that's even more shorter. Ugh, make me stop.
EDIT: One more line, one less character. Debatable...
Explanation
A[100]
- an array of any old thing - in this case A's (it's a nice short name). The contents are completely ignored, it's the size of the array that counts..Select(i=>r.Next())
- generates an enumerable of 100 values of r.Next()..OrderBy(i=>i)
- sorts the previous in order..ToList()
- convert the sorted enumerable of int to a List, so we can use ForEach.ForEach(Console.WriteLine)
- call Console.WriteLine 100 times, passing in each integer value in the list.Mathematica,28 个字符
给出 {0,...,2^32} 中的 100 个(已排序)随机整数。
Mathematica, 28 chars
That gives 100 (sorted) random integers in {0,...,2^32}.
Common Lisp,0 到 10000 之间的 int(没有上限,但你必须选择一个)。
Common Lisp, int between 0 and 10000 (there is no upper bound for that, but you have to choose one).
APL
13 个字符:
APL
13 chars:
F#
F#
ruby 中的尝试:(
少了 8 个字符,但需要 Ruby 1.9 的
tap
kestrel)- 对于 ruby 1.8:
30 个字符。 (可以通过改回 9e9 来削减 2,但有问题的评论说范围应该是 MaxInt32。
An attempt in ruby:
(With eight fewer characters, but requiring the
tap
kestrel of Ruby 1.9)-for ruby 1.8:
30 characters. (could trim by 2 by changing back to 9e9, but comment in question says range should be MaxInt32.
哈斯克尔:
Haskell:
在 BASH 中:
In BASH:
Javascript:(通过 JSDB 或 Mozilla 的 Rhino 在 shell 模式下使用)
这是一个完整的测试运行:
编辑:看起来我可以通过直接赋值而不是“推送”来缩短它几个字符,而且我不这样做不需要 {}:
Javascript: (via JSDB or Mozilla's Rhino used in shell mode)
Here's a full test run:
edit: looks like I can shorten it a few chars by direct assignment rather than "push", and I don't need the {}s:
Python打印100个随机的、排序的整数
@Adam 已经打败了我,但我认为使用 randint() 和 sys.maxint 有足够的不同来发布。
Python to print 100 random, sorted integers
@Adam already beat me to it, but I thought using randint() and sys.maxint was sufficiently different to post anyway.
APL(交互式):
如果您想要数字 0-99(或 1-100,具体取决于无论您将工作区中的索引原点设置为 0 还是 1),要唯一,它需要 8 个字符,如下所示:
↑100?100
如果您不关心唯一性,请执行以下操作(9 个字符):
↑?100ρ100
想要更大的数字吗? 只需用您的上限 N 替换每行的第二个 100,您的随机数将在 0 - N-1 范围内(如果索引原点设置为 1,则为 1-N)。
如果您想保证数字范围为 0-99(如果您想要更大的上限,则为 0 - N-1),无论索引原点设置如何,只需将上述任一行括在括号中并添加
< code>-⎕IO
到末尾(其中 ⎕ 是 APL 的四字符)。 这是额外的 6 个字符。
APL (interactive):
If you want the numbers 0-99 (or 1-100, depending on whether you have the index origin in your workspace set to 0 or 1) to be unique, it takes 8 characters, like so:
↑100?100
If you don't care about uniqueness, do this (9 characters):
↑?100ρ100
Want larger numbers? Just substitute your upper limit, N, for the second 100 on each line, and your random numbers will be in the range 0 - N-1 (or 1-N if your index origin is set to 1).
If you want to guarantee that your numbers range from 0-99 (or 0 - N-1 if you're going for a larger upper limit) regardless of the index origin setting, just enclose either of the above lines in parentheses and add
-⎕IO
to the end (where ⎕ is APL's quad character). That's an additional 6 characters.
Powershell :
35 个字符(使用 PowerShell 社区扩展,取代 Get-Random):
20 个字符(纯 PowerShell v2):
Powershell :
35 chars (with PowerShell Community Extensions, which replaces
Get-Random
):20 characters (plain PowerShell v2):
Perl,比 nrich 的版本短了整整 8 个字节,并且在“use warnings”下运行; :)
Perl, a full 8 bytes shorter than nrich's version, and runs under "use warnings;" :)
爪哇:
Java:
绝妙:
groovy:
克洛尤尔
Clojure
在 OCaml 中:
编辑:在 OCaml 中,在顶层键入将打印出列表,但如果您希望将列表打印到标准输出:
In OCaml:
Edit: in OCaml typing that in the toplevel will print out the list, but if you want the list printed to stdout:
Windows BATCH:160。这会在数字中添加一个前导零,但否则排序会有点混乱(因为排序按字符排序 - 它对数字一无所知)。
作为一句简短的话(72):
Windows BATCH: 160. This adds a leading zero's to the numbers, but otherwise the sorting is a little messed up (because sort sorts by characters - it doesn't know anything about numbers).
As a one-liner and way shorter (72):
C++ 不是适合这项工作的工具,但这里是:
C++ is not the right tool for this job, but here goes:
mackenir:改进了 7 个字符:
mackenir: an improvement by 7 characters:
带升压的 C++。 太糟糕了,#include 已经占了所有文本的一半:)
C++ with boost. Too bad that #include's are already half of all the text :)
C#
如果您同意对数组大小施加限制,那么:
否则,可以采取限制较少(但稍微冗长)的角度:
好的,我想这是我最后一次回到这个话题。 ..
116 个字符:
C#
If you're okay with imposing a limit on the array size then:
Otherwise, a less restrictive (but slightly more verbose) angle could be taken:
Okay, I think this is the last time I'm coming back to this one...
116 chars:
167 个字符的普通旧 C 代码:
plain old c-code in 167 chars:
Java,再次
我认为它不能比这更短..
我还删掉了不必要的空间。
LE:哦,是的,可以:)受到丁的帖子的启发..
Java, again
i don't think it can be made shorter than this..
i also cut out unnecessary spaces.
LE: oh yes it can :) inspired by ding's post..
他说的是最少的字符,而不是最少的字节。 =)
He said the least chars, not the least bytes. =)
Tcl已经死了。
tcl万岁。
创建一个 RANDOM (0-99) 长度列表并将 RANDOM (0-99) 整数放入其中。
还打印到屏幕上,并且可以完全按照 tcl 文件或 tcl shell 中所示的方式运行。
PHP 也不错。
完全确认可以锻炼
Tcl is dead.
Long live tcl.
Creates a RANDOM (0-99) length list and puts RANDOM (0-99) integers in it.
Also prints to the screen and can be run exactly as shown in a tcl file, or the tcl shell.
PHP is nice too.
confirms completely to exercise