15
\$\begingroup\$

Definition

Narcissistic 1 integers of an array think they are better than their neighbours, because they are strictly higher than their arithmetic mean.

Neighbours are defined as follows:

  • If the integer is at index 0 (the first), then its neighbours are the last and the second elements of the list.

  • If the integer is not the first nor the last, then its neighbours are the two immediately adjacent elements.

  • If the integer is at index -1 (the last), then its neighbours are the second-last and the first elements of the list.


Task

Given an array of integers, your task is to discard the narcissistic ones.

  • The integers can be positive, negative or zero.

  • You may assume that the array contains at least three elements.

  • All standard rules apply. This is , so the shortest code in bytes wins.

Examples

Consider the array [6, 9, 4, 10, 16, 18, 13]. Then we can built the following table:

Element | Neighbours | Neighbours' Mean | Is Narcissistic?
--------+------------+------------------+-----------------
6       | 13, 9      | 11               | False.
9       | 6, 4       | 5                | True.
4       | 9, 10      | 9.5              | False.
10      | 4, 16      | 10               | False.
16      | 10, 18     | 14               | True.
18      | 16, 13     | 14.5             | True.
13      | 18, 6      | 12               | True.

By filtering the Narcissistic ones out, we are left with [6, 4, 10]. And that's it!

Test Cases

Input -> Output

[5, -8, -9]                         -> [-8, -9]
[8, 8, 8, 8]                        -> [8, 8, 8, 8]
[11, 6, 9, 10]                      -> [6, 10]
[1, 2, 0, 1, 2]                     -> [1, 0, 1]
[6, 9, 4, 10, 16, 18, 13]           -> [6, 4, 10]
[6, -5, 3, -4, 38, 29, 82, -44, 12] -> [-5, -4, 29, -44]

1 - Narcissist does not mean mathematically Narcissistic.

\$\endgroup\$
0

19 Answers 19

7
\$\begingroup\$

Jelly, 10 bytes

ṙ2+ṙ-<ḤCx@

Try it online!

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)
\$\endgroup\$
0
6
\$\begingroup\$

Python 2, 60 bytes

lambda x:[b for a,b,c in zip(x[-1:]+x,x,x[1:]+x)if b*2<=a+c]

Try it online!

\$\endgroup\$
6
\$\begingroup\$

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.

\$\endgroup\$
0
5
\$\begingroup\$

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.

\$\endgroup\$
5
\$\begingroup\$

05AB1E, 22 17 15 14 bytes

vy¹®1‚N+èO;>‹—

Try it online!

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.
\$\endgroup\$
1
4
\$\begingroup\$

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.

\$\endgroup\$
4
\$\begingroup\$

Octave / MATLAB, 48 bytes

@(x)x(conv([x(end),x,x(1)],[1,-2,1],'valid')>=0)

Try it online!

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.

\$\endgroup\$
2
\$\begingroup\$

J, 16 bytes

#~+:<:1&|.+_1&|.

Try it online!

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
\$\endgroup\$
2
\$\begingroup\$

Japt, 17 16 15 bytes

kÈ>½*[Y°ÉY]x!gU

Try it


Explanation

Implicit input of array U.

kÈ>

Remove (k) the elements that return true when passed through a function, with Y being the current index, that check if the current element is greater then ...

[Y°ÉY]

The array [Y-1, Y+1] ...

x!gU

Reduced by addition (x) after indexing each element into U ...

½*

Multiplied by .5.


Alternative, 15 bytes

fÈ+X§UgYÉ +UgYÄ

Try it

\$\endgroup\$
0
2
\$\begingroup\$

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]

Try it online!

indexes l where c(l[-1],l[1])+c(l[s],l[-s]), the neighbor-sums of l, are not less than twice l.

\$\endgroup\$
0
2
\$\begingroup\$

Mathematica, 40 bytes

Pick[#,+##>=3#2&@@@Partition[#,3,1,-2]]&
\$\endgroup\$
3
  • \$\begingroup\$ I think you need <= instead of <. \$\endgroup\$ Commented Oct 4, 2017 at 13:46
  • \$\begingroup\$ Actually no, you'll need >=. \$\endgroup\$ Commented Oct 4, 2017 at 13:47
  • \$\begingroup\$ @MartinEnder Ah, you're right. I have to Pick non-narcissistic numbers. \$\endgroup\$ Commented Oct 4, 2017 at 18:42
1
\$\begingroup\$

Jelly, 17 bytes

Ḣ+ṪH<ḢṆ
.ịj⁸Ç3ƤTị

Try it online!

\$\endgroup\$
1
\$\begingroup\$

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]

Try it online!

\$\endgroup\$
0
1
\$\begingroup\$

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:

Try it here.

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
\$\endgroup\$
0
1
\$\begingroup\$

Julia 0.6, 38 bytes

!x=x[x[[e=end;1:e-1]]+x[[2:e;1]].>=2x]

Try it online!

\$\endgroup\$
0
\$\begingroup\$

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])

\$\endgroup\$
0
\$\begingroup\$

Perl 5, 51 + 1 (-a) = 52 bytes

$p=$F[-1];say map{$p+$F[++$i%@F]<2*($p=$_)?'':$_}@F

Try it online!

\$\endgroup\$
0
\$\begingroup\$

PowerShell, 75 bytes

for($i=0;$i-lt$a.Length;$i++){if($a[$i]-le(($a[$i-1]+$a[$i+1])/2)){$a[$i]}}
\$\endgroup\$
0
\$\begingroup\$

APL, 20 bytes

{⍵/⍨⍵≤2÷⍨(1⌽⍵)+¯1⌽⍵}

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.