Is there a bump?

39

2

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

Post Rock Garf Hunter

Posted 2018-02-14T22:31:11.793

Reputation: 55 382

5Test Case Request: Numbers other than 0-1-2-3, also negatives allowed/disallowed? – Magic Octopus Urn – 2018-02-14T22:47:52.473

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) – ETHproductions – 2018-02-15T02:39:54.027

1@ETHproductions Isn't that already covered by [1,2,2]? Or am I missing something? – Fund Monica's Lawsuit – 2018-02-15T08:13:42.470

2@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]. – Shaggy – 2018-02-15T09:54:28.790

Answers

15

Jelly, 5 bytes

IṠIỊẠ

Returns 0 if there's a bump, 1 if not.

Try it online!

How it works

IṠIỊẠ  Main link. Argument: A (integer array)

I      Increments; take all forward differences of A.
 Ṡ     Take the signs.
       The signs indicate whether the array is increasing (1), decreasing (-1), or
       constant at the corresponding point. A 1 followed by a -1 indicates a local
       maximum, a -1 followed by a 1 a local minimum.
  I    Increments; take the forward differences again.
       Note that 1 - (-1) = 2 and (-1) - 1 = -2. All other seven combinations of
       signs map to -1, 0, or 1.
   Ị   Insignificant; map each difference d to (-1 ≤ d ≤ 1).
    Ạ  All; return 1 if all differences are insignificant, 0 if not.

Dennis

Posted 2018-02-14T22:31:11.793

Reputation: 196 637

1What is "Increments?". What is being incremented and what does that do? – Post Rock Garf Hunter – 2018-02-15T07:40:18.707

1@WheatWizard I think that this is the equivalent of 05AB1E's deltas (¥) command: an array [n0, n1, n2, n3] is poped and the array [n1-n0, n2-n1, n3-n2] is pushed. – Kaldo – 2018-02-15T08:49:13.743

10

JavaScript (ES6), 38 bytes

Returns a boolean.

a=>a.some(x=n=>x*(x=a<n|-(a>(a=n)))<0)

Test cases

let f =

a=>a.some(x=n=>x*(x=a<n|-(a>(a=n)))<0)

console.log(f([]))              // False
console.log(f([1]))             // False
console.log(f([1,2]))           // False
console.log(f([1,2,1]))         // True
console.log(f([1,2,2]))         // False
console.log(f([1,2,3]))         // False
console.log(f([1,2,2,1]))       // False
console.log(f([1,2,2,3]))       // False
console.log(f([1,2,1,2]))       // True
console.log(f([1,3,2]))         // True
console.log(f([2,0,1]))         // True
console.log(f([2,2,2]))         // False
console.log(f([-4,100000,89]))  // True

How?

We use a to store the previous value of n. We set x to 1 if a < n, -1 if a > n or 0 if a = n. And we test whether old_x * x < 0, which is only possible if (old_x = 1 and x = -1) or (old_x = -1 and x = 1).

Because x is initialized to the anonymous callback function of some(), it is coerced to NaN during the first iteration, which makes the test falsy.

Arnauld

Posted 2018-02-14T22:31:11.793

Reputation: 111 334

This will throw in strict mode. – Aluan Haddad – 2018-02-15T08:37:44.037

2@AluanHaddad Well, 99% of JS golfed code will throw in strict mode just because of undeclared variables. PPCG and codereview doesn't mix well. :P – Arnauld – 2018-02-15T08:51:57.127

That's fair, I'm not really much for golf. – Aluan Haddad – 2018-02-15T08:55:16.117

4Then why comment on it lol – Mark C. – 2018-02-16T13:11:31.960

8

Haskell, 42 bytes

any(<0).f(*).f(-)
f a b=zipWith a b$tail b

Try it online!

Explanation

First we have the function f that takes a binary function and a list and applies the binary function to every adjacent pair in the list.

Then our main function applies f(-) to the input list. This calculates the difference list. We then apply f(*) to the list to multiply every adjacent pair. Lastly we ask if any pair is less than zero.

A number in the end list can only be negative if it is the product of a negative and positive number from the difference list. Thus in order to produce a negative entry (and then return true) the original list must switch from increasing to decreasing or vice versa, that is it must have a bump.

Post Rock Garf Hunter

Posted 2018-02-14T22:31:11.793

