Code-golf: Output multiplication table to the Cons

2019-03-14 15:35发布

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!

30条回答
你好瞎i
2楼-- · 2019-03-14 16:28

PHP, 62 chars

for(;$x++<9;print"\n",$y=0)while($y++<9)printf("%02d ",$x*$y);
查看更多
混吃等死
3楼-- · 2019-03-14 16:30

Fortran95 - 40 chars (beating perl by 4 chars!)

This solution does print the leading zeros as per the spec.

print"(9(i3.2))",((i*j,i=1,9),j=1,9);end
查看更多
走好不送
4楼-- · 2019-03-14 16:33

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.

1..9
_.map{|y|puts"%02d "*9%_.map{|x|x*y}}

Ruby - 44 Chars (tied with perl)

(a=1..9).map{|y|puts"%02d "*9%a.map{|x|x*y}}

Ruby - 46 Chars

9.times{|y|puts"%02d "*9%(1..9).map{|x|x*y+x}}

Ruby - 47 Chars

And back to a double loop

(1..9).map{|y|puts"%02d "*9%(1..9).map{|x|x*y}}

Ruby - 54 chars!

Using a single loop saves a couple of chars!

(9..89).map{|n|print"%02d "%(n/9*(x=n%9+1))+"\n"*(x/9)}

Ruby - 56 chars

9.times{|x|puts (1..9).map{|y|"%.2d"%(y+x*y)}.join(" ")}
查看更多
迷人小祖宗
5楼-- · 2019-03-14 16:33

Another attempt using C#/Linq with GroupJoin:

Console.Write(
    String.Join(
        Environment.NewLine,
        Enumerable.Range(1, 9)
            .GroupJoin(Enumerable.Range(1, 9), y => 0, x => 0, (y, xx) => String.Join(" ", xx.Select(x => x * y)))
            .ToArray()));
查看更多
SAY GOODBYE
6楼-- · 2019-03-14 16:34

cat - 252 characters

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

Assuming that a trailing newline is wanted; otherwise, 251 chars.

* runs *

查看更多
我只想做你的唯一
7楼-- · 2019-03-14 16:36

PHP, 71 chars

for($x=0;++$x<10;print"\n"){for($y=0;++$y<10;){printf("%02d ",$x*$y);}}

Output:

$ php -r 'for($x=0;++$x<10;print"\n"){for($y=0;++$y<10;){printf("%02d ",$x*$y);}}'
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 
查看更多
登录 后发表回答