Code-golf:将乘法表输出到控制台
我最近向一位从事工作经验的学生推荐了一篇有关将乘法表转储到控制台的文章。它使用嵌套的 for 循环并将每个循环的步长值相乘。
这看起来像是一种 .NET 2.0 方法。我想知道,使用 Linq 和扩展方法,例如,需要多少行代码才能达到相同的结果。
stackoverflow 社区能够应对挑战吗?
挑战: 在控制台应用程序中,编写代码来生成如下例所示的表格:
01 02 03 04 05 06 07 08 09 02 04 06 08 10 12 14 16 18 03 06 09 12 15 18 21 24 27 04 08 12 16 20 24 28 32 36 05 10 15 20 25 30 35 40 45 06 12 18 24 30 36 42 48 54 07 14 21 28 35 42 49 56 63 08 16 24 32 40 48 56 64 72 09 18 27 36 45 54 63 72 81
由于这变成了一场与语言无关的代码高尔夫之战,我将与社区一起决定哪个是可接受答案的最佳解决方案。
关于表格的规范和格式已经有很多讨论,我故意添加了 00 格式,但双换行符最初只是在那里,因为我不知道在创建帖子时如何格式化文本!
I recently pointed a student doing work experience to an article about dumping a multiplication table to the console. It used a nested for loop and multiplied the step value of each.
This looked like a .NET 2.0 approach. I was wondering, with the use of Linq and extension methods,for example, how many lines of code it would take to achieve the same result.
Is the stackoverflow community up to the challenge?
The challenge:
In a console application, write code to generate a table like this example:
01 02 03 04 05 06 07 08 09 02 04 06 08 10 12 14 16 18 03 06 09 12 15 18 21 24 27 04 08 12 16 20 24 28 32 36 05 10 15 20 25 30 35 40 45 06 12 18 24 30 36 42 48 54 07 14 21 28 35 42 49 56 63 08 16 24 32 40 48 56 64 72 09 18 27 36 45 54 63 72 81
As this turned into a language-agnostic code-golf battle, I'll go with the communities decision about which is the best solution for the accepted answer.
There's been alot of talk about the spec and the format that the table should be in, I purposefully added the 00 format but the double new-line was originally only there because I didn't know how to format the text when creating the post!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
J - 8 个字符 - 24 个字符的正确格式
给出:
@earl 找到的此解决方案:
给出:
J - 8 chars - 24 chars for proper format
Gives:
This solution found by @earl:
Gives:
MATLAB - 10 个字符
...或 33 个字符用于更严格的输出格式
MATLAB - 10 characters
... or 33 characters for stricter output format
Brainf**k - 185 个字符
Brainf**k - 185 chars
cat - 252 个字符
假设需要尾随换行符;否则为 251 个字符。
* 运行 *
cat - 252 characters
Assuming that a trailing newline is wanted; otherwise, 251 chars.
* runs *
Python - 61 个字符
Python - 61 chars
C#
这只有 2 行。它使用 lambdas 而不是扩展方法
,当然它可以在一个长的、不可读的行中完成,
所有这些都假设您考虑 Labmda 一行?
C#
This is only 2 lines. It uses lambdas not extension methods
and of course it could be done in one long unreadable line
all of this is assuming you consider a labmda one line?
K - 12 个字符
让我们认真对待罗塞塔石碑分析,并将 Kdb+ 的 K4 与规范的 J 解决方案 (
*/~1+i.9
) 进行比较:J 的“table”运算符 (
/< /code>) 等于 K “each-left every-right” (
/:\:
) 习语。 K 中没有 J 极其方便的“自反”运算符 (~
),因此我们必须将a
作为左右参数传递。K - 12 characters
Let's take the rosetta-stoning seriously, and compare Kdb+'s K4 with the canonical J solution (
*/~1+i.9
):J's "table" operator (
/
) equals the K "each-left each-right" (/:\:
) idiom. We don't have J's extremely handy "reflexive" operator (~
) in K, so we have to passa
as both left and right argument.Fortran95 - 40 个字符(比 perl 多 4 个字符!)
此解决方案确实按照规范打印前导零。
Fortran95 - 40 chars (beating perl by 4 chars!)
This solution does print the leading zeros as per the spec.
Oracle SQL,103 个字符:
Oracle SQL, 103 characters:
C# -
117, 113, 99, 96, 9589 个字符基于 NickLarsen 的想法
99, 85, 8281 个字符...如果您不关心前导零并且允许使用制表符进行对齐。
C# -
117, 113, 99, 96, 9589 charactersupdated based on NickLarsen's idea
99, 85, 8281 characters... If you don't care about the leading zeros and would allow tabs for alignment.
COBOL - 218 个字符 -> 216 个字符
编辑
216 个字符(可能是不同的编译器)
COBOL - 218 chars -> 216 chars
Edit
216 chars (probably a different compiler)
不是真正的单行,而是我能想到的最短的链接:
响应结合此和 SRuly 的答案
Enumberable.Range(1,9).ToList.ForEach(n => Enumberable.Range(1, 9).ToList.ForEach(n2 => Console.Write((n * n2).ToString("00 "))); Console.WriteLine();
Not really a one-liner, but the shortest linq i can think of:
In response to combining this and SRuly's answer
Enumberable.Range(1,9).ToList.ForEach(n => Enumberable.Range(1,9).ToList.ForEach(n2 => Console.Write((n * n2).ToString("00 "))); Console.WriteLine(); });
Ruby - 42 个字符(包括一个换行符,仅限交互式命令行)
此方法是两行输入,仅适用于 irb(因为 irb 为我们提供了 _ ),但会缩短与之前的方法相比,只增加了不到 2 个字符。
Ruby - 44 个字符(与 perl 并列)
Ruby - 46 个字符
Ruby - 47 个字符
回到双循环
Ruby - 54 个字符!
使用一个循环可以节省几个字符!
Ruby - 56 个字符
Ruby - 42 Chars (including one linebreak, interactive command line only)
This method is two lines of input and only works in
irb
(because irb gives us_
), but shortens the previous method by a scant 2 charcters.Ruby - 44 Chars (tied with perl)
Ruby - 46 Chars
Ruby - 47 Chars
And back to a double loop
Ruby - 54 chars!
Using a single loop saves a couple of chars!
Ruby - 56 chars
Haskell —
858479 个字符如果需要双倍间距(
8981 个字符),Haskell —
858479 charsIf double spacing is required (
8981 chars),F# - 61 个字符:
如果您更喜欢更适用的/LINQ-y 解决方案,则使用 72 个字符:
F# - 61 chars:
If you prefer a more applicative/LINQ-y solution, then in 72 chars:
c# -
125,123 个字符(2 行):c# -
125, 123 chars (2 lines):C -
9779 个字符C -
9779 charactersPerl,44 个字符
(没有希望接近 J,但具有矩阵运算的语言在这里属于自己的一类......)
Perl, 44 chars
(No hope of coming anywhere near J, but languages with matrix ops are in a class of their own here...)
R(在这个级别上与 Matlab 非常相似):12 个字符。
R (very similar to Matlab on this level): 12 characters.
PHP,71 个字符
输出:
PHP, 71 chars
Output:
C#,135 个字符,干净整洁:
C#, 135 chars, nice and clean:
PostgreSQL:
8174 个字符PostgreSQL:
8174 charsRuby - 56 个字符 :D
Ruby - 56 chars :D
C - 66 个字符
这解决了对 main 第二个参数的抱怨:)
C - 77 个字符
基于 dreamlax 的 97 个字符答案。他当前的答案有点类似于现在的这个:)
用 gcc 编译可以,我认为 main(x,y) 对于高尔夫来说是公平的游戏
C - 66 Chars
This resolves the complaint about the second parameter of main :)
C - 77 chars
Based on dreamlax's 97 char answer. His current answer somewhat resembles this one now :)
Compiles ok with gcc, and
main(x,y)
is fair game for golf i reckonXQuery 1.0(96 字节)
运行(使用 XQSharp):
XQuery 1.0 (96 bytes)
Run (with XQSharp) with:
Scala -
775958 个字符抱歉,我必须这样做,Malax 的 Scala 解决方案太可读了...
[编辑] 对于理解来说似乎更好选择:
[编辑]一个更长的解决方案,但没有乘法,而且更加混乱:
Scala -
775958 charsSorry, I had to do this, the Scala solution by Malax was way too readable...
[Edit] For comprehension seems to be the better choice:
[Edit] A much longer solution, but without multiplication, and much more obfuscated:
PHP,62 个字符
PHP, 62 chars
Java -
155137 个字符更易读的格式:
Java -
155137 charsMore readable format:
将 C#/Linq 与 GroupJoin 结合使用的另一次尝试:
Another attempt using C#/Linq with GroupJoin:
Ruby — 47 个字符
输出
(如果我们忽略间距,则变为 39:
puts (a=1 ..9).map{|i|a.map{|j|j*i}*" "}
不管怎样,我觉得冗长的map< 还有一些改进的空间/code> 东西。)
Ruby — 47 chars
Output
(If we ignore spacing, it becomes 39:
puts (a=1..9).map{|i|a.map{|j|j*i}*" "}
And anyway, I feel like there's a bit of room for improvement with the wordymap
stuff.)