| Bytes | Lang | Time | Link |
|---|---|---|---|
| 074 | AWK | 241104T180930Z | xrs |
| 012 | Japt f | 171003T164103Z | Shaggy |
| 038 | Julia 0.6 | 171010T204627Z | Dennis |
| 020 | APL | 171005T181455Z | Adalynn |
| 127 | Java 8 | 171004T133851Z | Kevin Cr |
| 075 | PowerShell | 171004T222705Z | lit |
| 048 | Octave / MATLAB | 171003T212048Z | Luis Men |
| 040 | Mathematica | 171003T212103Z | JungHwan |
| 056 | JavaScript ES6 | 171003T165143Z | Neil |
| 056 | R | 171003T170934Z | Giuseppe |
| 051 | Haskell | 171004T095619Z | Laikoni |
| 016 | J | 171004T082333Z | miles |
| 060 | Python 2 | 171003T164102Z | Jonathan |
| nan | Perl 5 | 171003T220959Z | Xcali |
| 010 | Jelly | 171003T165220Z | Leaky Nu |
| 059 | JavaScript ES5 | 171003T171017Z | DanielIn |
| 060 | Python 2 | 171003T170118Z | Rod |
| 014 | 05AB1E | 171003T164055Z | Magic Oc |
| 044 | Mathematica | 171003T165602Z | Misha La |
| 017 | Jelly | 171003T165152Z | Erik the |
AWK, 74 bytes
{for(;i++<NF;)print 2*$i<=(1~i?$NF+$2:i~NF?$1+$(NF-1):$(i-1)+$(i+1))?$i:X}
{for(;i++<NF;)print # print the number if..
2*$i<=(1~i?$NF+$2: # first array element
i~NF?$1+$(NF-1): # last array element
$(i-1)+$(i+1))?$i: # others
X} # or nothing if narcissistic
Japt -f, 12 bytes
ѧWgVÉ +WgVÄ
ѧWgVÉ +WgVÄ :Implicit filter of each element at index V in array U
Ñ :Multiply by 2
§ :Is less than or equal to
WgVÉ : The element in W at index V-1
+ : Plus
WgVÄ : The element in W at index V+1
Java 8, 141 137 127 bytes
import java.util.*;a->{List r=new Stack();for(int i=0,l=a.length;i<l;)if(2*a[i]<=a[(i-1+l)%l]+a[++i%l])r.add(a[i-1]);return r;}
-10 bytes thanks to @Nevay.
Explanation:
import java.util.*; // Required import for List and Stack
a->{ // Method with integer-array parameter and List return-type
List r=new Stack(); // Return-list
for(int i=0, // Index integer, starting at 0
l=a.length; // Length of the input array
i<l;) // Loop over the input array
if(2*a[i]<= // If two times the current item is smaller or equal to:
a[(i-1+l)%l] // The previous integer in the list
+a[++i%l]) // + the next integer in the list
r.add(a[i-1]); // Add the current integer to the result-list
// End of loop (implicit / single-line body)
return r; // Return result-List
} // End of method
PowerShell, 75 bytes
for($i=0;$i-lt$a.Length;$i++){if($a[$i]-le(($a[$i-1]+$a[$i+1])/2)){$a[$i]}}
Octave / MATLAB, 48 bytes
@(x)x(conv([x(end),x,x(1)],[1,-2,1],'valid')>=0)
Explanation
The input array is first extended with the last (x(end)) and first (x(1)) entries at the appropriate sides.
The test for narcissism is done by convolving the extended array with [1, -2, 1] and keeping only the 'valid' part.
Comparing each entry in the convolution result with 0 gives a logical index (mask) which is used to select the numbers from the input.
Mathematica, 40 bytes
Pick[#,+##>=3#2&@@@Partition[#,3,1,-2]]&
JavaScript (ES6), 57 56 bytes
a=>a.filter((e,i)=>e+e<=a[(i||l)-1]+a[++i%l],l=a.length)
Edit: Saved 1 byte thanks to @g00glen00b.
R, 51 56 bytes
Thanks to user2390246 for correcting my algorithm
function(l)l[c(l[-1],l[1])+c(l[s<-sum(l|1)],l[-s])>=2*l]
indexes l where c(l[-1],l[1])+c(l[s],l[-s]), the neighbor-sums of l, are not less than twice l.
Haskell, 51 bytes
f s=[b|(a,b,c)<-zip3(last s:s)s$tail$s++s,b*2<=a+c]
Try it online! Usage example: f [1,2,3] yields [1,2].
For s = [1,2,3], last s:s is the list [3,1,2,3] and tail$s++s the list [2,3,1,2,3]. zip3 generates a list of triples (a,b,c) from three given list, truncating the longer ones to the length of he shortest list. We get [(3,1,2),(1,2,3),(2,3,1)], with b being the original list element and a and c its neighbours. The list comprehension then selects all b where b*2<=a+c, that is b is not narcissistic.
J, 16 bytes
#~+:<:1&|.+_1&|.
Explanation
#~+:<:1&|.+_1&|. Input: array A
_1&|. Rotate A right by 1
1&|. Rotate A left by 1
+ Add
+: Double each in A
<: Less than or equal to
#~ Copy the true values from A
Python 2, 64 60 bytes
- Saved four bytes thanks to xnor; golfing
l[-~j%len(l)](and a space) to(l+l)[-~j].
lambda l:[k for j,k in enumerate(l)if(l+l)[-~j]+l[~-j]>=k+k]
Jelly, 10 bytes
ṙ2+ṙ-<ḤCx@
Explanation:
ṙ2+ṙ-<ḤCx@
ṙ2 Rotate the original list two elements to the left
+ Add each element to the respective element of the original list
ṙ- Rotate the result one element to the right
<Ḥ Check if each element is less than the double of its respective element on the original list
C Subtract each 1/0 boolean from 1 (logical NOT in this case)
x@ Repeat each element of the original list as many times as the respective element of the logical NOT (i.e. keep elements of the original list where the respective element from the result is 1)
JavaScript ES5 , 59 bytes
F=a=>a.filter((x,i)=>2*x<=a[-~i%(l=a.length)]+a[(i-1+l)%l])
console.log(""+F([5, -8, -9])==""+[-8, -9])
console.log(""+F([8, 8, 8, 8])==""+[8, 8, 8, 8])
console.log(""+F([11, 6, 9, 10])==""+[6, 10])
console.log(""+F([1, 2, 0, 1, 2])==""+[1, 0, 1])
console.log(""+F([6, 9, 4, 10, 16, 18, 13])==""+[6, 4, 10])
console.log(""+F([6, -5, 3, -4, 38, 29, 82, -44, 12])==""+[-5, -4, 29, -44])
05AB1E, 22 17 15 14 bytes
vy¹®1‚N+èO;>‹—
vy # For each...
¹ # Push array.
®1‚ # Push [1,-1]
N+ # Add current index.
è # Push surrounding values of current index.
O; # Summed in half.
>‹ # A <= B?
— # If true, print current.
Mathematica, 44 bytes
Pick[#,#<=0&/@(2#-(r=RotateLeft)@#-#~r~-1)]&
How it works
Given input such as {11,6,9,10}, computes
2*{11,6,9,10} - {6,9,10,11} - {10,11,6,9}
and picks out the elements of the original input in places where this result is at most 0.