代码高尔夫:生成帕斯卡三角形
生成列表列表(或打印,我不介意)帕斯卡三角形大小为 N,代码行数尽可能少!
这是我的尝试(python 2.6中的118个字符,使用一个技巧 ):
c,z,k=locals,[0],'_[1]'
p=lambda n:[len(c()[k])and map(sum,zip(z+c()[k][-1],c()[k][-1]+z))or[1]for _ in range(n)]
解释:
- 列表推导式的第一个元素(当长度为 0 时)是
[1]
- 下一个元素通过以下方式获得:
- 获取前一个列表并创建两个列表,其中一个用 填充一个 0 在开头,另一个在结尾。
- 例如,对于第二步,我们采用
[1]
并制作[0,1]
和[1,0]
- 例如,对于第二步,我们采用
- 求和两个新列表逐个元素
- 例如,我们创建一个新列表
[(0,1),(1,0)]
并使用总和进行映射。
- 例如,我们创建一个新列表
- 重复n次就这样了。
用法(带有漂亮的打印,实际上超出了代码高尔夫xD):
result = p(10)
lines = [" ".join(map(str, x)) for x in result]
for i in lines:
print i.center(max(map(len, lines)))
输出:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
Generate a list of lists (or print, I don't mind) a Pascal's Triangle of size N with the least lines of code possible!
Here goes my attempt (118 characters in python 2.6 using a trick):
c,z,k=locals,[0],'_[1]'
p=lambda n:[len(c()[k])and map(sum,zip(z+c()[k][-1],c()[k][-1]+z))or[1]for _ in range(n)]
Explanation:
- the first element of the list comprehension (when the length is 0) is
[1]
- the next elements are obtained the following way:
- take the previous list and make two lists, one padded with a 0 at the beginning and the other at the end.
- e.g. for the 2nd step, we take
[1]
and make[0,1]
and[1,0]
- e.g. for the 2nd step, we take
- sum the two new lists element by element
- e.g. we make a new list
[(0,1),(1,0)]
and map with sum.
- e.g. we make a new list
- repeat n times and that's all.
usage (with pretty printing, actually out of the code-golf xD):
result = p(10)
lines = [" ".join(map(str, x)) for x in result]
for i in lines:
print i.center(max(map(len, lines)))
output:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(23)
PHP,115 个字符
如果您不关心 print_r() 是否以正确的顺序显示输出数组,您可以将其削减到 113 个字符,例如
PHP, 115 chars
If you don't care whether print_r() displays the output array in the correct order, you can shave it to 113 chars like
Perl,63 个字符:
Perl, 63 characters:
我在 C++ (378c) 中的尝试。 不如其他帖子那么好..但我为自己提出了自己的解决方案感到自豪=)
My attempt in C++ (378c). Not anywhere near as good as the rest of the posts.. but I'm proud of myself for coming up with a solution on my own =)
老帖子,但我今天写这个是为了回应另一个论坛上的挑战:
Old thread, but I wrote this in response to a challenge on another forum today:
另一个刺(蟒蛇):
another stab (python):
Haskell,164C 带格式:
不带格式,52C:
更易读的形式:
Perl,111C,不居中:
Haskell, 164C with formatting:
Without formatting, 52C:
A more readable form of it:
And perl, 111C, no centering:
方案 — 100 个字符的压缩版本
这是更易读的形式(269 个字符):
Scheme — compressed version of 100 characters
This is it in a more readable form (269 characters):
VBA/VB6(392 个字符,带格式)
VBA/VB6 (392 chars w/ formatting)
PHP 100 个字符
PHP 100 characters
红宝石,83c:
测试:
Ruby, 83c:
test:
另一个python解决方案,如果内置函数的名称更短,则可能会短得多...106个字符。
Another python solution, that could be much shorter if the builtin functions had shorter names... 106 characters.
另一个尝试,在prolog中(我正在练习xD),不太短,只是164c:
解释:
Another try, in prolog (I'm practising xD), not too short, just 164c:
explanation:
VBA,122 个字符:
VBA, 122 chars:
几年前我写了这个 C++ 版本:
I wrote this C++ version a few years ago:
以下只是一个返回
List[List[Int]]
的 Scala 函数。 没有漂亮的印刷或任何东西。 有什么建议的改进吗? (我知道这效率低下,但这不是现在的主要挑战,不是吗?)。 145 C.或者也许:(
我是 Scala 菜鸟,所以请对我好一点:D)
The following is just a Scala function returning a
List[List[Int]]
. No pretty printing or anything. Any suggested improvements? (I know it's inefficient, but that's not the main challenge now, is it?). 145 C.Or perhaps:
(I'm a Scala noob, so please be nice to me :D )
Perl 版本(139 个字符,无 shebang)
输出从 1 2 1 开始
a Perl version (139 chars w/o shebang)
output starts from 1 2 1
Haskell,58 个字符:
输出:
更易读:
Haskell, 58 characters:
Output:
More readable:
C 中的 69C:
像这样使用它:
69C in C:
Use it like so:
F#:81个字符
说明:我太懒了和 Haskell 和 K 程序员一样聪明,所以我采取了直接的路线:帕斯卡三角形中的每个元素都可以使用行 n 和列 k 唯一标识,其中每个元素的值为 n!/(k! (nk)!。
F#: 81 chars
Explanation: I'm too lazy to be as clever as the Haskell and K programmers, so I took the straight forward route: each element in Pascal's triangle can be uniquely identified using a row n and col k, where the value of each element is
n!/(k! (n-k)!
.Python:75 个字符
Python: 75 characters
较短的序言版本(112 而不是 164):
Shorter prolog version (112 instead of 164):
K (Wikipedia), 15 个字符:
示例输出:
也很容易解释:
p
是一个采用隐式参数x
。p
从1
(D) 开始展开 (C) 匿名函数 (B)x
次 (A)。匿名函数只需获取一个列表
x
,附加0
并通过添加 (+
) 每个相邻对 (0
) 来返回结果>':) 值:例如从(1 2 1)
开始,它将生成(1 2 1 0)
,添加对(1 1+2 2+1 1+0)
,给出(1 3 3 1)
。更新:适应K4,削减了另外两个角色。 作为参考,这里是原始的 K3 版本:
K (Wikipedia), 15 characters:
Example output:
It's also easily explained:
p
is a function taking an implicit parameterx
.p
unfolds (C) an anonymous function (B)x
times (A) starting at1
(D).The anonymous function simply takes a list
x
, appends0
and returns a result by adding (+
) each adjacent pair (':
) of values: so e.g. starting with(1 2 1)
, it'll produce(1 2 1 0)
, add pairs(1 1+2 2+1 1+0)
, giving(1 3 3 1)
.Update: Adapted to K4, which shaves off another two characters. For reference, here's the original K3 version:
J,APL家族中的另一种语言,9个字符:
这使用J的内置“组合”动词。
输出:
J, another language in the APL family, 9 characters:
This uses J's builtin "combinations" verb.
Output: