模组平衡清单


14

介绍

假设我有一个整数列表,比如说L = [-1,2,2,1,2,7,1,4]。我喜欢在生活中保持平衡,因此很高兴看到它具有与偶数元素一样多的奇数元素。更重要的是,它在3的所有模数类中也具有相等数量的元素,并且其元素位于:

         [-1,2,2,1,2,7,1,4]
0 mod 3:
1 mod 3:         1   7 1 4
2 mod 3:  -1 2 2   2

可悲的是,对于4的模数类,它不再成立。一般情况下,我们说一个非空列表被模平衡Ñ如果它具有相同数量的在所有模类元素Ñ指此数目不是0上面所列大号是平衡的模2和3,但非平衡模4。

任务

您的输入是一个非空列表L,该列表以任何合理的格式获取整数。您的输出是N≥2的那些整数的列表,以使LN为模,并且仍然采用任何合理的格式。输出的顺序无关紧要,但是它不应包含重复项。

确保输出中只有有限数量的数字,这恰恰意味着并非L的所有元素都在其中出现相同的次数。无效输入的示例为[3][1,2][0,4,4,0,3,3]。请注意,输出中的最大数字最多为max(L)-min(L)

每种语言的最低字节数为准,并且适用标准规则。

测试用例

[1,1,2] -> []
[1,1,5] -> [2,4]
[1,1,24] -> [23]
[1,2,3,2] -> [2]
[12,12,-4,20] -> [2,3,4,6,8,12,24]
[1,1,12,12,-3,7] -> [3,10]
[-1,2,2,1,2,7,1,4] -> [2,3]
[4,-17,-14,-18,-18,3,5,8] -> []
[-18,0,-6,20,-13,-13,-19,13] -> [2,4,19]
[-11,-19,-19,3,10,-17,13,7,-5,16,-20,20] -> []
[3,0,1,5,3,-6,-16,-20,10,-6,-11,11] -> [2,4]
[-18,-20,14,13,12,-3,14,6,7,-19,17,19] -> [2,3]
[-16,-9,6,13,0,-17,-5,1,-12,-4,-16,-4] -> [3,9]
[-97,-144,3,53,73,23,37,81,-104,41,-125,70,0,111,-88,-2,25,-112,54,-76,136,-39,-138,22,56,-137,-40,41,-141,-126] -> [2,3,6]

