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条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-03-14 16:18

Scala - 77 59 58 chars

print(1 to 9 map(p=>1 to 9 map(q=>"%02d "format(p*q))mkString)mkString("\n"))

Sorry, I had to do this, the Scala solution by Malax was way too readable...

[Edit] For comprehension seems to be the better choice:

for(p<-1 to 9;q<-{println;1 to 9})print("%02d "format p*q)

[Edit] A much longer solution, but without multiplication, and much more obfuscated:

val s=(1 to 9).toSeq
(s:\s){(p,q)=>println(q.map("%02d "format _)mkString)
q zip(s)map(t=>t._1+t._2)}
查看更多
该账号已被封号
3楼-- · 2019-03-14 16:19

C#, 135 chars, nice and clean:

var rg = Enumerable.Range(1, 9);
foreach (var rc in from r in rg 
                   from c in rg 
                   select (r * c).ToString("D2") + (c == 9 ? "\n\n" : " "))
    Console.Write(rc);
查看更多
Ridiculous、
4楼-- · 2019-03-14 16:19

XQuery 1.0 (96 bytes)

string-join(for$x in 1 to 9 return(for$y in 1 to 9 return concat(0[$x*$y<10],$x*$y,' '),'

'),'')

Run (with XQSharp) with:

xquery table.xq !method=text
查看更多
疯言疯语
5楼-- · 2019-03-14 16:19

K - 12 characters

Let's take the rosetta-stoning seriously, and compare Kdb+'s K4 with the canonical J solution (*/~1+i.9):

  a*/:\:a:1+!9
1 2  3  4  5  6  7  8  9 
2 4  6  8  10 12 14 16 18
3 6  9  12 15 18 21 24 27
4 8  12 16 20 24 28 32 36
5 10 15 20 25 30 35 40 45
6 12 18 24 30 36 42 48 54
7 14 21 28 35 42 49 56 63
8 16 24 32 40 48 56 64 72
9 18 27 36 45 54 63 72 81

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 pass a as both left and right argument.

查看更多
叼着烟拽天下
6楼-- · 2019-03-14 16:20

Brainf**k - 185 chars

>---------[++++++++++>---------[+<[-<+>>+++++++++[->+>>---------[>-<++++++++++<]<[>]>>+<<<<]>[-<+>]<---------<]<[->+<]>>>>++++[-<++++>]<[->++>+++>+++<<<]>>>[.[-]<]<]++++++++++.[-<->]<+]
查看更多
兄弟一词,经得起流年.
7楼-- · 2019-03-14 16:23

F# - 61 chars:

for y=1 to 9 do(for x=1 to 9 do printf"%02d "(x*y));printfn""

If you prefer a more applicative/LINQ-y solution, then in 72 chars:

[1..9]|>Seq.iter(fun y->[1..9]|>Seq.iter((*)y>>printf"%02d ");printfn"")
查看更多
登录 后发表回答