平滑线图


13

给定一个整数n的输入和一个正整数m 1m 2,...的列表,输出一个整数列表m 1 'm 2 ',...,其中m x '被定义为m的平均值 xnm x + n

计算这些平均值时,请忽略超出范围的索引(并相应地调整要除以的总和)。n总是≥1,但绝不能是m(舍入)的长度的一半或更多。这意味着最小长度为4中的元素将是正整数,但输出必须精确到至少3位小数。

列表的输入/输出元素可以是空格/逗号分隔的字符串,也可以是数组/列表/等。对于输入,如果您的解决方案是一个函数,则可以另外使用n的第一个参数和m x的其他参数(这也适用于命令行参数)。

这是以下内容的直观表示n=1

1 4 5 7 10
__/ | | |
L avg(1,4) = 2.5
    | | |
\___/ | |
  L avg(1,4,5) = 3.333
      | |
  \___/ |
    L avg(4,5,7) = 5.333
        |
    \___/
      L avg(5,7,10) = 7.333

      \___
        L avg(7,10) = 8.5

Final output: 2.5 3.333 5.333 7.333 8.5

由于这是,因此以字节为单位的最短代码为准。

测试用例(这些是手动完成的;如有错误,请通知我):

In                             Out
----------------------------------------------------------------------
n=1, m=12 6 3 9                9 7 6 6
n=1, m=1 4 5 7 10              2.5 3.333 5.333 7.333 8.5
n=1, m=1 3 3 7 4 2 4 2         2 2.333 4.333 4.666 4.333 3.333 2.666 3
n=2, m=1 3 5 9 10 14 15 16 23  3 4.5 5.6 8.2 10.6 12.8 15.6 17 18
n=3, m=1 1 1 1 1 1 1 1         1 1 1 1 1 1 1 1
n=3, m=1 2 3 4 5 6 7 8         2.5 3 3.5 4 5 5.5 6 6.5

清楚地讲,这与在前面加上和添加零,然后计算长度为3?的每个连续子序列的均值相同。
El'endia Starman

@ El'endiaStarman号avg(0,1,2)与相同avg(1,2)。对于“边缘情况”(ha),您不应平均输入列表中的多个元素。
门把手

啊,对,我明白了。
El'endia Starman


如果没有m_i正数的限制,这将是一个更好的挑战。
彼得·泰勒

Answers:


1

Pyth,20个字节

m.O:vzeS,0-dQh+dQUvz

测试套件

非常简单,只需将适当的部分从列表中切出,然后取平均值即可。


@ThomasKwa我尝试过,但似乎您必须存储E在地图外部,因为如果它在内部,它将继续尝试读取新值。这使得它占用相同数量的字节。
FryAmTheEggman'1

3

MATL30 28 26 24字节

2*1+:g2X53$X+1Mbgbb3$X+/

在Matlab和Octave上测试。使用语言/编译器的当前版本(9.1.0)

输入为:首先是控制窗口长度的数字,然后是format的数组[1 4 5 7 10]

编辑(2016年5月20日):在线尝试!链接中的代码已X+替换为,Y+以符合该语言的18.0.0版本。

>> matl
 > 2*1+:g2X53$X+1Mbgbb3$X+/
 >
> 1
> [1 4 5 7 10]
2.5 3.333333333333333 5.333333333333333 7.333333333333333               8.5

>> matl
 > 2*1+:g2X53$X+1Mbgbb3$X+/
 >
> 2
> [1 3 5 9 10 14 15 16 23]
3               4.5               5.6 8.199999999999999              10.6               2.8              15.6        17                18

说明

等效的Matlab代码为

n = 1; %// first input: number controlling window length
x = [1 4 5 7 10]; %// second input: array
result = conv(x,ones(1,2*n+1),'same')./conv(ones(size(x)),ones(1,2*n+1),'same');

MATL代码利用了最近添加的隐式输入和自动功能输入剪贴板的功能:

2*1+          % get implicit input "n". Multipliy by 2 and add 1
:g            % create a vector of 2*n+1 "true" values (will act as "one" values)
2X5           % 'same' string literal
3$X+          % get implicit input "x" (array). Convolution using three inputs
1M            % push all three inputs from last function
bgbb          % convert first input to "true" values. Will act as "one" values
3$X+          % convolution using three inputs
/             % divide element-wise. Implicitly print

2

CJam,31个 30字节

