伪造简短事实


28

在布尔值列表中找到最长的true。返回相同的列表,所有其他true均被伪造。

输入输出

一个列表; 任何常规格式(例如,分隔列表为字符串)。

细节

正确和错误可以是您的语言通常用于这些值的任何值,也可以是整数1和0。如果您使用单个字符,则列表可以是串联(例如10001)。

如果有最长的并列,请保持所有并列运行正确,并伪造所有较短的运行。

例子

input ↦ output
1,0,1,0,1 ↦ 1,0,1,0,1
1,1,0,1,1,0,1 ↦ 1,1,0,1,1,0,0
1,1,0,1,1,1,0,1,1 ↦ 0,0,0,1,1,1,0,0,0
1,1,1 ↦ 1,1,1
0,0,1 ↦ 0,0,1
0,0 ↦ 0,0
1,1,1,0,0,0,1,1,1,1,0,1,0,0,1,1,0,1,1,1,1,0,0,1,0 ↦ 0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0

(直接来自https://stackoverflow.com/q/37447114

Answers:


19

果冻,8 字节

ṣ0¬¬M¦j0

在线尝试!验证所有测试用例

怎么运行的

ṣ0¬¬M¦j0  Main link. Argument: A (list of Booleans)

ṣ0        Split at zeroes. This leaves a 2D list of ones.
  ¬       Negate each 1, replacing it with 0.
     ¦    Conditional application:
    M       Yield all maximal indices.
            In lexicographical list comparison, a shorter list of zeroes is less
            than a longer one, so this identifies the longest runs.
   ¬        Negate the items in those lists, changing zeroes back to ones.
      j0  Join, separating by single zeroes.

23
哎呀

11

Haskell中,595855,64个字节

import Data.List
((=<<)=<<(=<<)(<$).(==).maximum.([1<2]:)).group

有趣的是,这适用于其中的任何值列表falsy < truthy。所以False/True0/1'f'/'t',等。

注意:

正如几个人指出的那样(包括@proud haskeller@nimi),以前的版本在所有虚假值的列表上均失败。如.([1<2]:)所建议,添加修复了此问题@proud haskeller。我暂时保留相同的解释,因为我认为这仍然有意义。如果有人发表评论并要求解释,我将进行编辑。

说明:

我将首先使用不带的糖group,然后再添加回去。首先,我发现单词在视觉上通常比符号更容易理解,因此我将进行一些替换。(请注意,这=<<是“经典”的,因此它对于列表和函数的适用方式有所不同。我称其bind=<<for函数的版本。)

bind :: (a -> b -> c) -> (b -> a) -> b -> c
bind k f = k =<< f
bind k f = \ r -> k (f r) r

f = ((=<<)=<<(=<<)(<$).(==).maximum)
f = ((bind) concatMap (bind)(<$).equals.maximum)
f = (bind concatMap (bind (<$) . equals . maximum))
f = bind concatMap ((bind (<$)) . equals . maximum))
f = bind concatMap ((\f r -> (<$) (f r) r) . equals . maximum))
f = bind concatMap ((\f r -> (f r) <$ r) . equals . maximum)
f = bind concatMap ((\g r -> (g r) <$ r) . equals . maximum)
f = (\h r -> concatMap (h r) r) ((\g r -> (g r) <$ r) . equals . maximum)
f = \r -> concatMap (((\g r -> (g r) <$ r) . equals . maximum) r) r
f = \r -> concatMap (((\g r -> (g r) <$ r) . equals) (maximum r)) r
f = \r -> concatMap (((\g s -> (g s) <$ s)) (equals (maximum r))) r
f = \r -> concatMap (((\s -> ((equals (maximum r)) s) <$ s))) r
f = \r -> concatMap (\s -> (s == (maximum r)) <$ s) r

f . group = ((=<<)=<<(=<<)(<$).(==).maximum).group
f . group = \r -> concatMap (\s -> (s == (maximum (group r))) <$ s) (group r)

最后一个细节是x <$ list将的每个元素替换为listx并将其group list拆分list成相等元素的块。所以group [1, 1, 2, 3, 3, 3] == [[1, 1], [2], [3, 3, 3]]

总而言之,该函数将值列表分为仅真值组和仅假值组。然后,对于每个组,用语句的结果替换每个元素this is the biggest group(的最大组true将是最大),然后将这些组连接起来。

保存的四个字节 @Zgarb


