# Sum of neighbours

This should be a fairly simple challenge.

For an array of numbers, generate an array where for every element all neighbouring elements are added to itself, and return the sum of that array.

Here is the transformation which occurs on the input array [1,2,3,4,5]

[1,2,3,4,5] => [1+2, 2+1+3, 3+2+4, 4+3+5, 5+4] => [3,6,9,12,9] => 39
0          => neighbours of item 0, including item 0
[1,2]       => 1 + 2      => 3
1
[1,2,3]     => 1 + 2 + 3  => 6
2
[2,3,4]   => 2 + 3 + 4  => 9
3
[3,4,5] => 3 + 4 + 5  => 12
4
[4,5] => 4 + 5      => 9

3+6+9+12+9 => 39


### Test cases

[]            => 0 (or falsy)
[1]           => 1
[1,4]         => 10 (1+4 + 4+1)
[1,4,7]       => 28
[1,4,7,10]    => 55
[-1,-2,-3]    => -14
[0.1,0.2,0.3] => 1.4
[1,-20,300,-4000,50000,-600000,7000000] => 12338842


var QUESTION_ID=96188,OVERRIDE_USER=41257;function answersUrl(e){return"http://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(e,s){return"http://api.stackexchange.com/2.2/answers/"+s.join(";")+"/comments?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){answers.push.apply(answers,e.items),answers_hash=[],answer_ids=[],e.items.forEach(function(e){e.comments=[];var s=+e.share_link.match(/\d+/);answer_ids.push(s),answers_hash[s]=e}),e.has_more||(more_answers=!1),comment_page=1,getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){e.items.forEach(function(e){e.owner.user_id===OVERRIDE_USER&&answers_hash[e.post_id].comments.push(e)}),e.has_more?getComments():more_answers?getAnswers():process()}})}function getAuthorName(e){return e.owner.display_name}function process(){var e=[];answers.forEach(function(s){var r=s.body;s.comments.forEach(function(e){OVERRIDE_REG.test(e.body)&&(r="<h1>"+e.body.replace(OVERRIDE_REG,"")+"</h1>")});var a=r.match(SCORE_REG);a&&e.push({user:getAuthorName(s),size:+a[2],language:a[1],link:s.share_link})}),e.sort(function(e,s){var r=e.size,a=s.size;return r-a});var s={},r=1,a=null,n=1;e.forEach(function(e){e.size!=a&&(n=r),a=e.size,++r;var t=jQuery("#answer-template").html();t=t.replace("{{PLACE}}",n+".").replace("{{NAME}}",e.user).replace("{{LANGUAGE}}",e.language).replace("{{SIZE}}",e.size).replace("{{LINK}}",e.link),t=jQuery(t),jQuery("#answers").append(t);var o=e.language;/<a/.test(o)&&(o=jQuery(o).text()),s[o]=s[o]||{lang:e.language,user:e.user,size:e.size,link:e.link}});var t=[];for(var o in s)s.hasOwnProperty(o)&&t.push(s[o]);t.sort(function(e,s){return e.lang>s.lang?1:e.lang<s.lang?-1:0});for(var c=0;c<t.length;++c){var i=jQuery("#language-template").html(),o=t[c];i=i.replace("{{LANGUAGE}}",o.lang).replace("{{NAME}}",o.user).replace("{{SIZE}}",o.size).replace("{{LINK}}",o.link),i=jQuery(i),jQuery("#languages").append(i)}}var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk",answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;getAnswers();var SCORE_REG=/<h\d>\s*([^\n,]*[^\s,]),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/,OVERRIDE_REG=/^Override\s*header:\s*/i;
body{text-align:left!important}#answer-list,#language-list{padding:10px;width:290px;float:left}table thead{font-weight:700}table td{padding:5px}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"> <div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr></thead> <tbody id="answers"> </tbody> </table> </div><div id="language-list"> <h2>Winners by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody id="languages"> </tbody> </table> </div><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table>

• – Martin Ender Oct 14 '16 at 12:42
• Do we need to support floating point numbers or only integers? – corvus_192 Oct 14 '16 at 12:55
• @corvus_192 The test cases include non-integers. – Geobits Oct 14 '16 at 13:10
• @Geobits I didn't notice that, I'll edit my answer. – corvus_192 Oct 14 '16 at 13:12
• You should do this with 2 dimensional arrays next. – Bradley Uffner Oct 14 '16 at 18:23

# APL (Dyalog Unicode), 10 bytes

+/¯1↓1↓3/⊢


Try it online!

## Explanation

Use the same observation as a previous answer that the result is the sum of 3x the elements minus the first and last.

+/¯1↓1↓3/⊢ ⍝ Function train. Below explanation uses a sample input of 1 2 3
3/⊢ ⍝ Repeats (/) each element of the right hand side (⊢) three times - 1 1 1 2 2 2 3 3 3
1↓    ⍝ Drops (↓) the first item of the array -> 1 1 2 2 2 3 3 3
¯1↓      ⍝ Drops (↓) the last item (¯1) of the array -> 1 1 2 2 2 3 3
+/         ⍝ Sum over the array -> 14


# C#, 49 45 bytes

(27 if you don't count the using statement)

using System.Linq;v=>v.Sum()*3-v[0]-v.Last();

• Sum()*3 saves 4 bytes – Peter Taylor Oct 15 '16 at 7:00

# GameMaker Language, 69 bytes

a=argument_count;for(i=0;i<a;)s+=argument[i++]*(3-(i=1||i=a))return s


# Actually, 9 bytes

Golfing suggestions welcome. Try it online!

3@VdXpXΣΣ


Ungolfing

       Implicit input a.
3@V    Push all slices of a of length 1 <= n <= 3.
dXpX   Get rid of the first and last slices since they're length 1 lists.
Σ      Summing a 2D array essentially flattens into a 1D array.
Σ      Push sum(a).
Implicit return.


# Vim, 21 bytes

yGPP<C-V>GI+<Esc>DgvJD^C<C-R>=<C-R>"<CR>

Input is one number per line. Same algorithm as other entries (triplicate array, remove first and last items, sum).

# Pyth, 107 5 bytes

sPt*3


Saved 1 byte thanks to implicit input, and 1 byte thanks to the P function (replaces t_ in the previous solution)

Previous versions:

st_t*3Q


Concatenates the array 3 times, removes first and last elements, and sums it up

-*3sQ+eQhQ


The final sum is 3 times the sum of the elements - first element - last element.
That's exactly what this code does :)

Try it here!

# C++14, 64 59 bytes

As unnamed lambda

[](auto c,int&r){r=-c[0];for(auto x:c)r+=3*x;r-=c.back();}


Initializes counter with -c[0], then sums all elements times 3 and subtracts the last element. This gives 3*sum(c)-c.first-c.last which is the direct formula for this problem.

Takes any container that supports .back(). If you insinst on using plain old arrays (örglöärks) then substitute c.back() with std::end(c)[-1] for +7 bytes.

Usage:

auto n=[](auto c,int&r){r=-c[0];for(auto x:c)r+=3*x;r-=c.back();};

std::vector<int> v{1,2,3,4,5};
//or// std::array<int,5> v{1,2,3,4,5};
//or// int v[]={1,2,3,4,5}; //with std::end(c)[-1]
int r;
n(v,r);
std::cout << r << std::endl;