确定垂直切片


23

给定图像,输出[整个垂直截面的像素宽度] 1(如果存在)。如果不存在垂直截面,则输出0

输入可以作为本地文件或嵌套数组提供。如果选择将输入作为嵌套数组,则白色像素应由真实值表示,而非白色像素应由伪值表示。

1.连续的全白列的数量


您可以假设

  • 没有图像会大于1000平方像素

  • 每个图像最多只能有一个完整的垂直部分


例子

输入:

输出:

50
57
0
0

以下是前两个示例,以黄色突出显示它们的部分:


中间可以有黑色的小岛,以便有多个垂直部分吗?
xnor

@xnor:每个图像只会有一个完整的垂直部分。我将其添加到规范中。
扎克·盖茨

我的代码为第一个测试用例输出50,但为最后3个测试用例输出正确的数字,垂直切片从233到282列(= 50像素)。您能确认48是正确的数字吗?
大卫,

@David:我看到了从232到282列(不包括在内)的正确切片。我相信你是对的。
扎克·盖茨

2
我认为没有人遇到问题,但是值得一提的是,您正在寻找连续的全白列的数量。从示例中可以清楚地看出,但是通常最好不要依赖示例或测试用例。
MichaelS

Answers:


36

果冻,2个字节

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


21
ಠ_ಠ这种水平的高尔夫令我震惊。
Addison Crump

没有连续切片时的输出是什么?(有效输入)
艾迪生·克兰普

3
ps也可以在MATL中使用!
大卫,

然后将没有所有1s的列,这意味着Pwill 的结果是[0,0,0...0],而Sum是0预期的。
林恩

@David发布吗?Xps但是,如果图像可以是单行,则可能需要(或询问OP是否有最小尺寸)
Luis Mendo

7

APL,4个字节

+/×⌿

Try it here.

这是我的第一个APL答案!

感谢@ jimmy23013和@NBZ节省了字节!


这不是功能。(+/×/⍉)不起作用。
jimmy23013 '16

1
但是您可以使用(+/×⌿),而且短了1个字节。
jimmy23013 '16

删除括号再保存2个字节。其他APL的答案有很多只是一个匿名函数列车必须命名或括号使用:+/×⌿ f←+/×⌿ f picture
亚当

6

Bash +通用工具,17

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
$

6

Perl,21 22字节

固定版

包含+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)

4

Python 2,30个字节

使用许多我喜欢的内置函数链接在一起,这是一个出人意料的优雅解决方案。

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

4

Pyth,5岁

s*VFQ

在这里尝试

这使用了Lynn的算法,但是我决定发布它只是为了展示如何在Pyth中进行矢量操作。这里的窍门是将“糖”语法助手链接起来VF以便将折叠用作矢量运算。折叠的运算符当然是乘法,然后将结果求和以获得最终答案。


4

JavaScript(ES6),54 45 43字节

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答案,尽管由于使用everysome代替打高尔夫球reduce。第一个版本将black = 0编码,而第二个版本将black = 1编码。

编辑:由于@ edc65,又节省了2个字节。


3
尝试使用map
CalculatorFeline

我的数字是45。而你却没有尽力,因为它可能是43
edc65

a => a [s = 0] .map((_,i)=> s + =!a.some(b => b [i]))| s
edc65

1
@ edc65好吧,您知道计算的两个困难问题是缓存失效,命名和一字不漏的错误……
Neil

4

Ĵ5 6字节

以布尔矩阵为参数。

[:+/*/

这是我的第一个J答案!(错了1.5年……)

*/ 列乘积

+/ 和

[: 上限(自 +/不应接受左参数)

在线尝试!


3

CJam,7个字节

q~:.*:+

在线尝试!

q~      e# read input and evaluate: push nested array
:.*     e# fold vectorized product over nested array: element-wise product of rows
:+      e# fold addition over array: compute its sum

2

Mathematica 24

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]&相同的字节数
CalculatorFeline

1和0在Mathematica中不是真实的,也不是虚假的(如果是,则可能相反)。
马丁·恩德

1

𝔼𝕊𝕄𝕚𝕟,7个字符/ 9个字节

⨭МƟïⓜ⨴$

Try it here (Firefox only).

这是@Lynn的绝佳算法,但我是独立找到的。(我以为在某个地方有内置的,仍然看起来:P)

说明

МƟï转置输入数组,ⓜ⨴$将每个内部矢量转换为其乘积,并对所得数组求和。


1

Japt6个 4字节

将输入作为行的数组,1白色和0黑色。

y xe
  • 多亏了ETH,节省了2个字节。

测试一下


说明

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 :-)
ETHproductions

周末错过了该评论,@ ETHproductions-谢谢:)
Shaggy
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.