平均两个清单


11

平均两个清单

挑战

给定两个正整数列表,请确定是否有可能将元素重新排列为两个新列表,以使新列表具有相同的算术平均值(平均值)。

输入值

输入可以通过STDIN或作为函数参数获取。输入可以视为列表,或者如果您的语言不支持列表(或类似的数组/字典),则可以将输入作为逗号或空格分隔的字符串。那是,

"1 4 8 2 5,3 1 5 2 5"

是相同的:

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

所有输入列表的长度将相同

输出量

如果可以创建两个具有相同平均值的新列表,则程序/函数应打印或返回平均值。如果不能,则程序应输出一张悲伤的脸:(

请注意,具有相等均值的重新排列的列表(如果存在)不必具有相同的长度。可以进行任意数量的交换以创建新列表。

例子

1 4 8 2 5,3 1 5 2 5 -> 1 4 8 2 3,5 1 5 2 5 (swapped 3 and 5) -> 3.6
1 3 6 2,16 19 19 14 -> [[1,6,19,14],[3,2,16,19]] -> 10
2 6 2,6 3 5 -> 2 6,2 6 3 5 (moved 2) -> 4
90 80 20 1,40 60 28 18 -> :(

这是因此以字节为单位的最短代码获胜。与往常一样,不允许出现标准漏洞


2
我们可以交换每个列表中任意数量的元素吗?一个列表可以给另一个元素吗?我不明白您所说的“存在有效置换”的意思。另外,这需要更多的测试用例。
xnor

@xnor您可以将一个元素移动到另一个元素。我将添加更多测试用例
Downgoat 2015年

那么,这是否等同于“给定一个列表(它们的并集),可以将其划分为两个具有相同平均值的非空列表吗?”
xnor

1
@ vihan1086为什么不将单个列表作为输入呢?您的演示似乎不必要地复杂。
xnor

2
@ vihan1086看着您的Sandbox帖子,在那里提出了许多相同的要求澄清的内容,您说您已经澄清了许多这些要点,但是您所做的修改并没有使它们更加清晰。您最好替换混乱的文本,而不是添加其他文本。
xnor

Answers:


12

Pyth,24个字节

?}KcsJsQlJmcsdldtPyJK":(

在线试用:演示

感谢Dennis注意到错误并打了一个字节。

说明:

?}KcsJsQlJmcsdldtPyJK":(   implicit: Q = evaluated input
      sQ                   all numbers of Q
     J                     save them in J
  KcsJ  lJ                 average of J (sum(J) / len(J))
                           store in K
          m     tPyJ       map each nonempty subset d of J to:
           csdld             average of d
?}                         if K in ^:
                    K        print K
                     ":(   else print sad-face

5
做得好,+ 1。但是,Pyth真的没有内置的均值计算工具吗?
Alex A.

@AlexA。现在它确实有一个(即.O
Xcoder先生17年

6

SWI-Prolog,159字节

a(A,B):-append([A,B],R),permutation(R,S),append([Y,Z],S),sum_list(Y,I),sum_list(Z,J),length(Y,L),length(Z,M),L\=0,M\=0,I/L=:=J/M,W is J/M,write(W);write(':(').

称为 a([1,4,8,2,5],[3,1,5,2,5]).


5

朱莉娅101字节

f(a,b)=(m=mean;p=filter(i->m(i[1])==m(i[2]),partitions([a,b],2));isempty(p)?":(":m(collect(p)[1][1]))

这将创建一个接受两个数组并相应地返回字符串或浮点数的函数。

取消+说明:

function f(a,b)
    # Get the set of all 2-way partitions of the array [a,b]
    l = partitions([a,b], 2)

    # Filter the set of partitions to those where the two
    # contained arrays have equal means
    p = filter(i -> mean(i[1]) == mean(i[2]), l)

    # Return a frown if p is empty, otherwise return a mean
    isempty(p) ? ":(" : mean(collect(p)[1][1])
end

2

R,94字节

我认为与Jakubes基本相同。如果两个列表的均值与列表中值的任何组合的均值匹配(但不包括列表的组合长度),则输出均值,否则输出可悲的表情。

if(mean(l<-scan())%in%unlist(sapply(2:length(l)-1,function(x)combn(l,x,mean))))mean(l)else':('

测试运行

> if(mean(l<-scan())%in%unlist(sapply(2:length(l)-1,function(x)combn(l,x,mean))))mean(l)else':('
1: 1 4 8 2 5
6: 3 1 5 2 5
11: 
Read 10 items
[1] 3.6
> if(mean(l<-scan())%in%unlist(sapply(2:length(l)-1,function(x)combn(l,x,mean))))mean(l)else':('
1: 90 80 20 1
5: 40 60 28 18
9: 
Read 8 items
[1] ":("

0

果冻,22字节

FŒ!œs2ÆmE$$Ðf⁾:(ÆmX$Ṇ?

在线尝试!

聊天室Xcoder先生的帮助下完成

说明

FŒ!œs2ÆmE$$Ðf⁾:(ÆmX$Ṇ? - Main link, argument a (2D-array)

F                      - Flatten
 Œ!                    - All permutations
           Ðf          - Keep elements which are truthy when
   œs2    $            -   split into 2 parts and...
      Æm $             -   the means of each...
        E              -   are the same
                     ? - Ternary if
                    Ṇ  -   Condition: No lists remain
             ⁾:(       -   If so: Set the return value to ":("
                   $   -   Otherwise: 
                Æm     -     Get the mean of each list
                  X    -     Randomly choose one (all elements are the same)

失败2 6 2,6 3 5 -> 2 6,2 6 3 5 (moved 2) -> 4。您现在只将其分成相等长度的两个部分。
凯文·克鲁伊森
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.