某些自动计算上限的语言(也许是
Braachylog

Answers:


4

05AB1E,11个字节

ÄOL¦ʒ%{γ€gË

在线尝试!

ÄOL¦ʒ%{γ€gË  | Full program.

Ä            | Absolute value (element-wise).
 O           | Sum.
  L          | 1-based inclusive range.
   ¦         | Remove the first element (generates the range [2 ... ^^]).
    ʒ        | Filter / Select.
     %       | Modulo of the input with the current integer (element-wise).
      {      | Sort.
       γ     | Group into runs of adjacent elements.
        €g   | Get the length of each.
          Ë  | Are all equal?

4

Wolfram语言(Mathematica)56 52字节

感谢Not a tree,节省了4个字节。

Cases[Range[2,#.#],n_/;Equal@@Last/@Tally[#~Mod~n]]&

在线尝试!

打高尔夫球的主要技巧是使用绝对值总和(或1-范数)平方值的总和(而不是将其作为点积计算)作为上限Max@#-Min@#。否则,它将只是从字面上实现规范。


3

Perl 6的 52  48个字节

{grep {[==] .classify(*X%$^a).values},2.. .max-.min}

测试一下

{grep {[==] bag($_ X%$^a).values},2.. .max-.min}

测试一下

展开:

{  # bare block lambda with implicit parameter 「$_」

  grep

    {  # bare block lambda with placeholder parameter 「$a」

      [==]           # reduce with &infix:«==» (are the counts equal to each other)

        bag(         # group moduluses together

          $_ X% $^a  # find all the moduluses using the current divisor 「$a」

        ).values     # the count of each of the moduluses
    },

    2 .. .max - .min # all possible divisors
}

3

Haskell85 84字节

f l=[n|n<-[2..sum$abs<$>l],all.(==)=<<head$[r|m<-[0..n],_:r<-[[0|x<-l,mod x n==m]]]]

在线尝试!使用绝对值之和作为Martin Ender答案的最大值。

编辑:-1字节,感谢ØrjanJohansen。

说明:

f l=                             -- Given a list of numbers l,
  [n|n<-                       ] -- return a list of all numbers n of the range
    [2..sum$abs<$>l],            -- from 2 to the sum of the absolute values of l
      all.(==)=<<head$           -- for which all elements of the following list are equal:
        [r|m<-[0..n],         ]  -- A list r for each number m from 0 to n, where
          _:r<-[             ]   -- r is the tail of a list (to filter out empty lists)
          [0|x<-l,mod x n==m]    -- of as many zeros as elements of l mod n equal m.


2

R75 72字节

function(L){for(x in 2:(max(L)-min(L)))F=c(F,!sd(table(L%%x)))
which(F)}

在线尝试!

用于table计算每个整数模的计数xsd一组数字的标准偏差为零(如果它们均相等),否则为正。因此!sd(table(L%%x))TRUE无论哪里L是mod平衡mod x,否则为false。然后将这些值串联到中F

which然后从函数返回真值的索引。由于R使用基于1的索引,F并且最初是带有value的长度为一的向量FALSE,因此它将正确返回以开头的值2

可能会期望内置函数range来计算数据集范围,即max(D)-min(D),但是可悲的是它会计算并返回向量c(min(D), max(D))


2

干净,121字节

使用Martin Ender答案中的绝对和技巧。

打高尔夫球:

import StdEnv   
f l=[n\\n<-[2..sum(map abs l)]|length(removeDup[length[m\\m<-[(e rem n+n)rem n\\e<-l]|m==c]\\c<-[0..n]])<3]

可读性:

import StdEnv
maximum_modulus l = sum (map abs l)
// mod, then add the base, then mod again (to fix issues with negative numbers)
list_modulus l n = [((el rem n) + n) rem n \\ el <- l]
// count the number of elements with each mod equivalency
equiv_class_count l n = [length [m \\ m <- list_modulus l n | m == c] \\ c <- [0..n]]
// for every modulus, take where there are only two quantities of mod class members
f l=[n \\ n <- [2..maximum_modulus l] | length (removeDup (equiv_class_count l n)) < 3]

在线尝试!


1

果冻,12字节

⁹%LƙE
ASḊçÐf

在线尝试!

感谢user202729保存一个字节,并感谢Martin Ender(间接)保存一个字节。

怎么运行的

⁹%LƙE ~ Helper link. Let's call the argument N.

⁹%    ~ Modulo the input by N (element-wise).
  Lƙ  ~ Map with length over groups formed by consecutive elements.
    E ~ All equal?

ASḊçÐf ~ Main link.

AS     ~ Absolute value of each, sum.
  Ḋ    ~ Dequeue (create the range [2 ... ^]).
   çÐf ~ Filter by the last link called dyadically.

可以在线尝试一种单线替代12轮


我删除了答案,因为现在已经多余了。感谢马丁ASSAbsolutes)了。
user202729

1
作为未来读者的参考,我澄清ḟ0聊天中为什么不需要。
Xcoder先生17年

1

Python 3, 120个 102字节

不太漂亮。

-18个字节感谢Xcoder先生

f=lambda a,i=2:[]if i>max(a)-min(a)else(len({*map([k%i for k in a].count,range(i)),0})<3)*[i]+f(a,i+1)

在线尝试!


1

玛特,19字节

-4个字节感谢Luis Mendo!

S5L)d:Q"G@\8#uZs~?@

在线尝试!

在R中的答案的端口。

Suppose we have input [12,12,-4,20]
         # (implicit input), stack: [12,12,-4,20]
S        # sort the list, stack: [-4, 12, 12, 20]
5L       # push [1 0], stack: [[-4, 12, 12, 20]; [1 0]]
)        # 1-based modular index, stack: [-4, 20]
d        # successive differences, stack: [24]
:        # generate 1:(max(data)-min(data)), stack: [[1...24]]
Q        # increment to start at 2, stack: [[2...25]]
"        # for each element in [2...25]
 G       # push input, stack: [[12,12,-4,20]]
 @       # push loop index, stack: [[12,12,-4,20], 2]
 \       # compute modulo, stack: [[0,0,0,0]]
 8#      # use alternate output spec, unique has 4 outputs, so 8 keeps only the 4th
 u       # unique. 4th output is the counts of each unique value, stack: [4]
 Zs      # compute standard deviation, stack: [0]
 ~       # logical negate, stack: [T]
 ?       # if true,
  @      # push loop index
         # (implicit end of if)
         # (implicit end of for loop)
         # (implicit output of stack as column vector


您可以使用缩短一点S5L)d,而不是X>GX<-8#u替代FFFT#u
路易斯Mendo

@LuisMendo我不[1 0]知道该怎么推(但是我知道有可能),所以5L非常方便,我*still* really need to go and properly read the docs for #`:(但谢谢!
Giuseppe

对于#,指定一个大于最大输出数量的数字只是选择单个输出。用函数u的最大值为4,所以5#uT#u6#uFT#u
路易斯Mendo

0

JavaScript(ES6),117个字节

输出以空格分隔的值列表。

a=>(g=m=>a.map(n=>x[k=(z|=m/2<n|m/2<-n,n%m+m)%m]=-~x[k],y=z=0,x=[])|z?(x.some(x=>x-(y?y:y=x))?'':m+' ')+g(m+1):'')(2)

测试用例


0

Clojure,91字节

#(for[i(range 2(apply +(map * % %))):when(apply =(vals(frequencies(for[j %](mod j i)))))]i)

使用frequencies代码高尔夫球并不理想。


0

J,38个字节

[:}.@I.([:i.1#.|)(1=1#.[:~:|#/.])"0 1]

归功于Xcoder先生以获取绝对值的总和。

如果需要,可以在TIO链接中进行编辑-我有点急于打高尔夫。

说明和TIO链接即将推出。


0

APL(Dyalog)43 41 38 30字节

代码中的⍨讲述了整个故事。

@Adám节省了8个字节

x⊆⍨1=⊂(≢∘∪1⊥|∘.=|)¨⍨x1+∘⍳1⊥|

在线尝试!


火车+深度→行,30个字节:∊x⊆⍨1=⊂(≢∘∪1⊥|∘.=|)¨⍨x←1+∘⍳1⊥|
2015年
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.