给定图像,输出[整个垂直截面的像素宽度] 1(如果存在)。如果不存在垂直截面,则输出0
。
输入可以作为本地文件或嵌套数组提供。如果选择将输入作为嵌套数组,则白色像素应由真实值表示,而非白色像素应由伪值表示。
1.连续的全白列的数量
您可以假设
没有图像会大于1000平方像素
每个图像最多只能有一个完整的垂直部分
例子
输入:
输出:
50
57
0
0
以下是前两个示例,以黄色突出显示它们的部分:
给定图像,输出[整个垂直截面的像素宽度] 1(如果存在)。如果不存在垂直截面,则输出0
。
输入可以作为本地文件或嵌套数组提供。如果选择将输入作为嵌套数组,则白色像素应由真实值表示,而非白色像素应由伪值表示。
1.连续的全白列的数量
您可以假设
没有图像会大于1000平方像素
每个图像最多只能有一个完整的垂直部分
输入:
输出:
50
57
0
0
以下是前两个示例,以黄色突出显示它们的部分:
Answers:
PS
如果我像这样编码图像:
0000111111111100000
0000000111111111000
0000000001111100000
0000000011111000000
0001111111111111100
0000001111110000000
0000000111111110000
0000111111111100000
变成这样的嵌套数组:
[[0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0],[0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,0,0,0],...]
然后P
取所有行向量的逐元素乘积,并对结果中的所有行向量S
求和,得出垂直切片的长度。(这之所以起作用,是因为保证只有一个连续的切片。)在我们的例子中,答案是3
。
ps
也可以在MATL中使用!
1
s的列,这意味着P
will 的结果是[0,0,0...0]
,而S
um是0
预期的。
Xps
但是,如果图像可以是单行,则可能需要(或询问OP是否有最小尺寸)
(+/×/⍉)
不起作用。
(+/×⌿)
,而且短了1个字节。
+/×⌿
f←+/×⌿
f picture
rs -Tc|grep -vc 0
如果您不grep
用于图像处理,那么您做错了;-)。
这使用该rs
实用程序进行转置。 OSXrs
是捆绑在OSX中的,但是需要在大多数linux中安装像linux这样的东西sudo apt-get install rs
。
输入列是TAB分开的,行是换行符的:
0 0 0 0 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0
0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0
0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0
0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0
0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0
0 0 0 0 0 0 1 1 1 1 1 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0
0 0 0 0 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0
如果愿意,可以使用imagemagick和(GNU)sed将示例输入图像预处理为这种格式。例如:
$ for img in "AmXiR.jpg" "vb2Yt.jpg" "1V7QD.jpg" "MqcDJ.jpg" ; do
> convert -depth 1 "$img" xpm:- | \
> sed -nr '/pixels/{:l;n;/}/q;s/^"(.*)",?$/\1/;y/ ./01/;s/./&\t/g;p;bl}' | \
> rs -Tc|grep -vc 0
> done
50
57
0
0
$
包含+2 for -lp
(-l
可以省略,并且仍然是有效的解决方案,但是如果没有最后的换行符,这是很丑陋的)
在STDIN的0或更多行上给出1和0的序列。如果需要,您可以在数字之间添加空格或逗号或其他任何字符,只要各行的用法是一致的即可。
$a|=~$_}{$_=$a=~y;\xce;
如图所示,该方法有效,但用\xce
文字字节值替换即可获得要求的分数
如果有多个垂直截面,则返回所有截面宽度的总和。如果你想的宽度一个垂直部分使用
$a|=~$_}{$a=~/\xce+/;$_="@+"-"@-"
我最初误解了挑战,并实施了一个程序,该程序根据是否存在垂直线给出真还是假。这里的代码和说明适用于该旧版本
$a|=~$_}{$_|=~$a=~1
如果我可以在左侧添加1 =〜以获得几乎完美的对称性...我想最接近的是
1=>$a|=~$_}{$_|=~$a=~1
$a|=~$_ The bitwise operators in perl (&, |, ^, ~) also work on strings by
working on the sequence of byte values. The digits "0" and "1" happen
to have the same ASCII value differing only in the last bit which is
0 for "0" and 1 for "1". So I would really like to do an "&" here.
Unfortunately "&" of two different length strings shortens the result
to the shortest of the strings and my accumulator starts as an empty
string. The "|" of two strings however extends to the longest string.
So instead I will apply De Morgan's law and use "|" on the
complemented byte string
}{ Standard perl golf trick. "-p code" transforms to (simplified)
"while (<>) { code; print }". So if code is "code1 } { code2" this
becomes "while (<>) { code1 } {code2; print }". So you can use code1
for the loop operation, use code2 for the final calculation and get a
free print by assigning to $_
$_|=~$a=~1 I would like to match the accumulator with the bit complement of "1",
but $a=~~1 doesn't work because the 1 is not a string but a number.
$a=~~"1" would work but is too long. Next up is complementing $a back
and matching with 1, so $_=~$a=~1. That also doesn't work since the
first =~ will be interpreted as a string match insteads of equals
followed by complement. Easily solved by writing it as $_= ~a=~1. But
if I am going to give up a byte I can at least have some fun with it.
Using $_|= also makes the parse work and has the advantage that the
failure case will give 0 instead of an empty string, which looks
nicer. It also makes the code look very symmetric. I can also bring
out the symmetry more by putting 1=> in front (which evaluates 1
before the assignment and then immediately discards it)
使用许多我喜欢的内置函数链接在一起,这是一个出人意料的优雅解决方案。
lambda c:sum(map(all,zip(*c)))
使用@Lynn的测试图像:
>>> image = [[0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0], [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0]]
>>> func = lambda c:sum(map(all,zip(*c)))
>>> func(image)
3
a=>a[s=0].map((_,i)=>s+=a.every(b=>b[i]))|s
a=>a[s=0].map((_,i)=>s+=!a.some(b=>b[i]))|s
基于@Lynn的Jelly答案,尽管由于使用every
或some
代替打高尔夫球reduce
。第一个版本将black = 0编码,而第二个版本将black = 1编码。
编辑:由于@ edc65,又节省了2个字节。
map
Length@Cases[Total@#,0]&
采用以下形式的数组:
{{1, 0, 0, 0, 1, 0},
{1, 0, 0, 1, 1, 1},
{1, 1, 0, 0, 0, 0},
{1, 1, 0, 0, 1, 1},
{1, 0, 0, 1, 1, 1}}
在这种情况下,输出:
1
Length[Total@#~Cases~0]&
相同的字节数
⨭МƟïⓜ⨴$
这是@Lynn的绝佳算法,但我是独立找到的。(我以为在某个地方有内置的,仍然看起来:P)
МƟï
转置输入数组,ⓜ⨴$
将每个内部矢量转换为其乘积,并对⨭
所得数组求和。
将输入作为行的数组,1
白色和0
黑色。
y xe
y xe
:Implicit input of array U.
y :Transpose.
e :Map over each sub-array, checking if every element is truthy.
x :Reduce by summing, converting booleans to 1 or 0.
:Implicit output of resulting integer.
y x_×
5。实际上,e
效果也一样×
,所以y xe
对于4 :-)