Reputation: 55 382

Nice way to deal with the empty list! – Laikoni – 2018-02-15T10:05:31.673

6

Python 2, 43 bytes

f=lambda x,y,z,*t:0>(y-x)*(z-y)or f(y,z,*t)

Returns True if there's a bump, errors if there isn't. (allowed by default)

Try it online!

Dennis

Posted 2018-02-14T22:31:11.793

Reputation: 196 637

5

Octave with Image Package, 34 32 bytes

2 bytes saved thanks to @StewieGriffin!

@(x)0||prod(im2col(diff(x),2))<0

Try it online!

Explanation

Computes consecutive differences, arranges them in sliding blocks of length 2, obtains the product of each block, and tests if any such product is negative.

Luis Mendo

Posted 2018-02-14T22:31:11.793

Reputation: 87 464

0||prod(...) saves 2 bytes. You could also skip the entire any part, and use the default truthy/falsy definition to save 5 bytes. – Stewie Griffin – 2018-02-15T08:15:26.107

Darn, saving 5 bytes will make your solution shorter than mine :( Nice use of the image package. I didn't know it was on TIO. – Stewie Griffin – 2018-02-15T08:31:54.780

1@StewieGriffin Since the challenge requires two consistent values I cannot remove any. Thanks for the 0|| idea! – Luis Mendo – 2018-02-15T12:14:14.200

4

Haskell, 33 bytes

f(p:r@(c:n:_))=(c-p)*(c-n)>0||f r

Try it online!

True if there's a bump, errors if there's not.

totallyhuman

Posted 2018-02-14T22:31:11.793

Reputation: 15 378

l`zip3`tail l$drop 2l is just a hair shorter. I wonder if pattern matching is somehow even shorter? – Lynn – 2018-02-14T22:55:23.497

4

R, 48 bytes

function(x)any(apply(embed(diff(x),2),1,prod)<0)

Try it online!

How it works step-by-step using c(1,4,1,4) as example:

> x=c(1,4,1,4)
> diff(x)
[1]  3 -3  3
> embed(diff(x),2)
     [,1] [,2]
[1,]   -3    3
[2,]    3   -3
> apply(embed(diff(x),2),1,prod)
[1] -9 -9
> any(apply(embed(diff(x),2),1,prod)<0)
[1] TRUE

As a bonus, here is a solution of similar length and concept using package zoo:

function(x)any(zoo::rollapply(diff(x),2,prod)<0)

plannapus

Posted 2018-02-14T22:31:11.793

Reputation: 8 610

1neat! Note to self: remember embed exists. It's too bad that rowProds and colProds doesn't exist as aliases in R. – Giuseppe – 2018-02-15T09:53:42.053

1@Giuseppe out of desperation i actually checked if they existed :) but indeed just rowSums and rowMeans... – plannapus – 2018-02-15T09:55:07.637

1well, at least looking through the docs, .colSums will reshape the input into a matrix based on additional inputs which probably has a golfing application somewhere....now I just have to find one! – Giuseppe – 2018-02-15T09:58:12.367

@Guiseppe: Have a look at the functions in matrixStats package. – Michael M – 2018-02-16T08:58:43.907

@MichaelM Unfortunately because of the length of the package name it doesn't make it competitive (57 bytes: function(x)any(matrixStats::colProds(embed(diff(x),2)))<0). But for anything other than code golf, this package is indeed a treasure trove. – plannapus – 2018-02-16T09:09:28.220

@plannapus: My comment was purely unrelated of playing golf - my bad. – Michael M – 2018-02-16T09:15:58.870

3

Perl 6, 39 bytes

{so~(.[1..*]Zcmp$_)~~/'re L'|'s M'/}

Try it online!

$_ is the list argument to this anonymous function. .[1..*] is the same list, but with the first element dropped. Zcmp zips the two lists together with the cmp operator, resulting in a list of Order values. For example, for an input list 1, 2, 2, 2, 1 this would result in the list More, Same, Same, Less.

