39
\$\begingroup\$

Given a list of positive integers determine if there is an element that is either greater than its two neighbors or less than its two neighbors (a "bump"). To be clear a bump can never be the first or last item of the list because they only have one neighbor.

Your program should output one of two consistent values each corresponding to either a list with no bumps or a list with bumps. What the values are is unimportant you may choose them yourself.

This is so answers will be scored in bytes with fewer bytes being better.

Test cases

[] -> False
[1] -> False
[1,2] -> False
[1,2,1] -> True
[1,2,2] -> False
[1,2,3] -> False
[1,2,2,1] -> False
[1,2,2,3] -> False
[1,2,1,2] -> True
[1,3,2] -> True
[3,1,2] -> True
[2,2,2] -> False
\$\endgroup\$
  • 5
    \$\begingroup\$ Test Case Request: Numbers other than 0-1-2-3, also negatives allowed/disallowed? \$\endgroup\$ – Magic Octopus Urn Feb 14 '18 at 22:47
  • \$\begingroup\$ Suggested test case: [1,3,3] (ensures that answers using Dennis's algorithm take the sign of the increments rather than just using the increments themselves) \$\endgroup\$ – ETHproductions Feb 15 '18 at 2:39
  • 1
    \$\begingroup\$ @ETHproductions Isn't that already covered by [1,2,2]? Or am I missing something? \$\endgroup\$ – Fund Monica's Lawsuit Feb 15 '18 at 8:13
  • 2
    \$\begingroup\$ @NicHartley, the deltas of [1,2,2] are the same as the signs of those deltas but that's not the case with [1,3,3]. \$\endgroup\$ – Shaggy Feb 15 '18 at 9:54

40 Answers 40

1
2
1
\$\begingroup\$

APL (Dyalog), 15 19 20 bytes

2 bytes saved thanks to @EriktheOutgolfer

{2>≢⍵:0⋄2∊|2-/×2-/⍵}

Try it online!

1 for bump, 0 for no bump.

\$\endgroup\$
  • \$\begingroup\$ Unfortunately, this gives an error for , while it should return 0 instead (or throw an error for all falsy inputs). \$\endgroup\$ – Erik the Outgolfer Feb 14 '18 at 23:07
  • \$\begingroup\$ Oh, and, if you fix the issue, 0∊2> can be 2∊ instead for -2 bytes. \$\endgroup\$ – Erik the Outgolfer Feb 14 '18 at 23:21
  • \$\begingroup\$ @EriktheOutgolfer temporary quick fix, thanks \$\endgroup\$ – Uriel Feb 14 '18 at 23:45
  • \$\begingroup\$ Still fails for singleton lists \$\endgroup\$ – H.PWiz Feb 14 '18 at 23:48
  • \$\begingroup\$ @H.PWiz A possible fix is to replace ⍬≡ with 2>≢ for just 1 extra byte. \$\endgroup\$ – Erik the Outgolfer Feb 14 '18 at 23:51
1
\$\begingroup\$

Windows Batch, 126 120 114 bytes

I was pretty excited about my first use of the shift command.

This script generally works for numbers within 9 digits(no matter positive or negative.)

:L
@if %3.==. exit/b
@if %2 gtr %1 if %3 lss %2 goto:e
@if %2 lss %1 if %3 gtr %2 goto:e
@shift
@goto:L
:e
@echo T
\$\endgroup\$
  • 1
    \$\begingroup\$ An empty response might count as a consistent value, otherwise I think you might be able to save a couple of bytes by moving the echo T to the end and jumping to it instead of repeating it. I'm also wondering whether you can save bytes by moving the @shift to the beginning of the loop but I'm not so sure whether that helps. \$\endgroup\$ – Neil Feb 15 '18 at 0:57
  • \$\begingroup\$ @Neil Thanks for the if-golf. I don't see any reason moving @shift forward will help save bytes. I will take a look at the possible shift movement anyways. \$\endgroup\$ – stevefestl Feb 15 '18 at 1:01
1
\$\begingroup\$

C (gcc), 78 bytes

Variant: loop

r,i;f(a,n)int*a;{for(r=n>2,i=0;i<n-2;)r=(a[i++]-a[i])*(a[i]-a[i+1])<0&&r;i=r;}

Try it online!

Variant: recursive

f(a,n)int*a;{n=(n<3)?0:((a[--n]-a[--n])*(a[n]-a[n-1])<0&&((n<2)?1:f(a,n+1)));}

Try it online!

The criteria is that a[i]-a[i+1] and a[i+1]-a[i+2] are non-zero and have opposite sign. i=r is just a way to return the value (described here).

P. S. Additional two test cases may reveal bugs: { 1, 2, 1, 2, 2 } and { 2, 2, 1, 2, 1 }

\$\endgroup\$
  • \$\begingroup\$ Welcome to PPCG! \$\endgroup\$ – Steadybox Mar 3 '18 at 8:11
  • 1
    \$\begingroup\$ By the way, I think a[--n]-a[--n] has undefined behaviour and a[i++]-a[i] has unspecified behaviour. Doesn't matter since they seem to work with gcc in this case. \$\endgroup\$ – Steadybox Mar 3 '18 at 8:15
1
\$\begingroup\$

APL (Dyalog Classic), 15 bytes

0∨.>2×/2-/⊃,⊃,⊢

Try it online!

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

C (gcc), 101 97 bytes

r,i;f(a,n)int*a;{for(i=n|1;--i;a[i]-=a[i-1]);for(r=0;--n>0;r=a[n]<0&a[n-1]>0|a[n]>0&a[n-1]<0|r);}

Try it online!

Differences the list, then looks for two neighboring differences that are not zero and have opposite sign.

\$\endgroup\$
  • \$\begingroup\$ Welcome to PPCG! \$\endgroup\$ – Steadybox Feb 15 '18 at 17:03
0
\$\begingroup\$

Retina 0.8.2, 43 bytes

.+
$*
1`(1+)1¶\1¶1\1|¶(1+)(?<!\2¶\2)¶(?!\2)

Try it online! Takes input on separate lines and outputs 0 or 1. Explanation:

.+
$*

Convert to unary.

1`            |

Count at most one matching regex.

  (1+)1¶\1¶1\1

Match a number that is surrounded by greater numbers on both sides.

               ¶(1+)(?<!\2¶\2)¶(?!\2)

Match a number that is not surrounded by greater or equal numbers on either side, i.e. both are less.

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

Python, 91 bytes

def f(a):return[i for i,j in enumerate(a[1:-1])if(j>max(a[i+2],a[i])or j<min(a[i+2],a[i]))]

An empty array is a falsey value, while a populated array is truthy

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

GFORTH 71 Bytes

: B ROT 2DUP = -1 = IF DROP ROT = -1 = IF  ELSE ." TRUE " THEN THEN ;

Output:

1 2 1 B TRUE  ok
19 4 19 B TRUE  ok
5 4 6 B  ok
\$\endgroup\$
0
\$\begingroup\$

Elm, 102 bytes

f a b=case b of
 c::d::e->a c d::f a(d::e)
 _->[]
a g=case g of
 []->1<0
 i::k->i<0||a k
a<<f(*)<<f(-)

Explanation

This works very similar to my Haskell answer. Except Elm is missing all of the functions that do stuff so I had to make every thing from the ground up. You can test it by running the following here

import Html exposing (text)
f a b=case b of
 c::d::e->a c d::f a(d::e)
 _->[]
a g=case g of
 []->1<0
 i::k->i<0||a k
g=a<<f(*)<<f(-)
s x=case x of
 True->"True"
 False->"False"
main=g[1,1,1,1,1,0,1]|>s|>text
\$\endgroup\$
0
\$\begingroup\$

Perl 6, 50 47 bytes

{?grep {0>(.[1]-.[2])*[-] .[^2]},.rotor(3=>-2)}

Try it online!

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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