过滤掉伪元素!


15

我们定义一个数组/(数字列表)的超平均数为其前缀之和的算术平均值。

例如,[1, 4, -3, 10]以以下方式计算列表的超平均:

  • 我们得到前缀:[1], [1, 4], [1, 4, -3], [1, 4, -3, 10]

  • 总计:[1, 5, 2, 12]

  • 现在获取此列表中元素的算术平均值:(1 + 5 + 2 + 12) / 4 = 5

伪元件的阵列的是一种元素,其值是严格比其超平均水平。因此,我们的例子名单的伪元素14-3


给定一个浮点数列表,您的任务是返回伪元素列表。

  • 您不必担心浮点数错误。

  • 输入列表永远不会为空,并且可能包含整数和浮点数。如果提及,则整数可以视为浮点数(带有<integer>.0

  • 您可能会认为数字适合您选择的语言,但请不要以任何方式滥用它。

  • (可选)您也可以将数组的长度作为输入。

  • 这是,因此适用标记的标准规则。以字节为单位的最短代码(每种语言)获胜!


测试用例

输入->输出

[10.3]-> []
[5.4,5.9]-> [5.4,5.9]
[1,4,-3,10]-> [1,4,-3]
[-300,-20.9,1000]-> [-300,-20.9]
[3.3、3.3、3.3、3.3]-> [3.3、3.3、3.3、3.3]
[-289.93,912.3,-819.39,1000]-> [-289.93,-819.39]

如果允许某些语言将数组的长度作为附加输入,则应允许所有语言使用
ngenisis

1
@ngenisis适用于所有语言。如果采用同样的长度会缩短您的程序,请随时执行。该规范完全不受语言限制。
Xcoder先生17年

Answers:


7

MATL,8字节

ttYsYm<)

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

说明

tt    % Implicitly input array. Duplicate twice
Ys    % Cumulative sum
Ym    % Arithmetic mean
<     % Less than? (element-wise). Gives an array containing true / false
)     % Reference indexing : use that array as a mask to select entries 
      % from the input. Implicitly display

7

05AB1E9 8字节

-1个字节归功于Magic Octopus Urn

ηOO¹g/‹Ï

在线尝试!

η        # Get prefixes
 O       # Sum each
  O¹g/   # Get the mean ( length(prefix list) equals length(original list) )
      ‹Ï # Keep only the value that are less than the mean

05AB1E,6个字节

使用新ÅA命令。

ηOÅA‹Ï

在线尝试!

η      # Get prefixes
 O     # Sum each
  ÅA   # Get the mean
    ‹Ï #  Keep only the value that are less than the mean

2
ηOO¹g/›Ï为8; 也是从开头nOO!
魔术章鱼缸

5

Japt v2.0a0,12个 11 10字节

f<Uå+ x÷Ul

测试一下

  • 由于ETH指出了冗余字符,因此节省了1个字节。

说明

数组的隐式输入U

f<

f通过检查每个元素是否小于...来过滤()数组

Uå+

Uå通过累加... 累计减少()

x

通过求和依次减少结果数组...

/Ul

并除以的长度(lU

隐式输出结果数组。



3

果冻,9字节

+\S÷L<Ðf@

在线尝试!


也许<Ðf@应该改为<Ðḟ@
暴民埃里克(Erik the Outgolfer)'17年

@EriktheOutgolfer,但它通过了所有测试用例。
Leaky Nun

仍然有些东西对我来说似乎不好...首先+\S÷L计算超平均,然后<Ðf@将其作为正确的参数,如果元素是伪元素,<则将返回1,本质上是过滤伪元素而不是过滤他们出来。
暴民埃里克(Erik the Outgolfer)'17年

@EriktheOutgolfer在这种情况下,过滤出意味着进行过滤。
Leaky Nun

3

Python 2中78 76 71 66字节

-7个字节感谢Xcoder先生。

lambda l:[x for x in l if x<sum(sum(l[:i])for i in range(len(l)))]

在线尝试!


我认为你可以做range(len(l)),并l[:i+1]为-2字节(未测试)
Xcoder先生

打高尔夫球和混淆。;) 谢谢!
完全人类

您的解决方案虽然无效。更改x>sum(...)x<sum(...)它是有效的,还是76个字节
Xcoder先生

鞭子...固定。>。>
totallyhuman人类


3

Haskell,39个字节

f l=filter(<sum(scanl1(+)l)/sum(1<$l))l

在线尝试!

不幸的length是,Int它是类型,所以我不能将其与浮点除法/一起使用,而必须使用一种解决方法:sum(1<$l)


3

外壳10 9字节

感谢@Zgarb打高尔夫球1个字节!

f</L⁰Σ∫⁰⁰

在线尝试!

取消/解释

           -- argument is ⁰ (list) 
f       ⁰  -- filter the original list with
 <         --   element strictly smaller than
     Σ∫⁰   --   sum of all prefixes
  /L⁰      --   averaged out

2
f</L⁰Σ∫⁰⁰是9个字节,但是三个lambda参数感觉很笨拙。
Zgarb

3

JavaScript(ES6),56 55 52字节

a=>a.filter(x=>x<t/a.length,a.map(x=>t+=s+=x,s=t=0))

测试一下

