子数组中的数字相等


16

给定一个带有length >=3和的数字数组length % 3 == 0

[1, 2, 3, 4, ...]

您将其拆分为长度为3的子数组

[[1, 2, 3], [4, 5, ...], [...

并返回一个数组

  • [0] =>子数组中所有数字均相等的个案
  • [1] =>如果子数组中的所有数字都不相等,则子数组中只有两个数字相等的情况数

示例和测试用例:

  • 输入:[2, 4, 2, 5, 5, 5, 4, 2, 1, 3, 3, 1]输出[1, 2]

这是因为

[[2, 4, 2], [5, 5, 5], [4, 2, 1], [3, 3, 1]]
  ^     ^    ^  ^  ^               ^  ^ 
   equal    all equal              equal   

所以2 equal和1all equal

  • [3,5,6,5,5,7,6,6,8,7,7,7,3,4,2,4,4,3] => [1, 3]
  • [3,3,3,4,4,4,5,5,5,6,6,6,5,4,3] => [4, 0]
  • [3,4,5,6,7,8,9,8,7,6,5,4,3,2,1] => [0, 0]

这是,因此最短的答案以字节为单位。


PD:为我的英语道歉。


测试用例中的数字均为正。总是这样吗?
丹尼斯

@Dennis号可以是正数和负数。
路易斯·费利佩·德·耶稣·穆诺兹

Answers:


5

八度60 52 50字节

@(x)sum(sum(~diff(sort(reshape(x,3,[]))))'==[2 1])

在线尝试!

路易斯节省了8个字节!

说明:

将输入重塑为具有3行和适当数量的列的矩阵。然后,它对每列进行排序,并计算不同行上元素之间的差异。这给出了具有两行的矩阵,其中相同的数字将为零,而不同的数字将为正数。这是否定的,因此所有相等的元素都是1,所有不相等的都是0。然后,我们总结各自的栏目,让我们三个选项之一:0 = All elements are unequal1 = Two elements are equal2 = All elements are equal。然后>1,我们检查有多少个,以及究竟有多少个==1


4

JavaScript(ES6),70个字节

f=([a,b,c,...d],t=p=0)=>1/a?f(d,t+!(a-b&&a-c?b-c||++p:b-c&&++p)):[t,p]

在线尝试!

怎么样?

我们从输入数组中递归地提取每个三元组[a,b,c],并使用以下公式更新两个计数器t(三种)和p(对)。

t =
t + !(a - b && a - c ? b - c || ++p : b - c && ++p)

下面详细介绍了5种可能的情况,从“均等”到“均不相同”。

a b c | a-b && a-c | b-c | b-c || ++p | b-c && ++p | t +=
------+------------+-----+------------+------------+------------
4 4 4 | false      | 0   | n/a        | 0          | !0    --> 1
4 4 5 | false      | ≠0  | n/a        | ++p        | !++p  --> 0
4 5 4 | false      | ≠0  | n/a        | ++p        | !++p  --> 0
5 4 4 | true       | 0   | ++p        | n/a        | !++p  --> 0
4 5 6 | true       | ≠0  | ≠0         | n/a        | !(≠0) --> 0

如果输出可以有多个[0]并且[1]索引为“”,请注意:返回一个具有3个元素的数组,[0][1]返回适当的值,并[2]返回一个伪值(3个列表的数目,不包含任何共同的元素)。根据目前的规则。” codegolf.stackexchange.com/a/166082/31257 62字节a=>a.map(_=>++r[--new Set(a.slice(i,i+=3)).size],r=[i=0,i])&&r
guest271314

3

Pyth,13 14 12 11字节

/Lml{kcQ3S2

在这里尝试

说明

/Lml{kcQ3S2
      cQ3        Split the input into groups of 3.
  ml{k           Deduplicate and get the length of each.
/L               Count the number...
         S2      ... of 1s and 2s.

未能通过第三次测试(需要一些全相等和一些两个相等的三元组)
Jonathan Allan

3

05AB1E,10个字节

3ôεÙg}12S¢

在线尝试!

说明

3ô          # split input into groups of 3
  ε  }      # for each triple
   Ù        # remove duplicates
    g       # and get the length
      12S¢  # count the number of 1s and 2s in the result

3

OK17 16字节

+/(1 2=#=:)'0N3#

在线尝试!

            0N3# /reshape into groups of 3 (see ngn's comment)
  (       )'     /for each group:
        =:       /    make a map from number -> indices
       #         /    count number of keys/values
   1 2=          /    check if the count is equal to 1 or 2 
+/               /sum together the columns

对于k,17字节的版本是:+/(1 2=#=:)'0N 3#


0N 3-> 0N3(由于解析度用OK表示)
ngn

3

R,70个字节

function(v,x=lengths(by(v,seq(0,a=v)%/%3,table)))c(sum(x<2),sum(x==2))

在线尝试!

之前的版本 :

R,82字节

function(v,a=!1:2){for(i in lengths(by(v,seq(0,a=v)%/%3,table)))a[i]=a[i]+1;a[-3]}

在线尝试!


R,93字节

function(v,a=table(lengths(by(v,0:(length(v)-1)%/%3,unique)))[c('1','2')])`[<-`(a,is.na(a),0)

在线尝试!


1
移植八度音阶答案可能会更有效,但a=!1:2会更短一些。
朱塞佩

@ Giuseppe:谢谢,并使用seq(0,a=v)而不是0:(length(v)-1); 来保存了其他5个字节)不幸的是,我不知道八度,所以我无法轻松读取该答案……
digEmAll

@Giuseppe:更改了方法并节省了很多字节:)
digEmAll

好办法!通过applying 我得到了一些简化,unique但是在第三个测试用例中失败了。您的by方法更安全
-JayCe

@JayCe:幸运的是,R 3.2.0引入了可以节省很多字节的lengths函数...但是他们应该在R中引入一个简短的lambda函数定义,以便在代码高尔夫中更具竞争力:D
digEmAll


2

电源外壳,106字节

param($a)for(;$a){$x,$y,$z,$a=$a;if($x-eq$y-and$y-eq$z){$i++}else{$j+=$x-eq$y-or$y-eq$z-or$z-eq$x}}+$i,+$j

在线尝试!

锡罐上的字眼完全一样。循环输入$a。每次迭代都会剥离$x,$y,$z为下三个元素。测试if它们是否相等,如果相等,则递增$iElse$j如果至少一对相等,则递增。循环完成后,输出$i$j作为整数。

所以...很多...美元...


2

视网膜0.8.2,68字节

(.+)¶(.+)¶(.+)
;$1;$2;$3;$1;
%M`(;\d+)(?=\1;)
s`((1)|(3)|.)+
$#3 $#2

在线尝试!链接包括带有标题的测试用例,以将标题转换为每行一个值的所需格式。说明:

(.+)¶(.+)¶(.+)
;$1;$2;$3;$1;

用分隔符在每行上收集三个值,并在末尾复制第一个值。

%M`(;\d+)(?=\1;)

计算重复项对的数量。

s`((1)|(3)|.)+
$#3 $#2

计算3s和1s 的数量。




2

Common Lisp,113个字节

(lambda(l &aux(a 0)(b 0))(loop for(x y z)on l by #'cdddr do(if(= x y z)(incf a)(if(/= x y z)()(incf b))))`(,a,b))

在线尝试!

使用了以下事实:在Common Lisp中(= x y z),如果所有三个元素都相等,(/= x y z)则为true;如果没有对数相等,则为true。


2

Japt,14 13字节

2õ@ò3 x_â ʶX

尝试一下


说明

2õ                :Range [1,2]
  @               :Pass each X through a function
   ò3             :  Split input to arrays of length 3
       _          :  Pass each through a function
        â         :    Remove duplicates
          Ê       :    Get length
           ¶X     :    Test for equality with X
      x           :  Reduce by addition


2

视网膜,23字节

S2,3,` 
%Cq`\S+
*\C`1
2

在线尝试!

说明

S2,3,` 

从(从0开始)第二个开始,在每个第三个空格处分割输入,即,将输入分成三个组。

%Cq`\S+

在每行(%)上计算C唯一(q)个值(\S+)的数量()。

*\C`1

计算1s 的数量,并在末尾使用换行符(\)打印它们,但以空运行(*)进行打印,这样我们就不会丢失之前的结果。

2

计算2s 的数量(并自动打印)。


2

J16 15字节

-1字节感谢科尔!

1#.1 2=/_3#@=\]

在线尝试!

与大多数解决方案几乎相同的方法。

说明:

        _3    \]  - split the input into sublists of lenght 3
          #@~.    - for each triplet remove duplicates and take the length 
   1 2=/          - compare with 1 and 2
1#.               - add up

#@~.->#@=
科尔


1

Stax,14 个字节

ü┬─*HTÜ╫\Ä╢qm♥

运行并调试


[3,5,6,5,5,7,6,6,8,7,7,7,3,4,2,4,4,3][2,3]取而代之的是输出[1,3]
路易斯·费利佩·德·耶稣·穆诺兹

[3,3,3,4,4,4,5,5,5,6,6,6,5,4,3][1,0]取而代之的是输出[4,0]
路易斯·费利佩·德·耶稣·穆诺兹

[3,4,5,6,7,8,9,8,7,6,5,4,3,2,1][5,0]取而代之的是输出[0,0]
路易斯·费利佩·德·耶稣·穆诺兹

@LuisfelipeDejesusMunoz已修复
wastl

目前没有显示任何输出[1,1,1]。如果您使用2(而不是1T它将始终修剪/填充到确切的大小
递归



1

灵药,92字节

fn a->import Enum;c=map chunk(a,3),&(length uniq&1);{count(c,&(&1==1)),count(c,&(&1==2))}end

首先,将列表分块为大小长度3 chunk(a,3)

其次,它进行转换以找到每个元素的长度,并求其唯一性。map chunk(a,3),&(length uniq&1)

最后,它返回一个数组,该数组由结果列表等于1 count(c,&(&1==1))的次数和结果列表等于2的次数组成count(c,&(&1==2))

在线尝试!



0

Tcl,111字节

proc S {L a\ 0 e\ 0} {lmap {x y z} $L {expr {$x-$y|$y-$z?$x==$y|$y==$z|$x==$z?[incr e]:0:[incr a]}}
list $a $e}

在线尝试!


Tcl,112字节

proc S {L a\ 0 e\ 0} {lmap {x y z} $L {expr {$x-$y||$y-$z?$x==$y|$y==$z|$x==$z?[incr e]:0:[incr a]}}
list $a $e}

在线尝试!


Tcl,114字节

proc S {L a\ 0 e\ 0} {lmap {x y z} $L {expr {$x==$y&&$y==$z?[incr a]:$x==$y|$y==$z|$x==$z?[incr e]:0}}
list $a $e}

在线尝试!



0

Tcl,98字节

proc A l {set 1 0;set 2 0
foreach a\ b\ c $l {incr [llength [lsort -u "$a $b $c"]]}
return $1\ $2}

在线尝试!

使用命令-unique选项lsort。我叫12我为了方便变量,坚韧似乎很寻常的代码set 1 0:)


0

C#(Visual C#交互式编译器),108字节

x=>new[]{1,2}.Select(n=>x.Select((v,i)=>(v,g:i/3)).GroupBy(y=>y.g,y=>y.v).Count(y=>y.Distinct().Count()==n))

在线尝试!

少打高尔夫球...

// x is the input list of ints
x=>x
  // 1 distinct number means 3/3 are the same
  // 2 distinct number means 2/3 are the same
  new[]{1,2}
  // iterate over the outer array to get an index
  .Select(n=>x
    // iterate over the whole list with an index
    // and break into groups of size 3
    .Select((v,i)=>v,g:i/3))
    .GroupBy(y=>y.g,y=>y.v)
     // count the distinct values in each group
     // and get the result based on outer array value
    .Count(y=>y.Distinct().Count()==n))
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.