1
我认为您可以替换(\y->(maximum g==y)<$y)((<$)=<<(==maximum g))。我还没有测试过。
Zgarb '16

@Zgarb我只是从实例声明中得出了结论,并且它可以工作。谢谢。
迈克尔·克莱恩

3
更好的是:用无f点函数代替整个定义((=<<)=<<(=<<)(<$).(==).maximum).group。保存三个字节,完全不可读!
Zgarb '16

@Zgarb:太酷了!那时,b=(=<<);b b(b(<$).(==).maximum).group还少一个字节。我以前从未在Haskell高尔夫中见过这样的东西:)
Lynn

1
如果我没记错的话,可以通过(:[t])在最大值或类似内容之前插入来解决它
骄傲的haskeller

6

视网膜 47 43 36

0
!
T`p`0`\b(1+)\b(?<=(?=.*1\1).*)|!

在线尝试!尝试所有测试用例

感谢msh210打高尔夫球4个字节!

也要感谢Martin 7个字节!

说明:

0
!

将所有0s 替换为!s。这样做是为了使匹配群体1比较短,因为现在1!!1将有一个字边界(\b它们之间),它也匹配字符串的开头或结尾。

T`p`0`

这是一个配置选项,表示在反引号之后对输入应用正则表达式后,在每次匹配中都将每个可打印的ASCII字符转换为一个 0字符。

\b(1+)\b(?<=(?=.*1\1).*)|!

此正则表达式匹配1被零包围的s 组,但不能匹配1字符串中随处跟随其后的a 。这些是伪造的非最大群体。另外,这也匹配!我们添加的字符以将它们转换回0s。


5

MATL,14个字节

Y'yy*X>y=b*wY"

在线尝试!

所有测试用例的修改版本

说明

        % Implicitly grab the input as an array
Y'      % Perform run-length encoding of the input. Yields an array of values and an array
        % of run-lengths
yy      % Copy these outputs
*       % Multiply the values (booleans) by the run-lengths. This will zero-out all
        % zero-valued runs so we don't consider them when computing the longest run.
X>      % Compute the longest run of 1's
y       % Copy the run lengths vector
=       % Determine which runs are the same length as the longest run of ones
b*      % Bubble-up the values from the run-length encoding and multiply element-wise
        % With this boolean. This substitutes all 1's that are not in the longest run
        % of ones with 0's
w       % Flip the run-lengths and values on the stack
Y"      % Perform run-length decoding using these substituted values
        % Implicitly display the resulting boolean

4

Python 2,62个字节

lambda s:'0'.join(`1-(t+'1'in s)`*len(t)for t in s.split('0'))

Ideone上进行测试

怎么运行的

s.split('0')将输入字符串s分为零个或多个1的游程

对于每次运行t,我们检查是否t+'1's的子字符串。

  • 如果是,则运行不是最大,t+'1'in s返回True1-(t+'1'in s)返回1-True = 0,并且运行被替换为长度相同的0

  • 如果不是,则运行次数为最大值,t+'1'in s返回False1-(t+'1'in s)返回1-False = 1,并且该运行次数被长度相同的1的运行次数(即,其自身)替换。

最后,'0'.join恢复所有删除的0


3

J,25个字节

