22
3
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 > d_2 \\ d_2 < d_3 \\ d_3 > d_4 \\ \cdots \end{cases} \vee \begin{cases}d_1 < d_2 \\ d_2 > d_3 \\ d_3 < 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 -> Truthy
9191 -> Truthy
12121 -> Truthy
121212 -> Truthy
1212121 -> Truthy
19898 -> Truthy
Falsy
(Added another Falsy test case as per the comments, some answers might not cover the 4422 test case)
123 -> Falsy
321 -> Falsy
4103 -> Falsy
2232 -> Falsy
1919199 -> Falsy
4422 -> Falsy
This is code-golf so shortest answer in bytes wins! Standard loopholes are forbidden.
1May we take input as a list of digits rather than an integer? – Robin Ryder – 2020-02-06T17:03:06.640
2@RobinRyder yes you may – RGS – 2020-02-06T17:10:14.590
Dupe? – AdmBorkBork – 2020-02-06T19:01:43.760
2@AdmBorkBork this one has an input relaxation (
k>=3
); also the other challenge is so old that many of the answers there wouldn't be close to competitive on this one; I'd actually rather remove the old one as a dupe of this. – Giuseppe – 2020-02-06T19:27:40.2231@Giuseppe That's what I was leaning towards as well, hence why I didn't hammer it. I have now just hammered the old one. Thanks! – AdmBorkBork – 2020-02-06T19:52:03.560
1Related with a source restriction – Jo King – 2020-02-07T00:39:58.593
Suggested test case:
[4,4,2,2]
(or4422
if you prefer). – Chas Brown – 2020-02-07T00:58:13.043@ChasBrown thanks for the suggestion. why would you say 4422 is a nice test case here? – RGS – 2020-02-07T08:49:04.323