Mountain range numbers Mountain range number

A number is a mountain range number if the inequalities satisfied by their consecutive digits alternate. In a way, looking at the number's digits should exhibit a /\/\/\... or a \/\/\/... pattern.

More formally, if our number n has \$k\$ digits

$$n = d_1d_2d_3\cdots d_k$$

then n is a mountain range number if

$$\begin{cases}d_1 &gt; d_2 \\ d_2 &lt; d_3 \\ d_3 &gt; d_4 \\ \cdots \end{cases} \vee \begin{cases}d_1 &lt; d_2 \\ d_2 &gt; d_3 \\ d_3 &lt; d_4 \\ \cdots \end{cases}$$

Your task

Given an integer with 3 or more digits, output a Truthy value if the number is a mountain range number or Falsy otherwise.

Input

A positive integer n with 3 or more digits, in any reasonable format, e.g.


Integer
String
List of digits


Test cases

Truthy

1324 -&gt; Truthy
9191 -&gt; Truthy
12121 -&gt; Truthy
121212 -&gt; Truthy
1212121 -&gt; Truthy
19898 -&gt; Truthy


Falsy

(Added another Falsy test case as per the comments, some answers might not cover the 4422 test case)

123 -&gt; Falsy
321 -&gt; Falsy
4103 -&gt; Falsy
2232 -&gt; Falsy
1919199 -&gt; Falsy
4422 -&gt; Falsy


This is code-golf so shortest answer in bytes wins! Standard loopholes are forbidden.
R, 44 40 bytes

 crossed out 44 is still regular 44

-1 byte thanks to Giuseppe.



function(x,d=diff)all(d(sign(d(x)))^2&gt;3)


Try it online!

Computes the differences of the signs of the differences of the input. These must all be equal to 2 or -2, i.e. the square must equal 4; checking that the square is >3 is sufficient.

If two consecutive digits are equal, there will be a 0 in the signs of differences, leading to a difference of signs of differences equal to 1 or -1. If three consecutive digits are in ascending or descending order, then the corresponding differences will be of the same sign, leading to a difference of signs of differences equal to 0. If neither of these occurs, the number is a mountain range number.



Old version (included as it might be golfable):

R, 44 43 bytes

-1 byte thanks to Giuseppe.



function(x)all(s&lt;-sign(diff(x)),rle(s)$l&lt;2)


Try it online!

Computes the signs of the differences of consecutive digits. Then verifies that


none of the signs are 0s (would correspond to 2 equal consecutive digits);
the runs of the signs are all equal to 1, i.e. no 2 consecutive signs are equal.