o.innerText=(f=

a=>a.filter(x=>x<t/a.length,a.map(x=>t+=s+=x,s=t=0))

)(i.value=[1,4,-3,10]);oninput=_=>o.innerText=f(i.value.split`,`.map(eval))
<input id=i><pre id=o>


3

Java 8,81字节

此lambda表达式接受a List<Float>并将其突变。输入列表的迭代器必须支持删除(ArrayList例如,的支持)。分配给Consumer<List<Float>>

a->{float l=0,t=0,u;for(float n:a)t+=n*(a.size()-l++);u=t/l;a.removeIf(n->n>=u);}

非高尔夫λ

a -> {
    float l = 0, t = 0, u;
    for (float n : a)
        t += n * (a.size() - l++);
    u = t / l;
    a.removeIf(n -> n >= u);
}

在线试用

致谢

  • -3个字节,感谢Kevin Cruijssen
  • -17个字节感谢Nevay

1
您可以删除t/=l;并更改if(n<t)为,以节省3个字节if(n<t/l)
凯文·克鲁伊森

1
您可以使用列表而不是数组来修改提供的参数,而不是打印结果值a->{float l=0,t=0,u;for(float n:a)t+=n*(a.size()-l++);u=t/l;a.removeIf(n->n>=u);}(81字节)。
涅瓦



2

Python 3,76个字节

lambda x:[w for w in x if w<sum(u*v+v for u,v in enumerate(x[::-1]))/len(x)]

输入和输出是数字列表。在线尝试!

这也适用于Python 2(print在页脚中明显替换了语法)。


您需要颠倒清单吗?
Officialaimm

@officialaimm我认为是这样,因为枚举值1,2,3,...必须与x [0],x [-1],x [-2]一起使用。但是在所有情况下,结果似乎都是一样的,嗯……
Luis Mendo

1
我发现了一个反例,它表明倒车确实是必要的
Luis Mendo

啊,没关系..我只是这么想,因为它通过了所有测试用例...:P
Officialaimm





1

PHP, 84 bytes

for($i=--$argc;$i;)$s+=$i--/$argc*$r[]=$argv[++$k];foreach($r as$x)$x<$s&&print$x._;

takes input from command line arguments. Run with -nr or try it online.


summing up the partial lists is the same as summing up each element multiplied with the number of following elements +1 → no need to juggle with bulky array functions. It´s still long, though.



1

J, 15 bytes

#~[<[:(+/%#)+/\

Try it online! Expects a J-style array (negatives represented using _ instead of - and elements separated by spaces -- see the TIO link for examples).

I don't know if there's a way to remove the parentheses around the mean (+/%#) but removing that and the cap would be the first thing I'd try to do to golf this further.

Explanation

Sometimes J reads like (obfuscated) English.

#~ [ < [: (+/ % #) +/\
                   +/\  Sum prefixes
                     \   Get prefixes
                   +/    Sum each
          (+/ % #)      Mean
           +/            Sum of array
              %          Divided by
                #        Length of array
   [ <                  Input array is less than?
                         (gives boolean array of pairwise comparisons)
#~                      Filter by

1
you beat me to it by 3 mins :)
Jonah

12 bytes with #~]<1#.+/\%#
miles

@miles Unless you think it's similar enough, I think your comment might warrant its own answer. EDIT: I think it's very clever myself.
cole


1

Mathematica, 35 bytes

Cases[#,x_/;x<#.Range[#2,1,-1]/#2]&

Function which expects a list of numbers as the first argument # and the length of the list as the second argument #2. #.Range[#2,1,-1]/#2 takes the dot product of the input list # and the the list Range[#2,1,-1] == {#2,#2-1,...,1}, then divides by the length #2. Then we return the Cases x_ in the input list # which are less than the hyper-average.

Without the length as a second argument, we need 6 more bytes:

Cases[#,x_/;x<#.Range[h=Tr[1^#],1,-1]/h]&

0

K (oK), 26 bytes

Solution:

x@&x<(+/+/'x@!:'1+!#x)%#x:

Try it online!

Examples:

> x@&x<(+/+/'x@!:'1+!#x)%#x:1 4 -3 10
1 4 -3
> x@&x<(+/+/'x@!:'1+!#x)%#x:-289.93 912.3 -819.39 1000
-289.93 -819.39

Explanation:

Interpretted right-to-left. Struggled with a short way to extract prefixes:

x@&x<(+/+/'x@!:'1+!#x)%#x: / the solution
                        x: / store input in x, x:1 4 -3 10
                       #   / count, return length of x, #1 4 -3 10 => 4
     (               )     / do everything in the brackets together
                   #x      / count x
                  !        / til, range 0..x, !4 => 0 1 2 3
                1+         / add 1 vectorised, 1+0 1 2 3 => 1 2 3 4
             !:'           / til each, e.g. !1, !2, !3, !4
           x@              / index into x at these indices (now we have the prefixes)
        +/'                / sum (+ over) each, e.g. 1 5 2 12
      +/                   / sum over, e.g. 20
                      %    / right divided by left, 20%4 => 5 (now we have the hyper average)
   x<                      / boolean list where x less than 5
  &                        / indices where true, &0111b => 1 2 3
x@                         / index into x at these indices (now we have the filtered list)

Notes:

Alternative version taking length of input as parameter (25 byte solution):

> {x@&x<(+/+/'x@!:'1+!y)%y}[1 4 -3 10;4]
1 4 -3

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.