ri_S*l~1$++\2*)ew{S-_:+\,d/}%`

输入格式为n [m1 m2 ... mx]

运行所有测试用例。(自动将测试套件转换为所需的输入格式。)

这是通过在n空格之前和之后添加空格,然后获取所有length的子字符串2n+1,并在计算平均值之前再次删除空格来实现的。


1

朱莉娅,57字节

f(n,m)=[mean(m[max(1,i-n):min(end,i+1)])for i=1:endof(m)]

这个函数接受两个整数并返回一个浮点数数组。

这里的方法非常简单。我们通过取输入数组各部分的平均值在前面和后面截断来构造一个新数组。


0

Haskell,97 95字节

import Data.List
n#x|t<-2*n+1=[sum a/sum(1<$a)|a<-take t<$>take t(inits x)++tails x,length a>n]

用法示例:2 # [1,3,5,9,10,14,15,16,23]-> [3.0,4.5,5.6,8.2,10.6,12.8,15.6,17.0,18.0]

怎么运行的:

t<-2*n+1                      -- assign t to the maximum number of elements of a
                              -- of sublist
     take t(inits x)          -- build the sublists from t elements of the inits
                ++tails x     -- of the input and the tails of the input,
                              -- e.g. x=[1,2,3,4], t=3:
                              -- [[],[1],[1,2]] ++ [[1,2,3,4],[2,3,4],[3,4],[4],[]]
  a<-take t<$>                -- take at most t elements from every sublist
                ,length a>n   -- keep those with a minimum length of n+1
sum a/sum(1<$a)               -- calculate average, "sum(1<$a)" is the length of a

0

Pyth,22个字节

.OMsM.:++J*]YKE]MQJhyK

说明:

.OM sM .: +               Means of flattens of sublists of length 2K+1 of
            + J *         
                  ] Y     J is E copies of [].
                  K E     Save E to a variable to use it later.
               ]MQ        the input
            J             Put J on both sides of ]MQ.
          h y K           2K+1

在这里尝试。


0

JavaScript(ES6),104

运行总/运行样本大小。在Javascript中,读取数组边界之外的值会产生undefined,可以使用~~将其转换为0。

(l,n,p=0)=>l.map((v,i)=>(p+=~l[i-n-1]-~l[i+n])/(n+1+Math.min(n,l.length-1-i,i)),l.map((v,i)=>p+=i<n&&v))

不打高尔夫球

(l, n) => {
    p = 0;
    for (i = 0; i < n; i++) p += v;
    r = [];
    for (i = 0; i < l.length; i++) {
        p += (l[i + n] || 0) - (i > n ? l[i - n - 1] : 0);
        r.push(p / Math.min(n, l.length - i - 1, i);
    }
    return r;
}

测试

f=(n,l,p=0)=>l.map((v,i)=>(p+=~l[i-n-1]-~l[i+n])/(n+1+Math.min(n,l.length-1-i,i)),l.map((v,i)=>p+=i<n&&v))

console.log=x=>O.textContent+=x+'\n';


;[
  [1,[12,6,3,9],[9,7,6,6]]
, [1,[1,4,5,7,10],[2.5,3.333,5.333,7.333,8.5]]
, [1,[1,3,3,7,4,2,4,2],[2,2.333,4.333,4.667,4.333,3.333,2.667,3]]
, [2,[1,3,5,9,10,14,15,16,23],[3,4.5,5.6,8.2,10.6,12.8,15.6,17,18]]
, [3,[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1]]
, [3,[1,2,3,4,5,6,7,8],[2.5,3,3.5,4,5,5.5,6,6.5]]
].forEach(t=>{
  var n=t[0],l=t[1],x=t[2],r=f(n,l)
  // verify (limited to 3 decimals)
  var ok = r.every((v,i)=>v.toFixed(3)==x[i].toFixed(3))
  
  console.log((ok?'OK   ':'Fail ')+n+' '+l+' -> '+r+' ('+x+')')
})
<pre id=O></pre>


0

JavaScript(ES6),82个字节

码:

F=(n,a)=>a.map((e,i)=>(s=a.slice(i<n?0:i-n,i+n+1))&&s.reduce((l,c)=>l+c)/s.length)

测试:

F=(n,a)=>a.map((e,i)=>(s=a.slice(i<n?0:i-n,i+n+1))&&s.reduce((l,c)=>l+c)/s.length)

document.write('<pre>' +
  [
    [1, [12, 6, 3, 9], [9, 7, 6, 6] ],
    [1, [1, 4, 5, 7, 10], [2.5, 3.333, 5.333, 7.333, 8.5] ],
    [1, [1, 3, 3, 7, 4, 2, 4, 2], [2, 2.333, 4.333, 4.667, 4.333, 3.333, 2.667, 3] ],
    [2, [1, 3, 5, 9, 10, 14, 15, 16, 23], [3, 4.5, 5.6, 8.2, 10.6, 12.8, 15.6, 17, 18] ],
    [3, [1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1] ],
    [3, [1, 2, 3, 4, 5, 6, 7, 8], [2.5, 3, 3.5, 4, 5, 5.5, 6, 6.5] ]
  ].map(t => {
    var [n, m, e] = t;
    var r = F(n, m);
    // verify to precision of 3 decimals
    var test = r.every((v, i) => v.toPrecision(3) === e[i].toPrecision(3));

    return 'F(' + n + ', [' + m + '])\t' + (test ? 'Pass' : 'Fail') +
      '\n\t{' + r + '} ' + (test ? '=' : '≠') + ' {' + e + '}';
  }).join('\n\n') +
  '</pre>');

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.