Now we just need to know whether that list contains two adjacent elements More, Less or Less, More. The trick I used is to convert the list to a space-delimited string with ~, then test whether it contains either substring re L or s M. (The first one can't be just e L because Same also ends with an "e".)

The smart match operator returns either a Match object (if the match succeeded) or Nil (if it didn't), so so converts whatever it is into a boolean value.

Sean

Posted 2018-02-14T22:31:11.793

Reputation: 4 136

3

Ruby, 55 46 bytes

->a{a.each_cons(3).any?{|x,y,z|(y-x)*(y-z)>0}}

Try it online!

A lambda accepting an array and returning boolean.

-9 bytes: Replace (x<y&&y>z)||(x>y&&y<z) with (y-x)*(y-z)>0 (thanks to GolfWolf)

->a{
  a.each_cons(3)              # Take each consecutive triplet
    .any?{ |x,y,z|            # Destructure to x, y, z
      (y-x)*(y-z) > 0         # Check if y is a bump
    }
}

benj2240

Posted 2018-02-14T22:31:11.793

Reputation: 801

1I think you can use | instead of ||, saving you 1 byte. – Yytsi – 2018-02-15T07:19:37.647

46 bytes – Cristian Lupascu – 2018-02-15T09:38:06.783

Save 1 byte with '0<(y-x)*y-=z' – G B – 2018-06-15T06:49:41.247

3

Wolfram Language (Mathematica), 40 bytes

MatchQ[{___,x_,y_,z_,___}/;x<y>z||x>y<z]

Try it online!

alephalpha

Posted 2018-02-14T22:31:11.793

Reputation: 23 988

3

R, 58 56 bytes

function(x)any(abs(diff(sign(diff(c(NA,x)))))>1,na.rm=T)

Try it online!

Saved 2 bytes thanks to Giuseppe

NofP

Posted 2018-02-14T22:31:11.793

Reputation: 754

3You can get rid of the braces {} for -2 bytes. – Giuseppe – 2018-02-15T07:35:05.393

additionally, I think that you can port Stewie Griffin's approach for 42 bytes

– Giuseppe – 2018-02-15T09:36:19.293

@Giuseppe, I think Stewie ported my method, with the difference that mine can properly handle the empty vector as listed in the test cases. Matlab is a bit more lenient with empty vectors compared to R. – NofP – 2018-02-18T15:35:00.870

c() is NULL which is not the same as the empty vector of integers, integer(0), whereas in MATLAB [] is a double by default, but if you want to keep it this way, that's perfectly reasonable. – Giuseppe – 2018-02-20T14:42:17.340

3

J, 16 15 bytes

-1 byte thanks to FrownyFrog

1 e.0>2*/\2-/\]

Try it online!

Original: 16 bytes

0>[:<./2*/\2-/\]

2-/\] - differences of each adjacent items

2*/\ - products of each adjacent items

[:<./ - the minimum

0> - is negative?

Try it online!

Galen Ivanov

Posted 2018-02-14T22:31:11.793

Reputation: 13 815

Hello ! Could this not be shortened to this simpler explicit form 0><./2*/\2-/\ (13 bytes) ? – Mathias Dolidon – 2018-02-16T14:27:31.473

@Mathias Dolidon This works in the interpreter but here in PPCG it's common to provide a function (J verb) if there is some input. If the verb is tacit one, we don't count the assignment f=. bytes. Please have in mind that I'm relatively new user :) – Galen Ivanov – 2018-02-16T14:48:13.540

So am I, and you've clarified the rule for me. Thanks ! :) – Mathias Dolidon – 2018-02-16T15:02:52.697

11 e.0>2*/\2-/\] – FrownyFrog – 2018-03-03T09:19:38.020

@ FrownyFrog Thank you! It seems that I rarely use e. :) – Galen Ivanov – 2018-03-03T18:59:34.207

3

Java 8, 108 104 101 86 84 79 72 bytes

a->{int i=a.length,p=0;for(;i-->1;)i|=p*(p=a[i]-a[i-1])>>-1;return-i>1;}

-2 bytes thanks to @OlivierGrégoire.
-13 bytes thanks to @Nevay.

Try it online.

Kevin Cruijssen

Posted 2018-02-14T22:31:11.793

Reputation: 67 575

184 bytes. I changed the iteration order (going down), swapped the two multiplication operands and then could remove a superfluous -1. – Olivier Grégoire – 2018-02-16T10:25:44.223