[:(}.=>./)@;0<@(*#);.1@,]

这是一个单音动词,它接受并返回0-1数组。像这样使用它:

   f =: [:(}.=>./)@;0<@(*#);.1@,]
   f 1 1 0 1 1 1 0 1 1
0 0 0 1 1 1 0 0 0

说明

[:(}.=>./)@;0<@(*#);.1@,]  Input is y.
            0          ,]  Prepend 0 to y, and
                   ;.1@    cut the result along occurrences of 0,
                           so that each piece begins with a 0.
               (*#)        Multiply each piece element-wise by its length,
             <@            and put it in a box.
                           Without the boxing, the pieces would go in a 0-padded array.
           ;               Join the pieces back together.
                           Now all runs of 1 have been replaced by runs of (1+length of run).
[:(      )@                Apply verb in parentheses:
   }.                        remove the prepended 0,
     =                       form the 0-1 array of equality with
      >./                    the maximum value.

很好用的切;.
2016年

3

Pyth,26 24 23 21字节

M,G&HGJrgMrQ8 9qReSJJ

测试套件。

  • 在输入中使用1/0true/false
  • 用于true/false输出。

说明

M,G&HGJrgMrQ8 9qReSJJ

           Q      input
          r 8     run-length encode
        gM        convert each run of 1 to their length
                  for example: [1,1,1,0,1,1] will be
                  converted to [3,3,3,0,2,2]
                  in the run-length encoded version
                  [1,1,1,0,1,1] will be [[3,1],[1,0],[2,1]]
                  [3,3,3,0,2,2] will be [[3,3],[1,0],[2,2]]
                  therefore basically [G,H] becomes [G,H and G]
                  which is what the code below does:
M,G&HG            def g(G,H): return [G,H and G]
       r      9   run-length decode
      J           store to J

               qReSJJ

                R   J   in each element of J
               q eSJ    check if equal to maximum of J

前23个字节

M,G&HGJrgMrQ8 9msqdeSJJ

测试套件。

  • 在输入中使用1/0true/false
  • 用于1/0输出。

前24个字节

Jrm,hd&edhdrQ8 9msqdeSJJ

测试套件。

  • 在输入中使用1/0true/false
  • 用于1/0输出。

前26个字节

rm?nhdeS.u&YhNQ0,hd0drQ8 9

测试套件。

  • 在输入中使用1/0true/false
  • 用于1/0输出。

创建仅在单个位置调用的函数几乎总是一个错误。例如,您可以将其替换为:Jr.b,N&YNrQ8)9qReSJJJrm,hd*FdrQ8 9qReSJJ。两种版本都保存一个字节。或者甚至更疯狂地JrXR1*FdrQ8 9qReSJJ保存两个。;-)
雅库布

2

的Oracle SQL 12.1 137个 135字节

SELECT REPLACE(REPLACE(REPLACE(:1,m,2),1,0),2,m)FROM(SELECT MAX(TRIM(COLUMN_VALUE))m FROM XMLTABLE(('"'||REPLACE(:1,0,'",0,"')||'"')));

未打高尔夫球

-- Replace the max value with 2
-- Then replace every 1 with 0
-- Then replace 2 with the max value
SELECT REPLACE(REPLACE(REPLACE(:1,m,2),1,0),2,m)
FROM   ( -- Split on 0 and keep the max value
         SELECT MAX(TRIM(COLUMN_VALUE))m 
         FROM XMLTABLE(('"'||REPLACE(:1,'0','",0,"')||'"'))
       );

输入使用单个字符。例如:“ 1100111”


2

Mathematica46 41

1-Join@@Sign[1~Max~#-#]&[#*Tr/@#]&@*Split

适用于0和的列表1。我以为自己做得不错,直到我看了其他答案!


46个字符版本的说明;当我无法进一步改进时,我将进行更新。

要求对此代码进行解释。
非代码高尔夫等效项(使用版本10的操作员表格)为:

RightComposition[
  Split,
  Map[# Tr@# &],
  # - Max[1, #] &,
  UnitStep,
  Apply[Join]
]

这意味着从上到下依次应用由五个步骤(子功能)组成的功能。

  • Split:分解成相同元素的行:{1,1,0,1,1,0,1}↦{{1,1},{0},{1,1},{0,0}}

  • Map[# Tr@# &]:对于每个子列表(Map),将()乘以#其总和(矢量迹线,Tr):{1,1}↦{2,2}

  • # - Max[1, #] &从每个元素中减去出现在列表列表中任何位置的最大值,或两者之一,以较高者为准。(一个处理全零的情况。)

  • UnitStep:对于x <0,等于0;对于x> = 0,等于1,应用于每个元素。

  • Apply[Join]:将子列表合并为一个列表。也可以用Flatten或来完成Catenate,但简而言之Join@@更简洁。


2

C,135 129字节

在线尝试

m,c,i,d,j;f(int*l,int s){while(i<s)c=l[i++]?c+1:0,m=c>m?c:m;while(j<s)if(l[j++])d=d+1;else if(d<m)while(d)l[j-1-d--]=0;else d=0;}

不打高尔夫球

m,c,i;
f(int*l,int s)
{
    // obtain max
    while(i<s)
        c = l[i++] ? c+1 : 0,
        m = c>m ? c : m;

    c=0,i=0;

    // remove smaller segments
    while(i<s)
        if(l[i++]) c=c+1;
        else if(c<m) while(c) l[(i-1)-c--]=0;
        else c=0;
}

1

JavaScript(ES6),56个字节

s=>s.replace(/1+/g,t=>t.replace(/1/g,+!~s.indexOf(t+1)))

通过检查所有1的运行并将字符替换为0来工作,除非运行(相等)最长(通过在字符串中搜索较长的1来衡量)。

先前的72字节递归解决方案:

f=s=>/11/.test(s)?f(s.replace(/1(1*)/g,"0$1")).replace(/0(1+)/g,"1$1"):s

如果没有1(即最多1),则不执行任何操作。否则,1从每个1运行中减去一个,然后在较短的运行中递归调用自身,然后1在(现在同样最长的)运行中加一个后退。递归调用的数量比最长运行的长度少一。


“在所有1s的运行中,如果存在1s的运行比当前运行的时间长,则将每个1用0替换,否则用0替换。” 辉煌!
帕特里克·罗伯茨

1

朱莉娅51字节

s->replace(s,r"1+",t->map(c->c-contains(s,"1"t),t))

在线尝试!

怎么运行的

replace通过正则表达式查找输入字符串s中一个或多个1的所有所有游程,并调用lambda 确定替换字符串。r"1+"t->map(c->c-contains(s,"1"t),t)

lambda映射c->c-contains(s,"1"t)所有以t开头的字符。

  • 如果"1"t(串联)是的一个子小号,运行不是最大时,contains返回c-contains(s,"1"t)返回“1” -真=“0”,更换所有1的在与该运行0的。

  • 如果"1"t(concatenation)不是s的子字符串,则运行为最大值,contains返回falsec-contains(s,"1"t)返回'1'-false ='1',而运行保持不变。


1

APL,22个字符

(⊣=⌈/)∊(⊣×+/¨)(~⊂⊣)0,⎕

用英语(从右到左,以块为单位):

  • 在输入之前加上0
  • 以每个0开头的框
  • 将每个框乘以其总和
  • 展平
  • 如果数字等于最大值,则为1,否则为0

1

Java 8,205字节

这是Lambda表达式,用于Function<String,String>

s->{int x=s.length();for(String t="1",f="0";s.indexOf(t+1)>=0;t+=1){s=s.replaceAll(0+t+0,0+f+0);if(s.indexOf(t+0)==0)s=s.replaceFirst(t,f);if(s.lastIndexOf(0+t)==--x-1)s=s.substring(0,x)+f;f+=0;}return s;}

输入/输出是一个 String true,用1表示false,用0表示false。没有分隔符将值分开。

带有解释的代码:

inputString -> {
  int x = inputString.length();
  //starting with the truth combination "1",
  //loop until the input string does not contain the combination appended with another "1"
  //with each consecutive loop appending a "1" to the combination
  for( String truthCombo = "1", falseCombo = "0"; inputString.indexOf( truthCombo + 1 ) >= 0; truthCombo += 1 ) {
    //all instances in the input string 
    //where the combination has a "0" on either side of it
    //are replaced by "0"'s
    inputString = inputString.replaceAll( 0 + truthCombo + 0, 0 + falseCombo + 0 );
    //if the combination followed by a "0"
    //is found at the beginning of the input string
    //replace it with "0"'s
    if( inputString.indexOf( truthCombo + 0 ) == 0 )
      inputString = inputString.replaceFirst( truthCombo , falseCombo );
    //if the combination preceeded by a "0"
    //is found at the end of the input string
    //replace it with "0"'s
    if( inputString.lastIndexOf( 0 + truthCombo ) == --x - 1 )
      inputString = inputString.substring( 0, x ) + falseCombo;
    falseCombo += 0;
  }
  return inputString;
}

有关测试用例,请参见ideone


1

Clojure,137个字节

#(let[v(map(juxt first count)(partition-by #{1}%))](mapcat(fn[t](repeat(t 1)(if(=[1(apply max(map(fn[[f c]](if(= 1 f)c 0))v))]t)1 0)))v))

首先将输入划分为连续的零和一,并将它们映射到划分的第一个元素和元素计数的“元组”。然后,根据所需的零或一的最大长度序列,重复所需的零或一的数目。

少打高尔夫球:

(def f #(let [v(map(juxt first count)(partition-by #{1}%))
              m(apply max(map(fn[[f c]](if(= 1 f)c 0))v))]
           (mapcat (fn[[f c]](repeat c(if(=[1 m][f c])1 0))) v)))

0

Perl 5,68位元组

67,加1 -pe代替-e

y/0/ /;$_<${[sort@a]}[-1]&&y/1/0/for@a=split/\b/;$_=join"",@a;y; ;0

期望并输出0和1的字符串(串联)。

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.