179 bytes: a->{int i=a.length;for(;i-->2;)i|=(a[i]-a[--i])*(a[i]-a[i-1])>>-1;return-~i|3;} (returns -1 for truthy cases, 3 for falsey cases) - or, if using the presence/absence of an exception as return value 55 bytes: a->{for(int i=0;++i>0;)i|=(a[i-1]-a[i])*(a[i]-a[i+1]);} – Nevay – 2018-02-18T12:58:43.410

172 bytes: a->{int i=a.length,p=0;for(;i-->1;)i|=p*(p=a[i]-a[i-1])>>-1;return-i>1;} – Nevay – 2018-02-20T13:11:15.077

3

PostgreSQL 173 bytes

SELECT DISTINCT ON(a)a,x>j and x>k OR x<least(j,k)FROM(SELECT a,x,lag(x,1,x)OVER(w)j,lead(x,1,x)OVER(w)k FROM d WINDOW w AS(PARTITION BY rn ORDER BY xn))d ORDER BY 1,2 DESC;
     a     | c 
-----------+---
 {1}       | f
 {1,2}     | f
 {1,2,1}   | t
 {1,2,1,2} | t
 {1,2,2}   | f
 {1,2,2,1} | f
 {1,2,2,3} | f
 {1,2,3}   | f
 {1,3,2}   | t
 {2,2,2}   | f
 {3,1,2}   | t
(11 rows)

Evan Carroll

Posted 2018-02-14T22:31:11.793

Reputation: 133

Hello and welcome to the site. I'm not familiar with PostgreSQL but you might be able to reduce the amount of whitespace you use. In general most languages don't require most of the kinds of spacing you use. – Post Rock Garf Hunter – 2018-02-15T21:38:38.767

@WheatWizard it's the sample data into the database, it's irrelevant. – Evan Carroll – 2018-02-15T21:45:46.797

What's your code then? We don't allow the input to be inserted directly into the code instead of the input. If that is the case here you ought to rewrite it so that it takes input via a standard method.

– Post Rock Garf Hunter – 2018-02-15T21:48:12.183

@WheatWizard from the link you provided, https://codegolf.meta.stackexchange.com/a/5341/23085

– Evan Carroll – 2018-02-15T21:49:48.107

1Ok if that's the input format you are using that's fine then. Best of luck golfing here, it's nice to see people golfing in less commonly used languages. – Post Rock Garf Hunter – 2018-02-15T21:52:21.283

2

Japt, 11 bytes

-5 bytes thanks to @ETHproductions

ä- mÌäa d>1

Try it online! | Test cases

This uses Dennis's algorithm

Oliver

Posted 2018-02-14T22:31:11.793

Reputation: 7 160

2

Japt, 9 bytes

ä- ä* d<0

Try it online!

A mashup of Oliver's answer with the approach used by several other answers.

ETHproductions

Posted 2018-02-14T22:31:11.793

Reputation: 47 880

2

Attache, 39 bytes

Any&:&{_*~?Sum[__]}@Slices&2@Sign@Delta

Try it online!

Pretty happy with how this turned out.

Explanation

This is a composition of four functions:

Delta
Sign
Slices&2
Any&:&{_*~?Sum[__]}

Delta gets the differences between elements. =

Then, Sign is applied to each difference, giving us an array of 1s, 0s, and -1s. =

Then, Slices&2 gives all slices of length two from the array, giving all pairs of differences.

Finally, Any&:&{_*~?Sum[__]} is equivalent to, for input x:

Any[&{_*~?Sum[__]}, x]
Any[[el] -> { el[0] and not (el[0] + el[1] = 0) }, x]

This searches for elements which sum to zero but are not zero. If any such pair of elements exist, then there is a bump.

Conor O'Brien

Posted 2018-02-14T22:31:11.793

Reputation: 36 228

2

Octave, 33 bytes

@(x)0||abs(diff(sign(diff(x))))>1

Try it online!

Explanation:

@(x)                           % Anonymous function taking x as input
                  diff(x)       % Takes the difference between consecutive elements
             sign(diff(x))      % The sign of the differences
        diff(sign(diff(x)))     % The difference between the signs
    abs(diff(sign(diff(x)))>1   % Check if the absolute value is 2
@(x)abs(diff(sign(diff(x)))>1   % Output as matrices that are treated truthy or falsy

Stewie Griffin

Posted 2018-02-14T22:31:11.793

Reputation: 43 471

2

MATL, 8 bytes

dZSd|1>a

Try it online!

Stewie Griffin

Posted 2018-02-14T22:31:11.793

Reputation: 43 471

2

Husk, 7 bytes

V<0Ẋ*Ẋ-

Try it online!

Explanation

V<0Ẋ*Ẋ-  Implicit input, say [2,5,5,1,4,5,3]
     Ẋ-  Consecutive differences: [3,0,-4,3,1,-2]
   Ẋ*    Consecutive products: [0,0,-12,3,-2]
V<0      Is any of them negative? Return 1-based index: 3

Zgarb

Posted 2018-02-14T22:31:11.793

Reputation: 39 083

2

05AB1E, 7 bytes

¥ü‚P0‹Z

Try it online!

Explanation

¥         # calculate delta's
 ü‚       # pair each element with the next element
   P      # product of each pair
    0‹    # check each if less than 0
      Z   # max

Emigna

Posted 2018-02-14T22:31:11.793

Reputation: 50 798

Wasn't there a 1-byte alternative to 0‹ that basically checks the number for a negative sign? – Magic Octopus Urn – 2018-02-16T15:33:01.693

@MagicOctopusUrn: d used to check that top of stack only contained [0-9], which is the opposite of what we want here. But now it's more intelligent and negative/floats are also counted as numbers. – Emigna – 2018-02-16T15:39:19.307

Ahhhh... coulda sworn a saw the negative sign and returned true or something... But I think you're right, I'm remembering your d trick. – Magic Octopus Urn – 2018-02-16T15:48:06.390

2

Brachylog, 10 bytes

s₃.¬≤₁∧¬≥₁

Try it online!

Succeeds (true.) if there is a bump, and fails (false.) if there is no bump.

Explanation

This is fairly readable already:

s₃.           There is a substring of the input…
  .¬≤₁        …which is not non-decreasing…
      ∧       …and…
       ¬≥₁    …which is not non-increasing

Fatalize

Posted 2018-02-14T22:31:11.793

Reputation: 32 976

2

Brachylog, 10 bytes

s₃s₂ᶠ-ᵐ×<0

Try it online!

Not nearly as neat and elegant as @Fatalize's existing 10 byte answer, but it works!

s₃   % There exists a substring of three elements [I,J,K] in the array such that

s₂ᶠ  % When it's split into pairs [[I,J],[J,K]]

-ᵐ   % And each difference is taken [I-J, J-K]

×    % And those differences are multiplied (I-J)*(J-K)
     % (At a bump, one of those will be negative and the other positive. 
     % At other places, both differences will be positive, or both negative, 
     %  or one of them 0 - ultimately resulting in a non-negative product.)

<0   % The product is negative

sundar - Reinstate Monica

Posted 2018-02-14T22:31:11.793

Reputation: 5 296

1

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.

Uriel

Posted 2018-02-14T22:31:11.793

Reputation: 11 708

Unfortunately, this gives an error for , while it should return 0 instead (or throw an error for all falsy inputs). – Erik the Outgolfer – 2018-02-14T23:07:48.893

Oh, and, if you fix the issue, 0∊2> can be 2∊ instead for -2 bytes. – Erik the Outgolfer – 2018-02-14T23:21:20.537

@EriktheOutgolfer temporary quick fix, thanks – Uriel – 2018-02-14T23:45:49.767

Still fails for singleton lists – H.PWiz – 2018-02-14T23:48:39.260

@H.PWiz A possible fix is to replace ⍬≡ with 2>≢ for just 1 extra byte. – Erik the Outgolfer – 2018-02-14T23:51:08.787

1

Python 2, 60 bytes

lambda l:any(p>c<n or p<c>n for p,c,n in zip(l,l[1:],l[2:]))

Try it online!

Pretty much the same thing, thought it would be shorter though...

Python 2, 63 bytes

f=lambda l:l[3:]and(l[0]>l[1]<l[2]or l[0]<l[1]>l[2]or f(l[1:]))

Try it online!

totallyhuman

Posted 2018-02-14T22:31:11.793

Reputation: 15 378

158 bytes using multiplication instead – Shieru Asakoto – 2018-02-15T07:49:24.480

1

Julia 0.6, 57 56 bytes

l->any(p>c<n||p<c>n for(p,c,n)=zip(l,l[2:end],l[3:end]))

Basically just totallyhuman's python answer. -1 byte from user71546

Try it online!

Julia 0.6, 39 bytes

f(x,y,z,a...)=x>y<z||x<y>z||f(y,z,a...)

Lispy recursion style, aka Dennis's python answer. Returns true when a bump exists, otherwise throws an error. This should maybe be 42 bytes since you have to splat it when calling. Eg for a=[1,2,1] you call as f(a...). f(a)=f(a...) would remove that need, but is longer. I need to get better a recursion, and I don't really like writing code that throws an error.

Try it online!

gggg

Posted 2018-02-14T22:31:11.793

Reputation: 1 715

1seems the space after for is not necessary ;) – Shieru Asakoto – 2018-02-15T07:53:46.577

1

Husk, 9 bytes

▼mεẊ-Ẋo±-

Try it online!

Uses Dennis's algorithm.

Erik the Outgolfer

Posted 2018-02-14T22:31:11.793

Reputation: 38 134

1

Perl 5, 54 + 2 (-pa) = 56 bytes

map$\|=$F[$_-1]!=(sort{$a-$b}@F[$_-2..$_])[1],2..$#F}{

Try it online!

Xcali

Posted 2018-02-14T22:31:11.793

Reputation: 7 671

1

Pyt, 11 7 bytes

₋ʁ*0<Ʃ±

Outputs 1 if there is a bump, 0 otherwise

Try it online!

Port of Wheat Wizard's Haskell answer


Old way (11 bytes):

₋±₋Å1≤ĐŁ↔Ʃ=

Try it online!

Returns False if there is a bump, True otherwise

Port of Dennis' Jelly answer

mudkip201

Posted 2018-02-14T22:31:11.793

Reputation: 833

1

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

stevefestl

Posted 2018-02-14T22:31:11.793

Reputation: 539

1An 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. – Neil – 2018-02-15T00:57:31.160

@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. – stevefestl – 2018-02-15T01:01:50.363

1

Wolfram Language (Mathematica), 37 36 bytes

FreeQ[(d=Differences)@Sign@d@#,-2|2]&

Gives the opposite of the test case answers (False and True reversed). Prepend a ! to switch to the normal form.

OR

Abs@(d=Differences)@Sign@d@#~FreeQ~2&

Also reversed output, so replace FreeQ with MatchQ for normal form.

Explanation: Take the sign of the differences of the sequence. Iff the resulting sequence includes {1,-1} or {-1,1} there is a bump. The absolute value the differences of {1,-1} or {-1,1} is 2 in either case.

Shave off another byte by squaring the final list instead of taking the absolute value:

FreeQ[(d=Differences)@Sign@d@#^2,4]&

Try it online!

Kelly Lowder

Posted 2018-02-14T22:31:11.793

Reputation: 3 225

1

Perl, 35 bytes

Includes +3 for -p

bump.pl:

#!/usr/bin/perl -p
s%\S+ %$a*1*($a=$&-$')%eg;$_=/-/

Run as:

bump.pl <<< "3 1 2"

Ton Hospel

Posted 2018-02-14T22:31:11.793

Reputation: 14 114

1

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.

vazt

Posted 2018-02-14T22:31:11.793

Reputation: 311

Welcome to PPCG! – Steadybox – 2018-02-15T17:03:13.323

1

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 }

Alex Shykov

Posted 2018-02-14T22:31:11.793

Reputation: 11

Welcome to PPCG! – Steadybox – 2018-03-03T08:11:38.237

1By 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. – Steadybox – 2018-03-03T08:15:57.287

1

APL (Dyalog Classic), 15 bytes

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

Try it online!

ngn

Posted 2018-02-14T22:31:11.793

Reputation: 11 449

0

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.

Neil

Posted 2018-02-14T22:31:11.793

Reputation: 95 035

0

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

sonrad10

Posted 2018-02-14T22:31:11.793

Reputation: 535

0

Perl 6, 50 47 bytes

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

Try it online!

bb94

Posted 2018-02-14T22:31:11.793

Reputation: 1 831

0

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

panicmore

Posted 2018-02-14T22:31:11.793

Reputation: 51

0

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

Post Rock Garf Hunter

Posted 2018-02-14T22:31:11.793

Reputation: 55 382