Dennis numbers 2.0
JavaScript (ES6), 72 70 bytes
Takes a string as input. Returns either false or a truthy value (which can be a number).
It's using a regular expression to transform an input string such as "2846145"
into:
"(a=2+8)&&(a==4+6)&&(a==1+4+5)"
Then calls eval()
on this expression.
let f =
n=>eval(n.replace(/./g,(v,i)=>(v>n[i-1]?'+':i?')&&(a==':'(a=')+v)+')')
console.log(f("101"));
console.log(f("102"));
console.log(f("777"));
console.log(f("2846145"));
Jelly, 13 12 bytes
1 byte thanks to @Dennis.
DIṠ’0;œṗDS€E
Try it online!
Explanation
DIṠ’0;œṗDS€E Main link. Argument: N
D Convert N to its digits.
I Find the differences between the elements.
Ṡ Find the sign of each difference. This yields 1 for locations where the
list is strictly increasing and 0 or -1 elsewhere.
’ Decrement. This yields 0 for locations where the list is strictly
increasing and -1 or -2 elsewhere.
0; Prepend a 0.
D Get another list of digits.
œṗ Split the list of digits at truthy positions, i.e. the -1s and -2s.
S€ Sum each sublist.
E Check if all values are equal.
Python, 50 bytes
r='0'
for d in input():r=d+'=+'[r<d]*2+r
1/eval(r)
Expects input()
to evaluate to a string, so the input needs surrounding quotes in Python 2. Output is via exit code, where 0 indicates success (truthy) and 1 indicates failure (falsy).
Test it on Ideone.
How it works
We initialize r to the string 0 and iterate over all digits d in the input.
If d is larger than the first digit of r (initially 0, then equal to the previous value of d),
r<d
evaluates to True and'=+'[r<d]*2
yields++
.If d is smaller than the first digit of r,
'=+'[r<d]*2
yields==
.If d is equal to the first digit of r, r will be longer than the singleton string d, so
'=+'[r<d]*2
yields once again==
.
In all cases, the digit d and the two generated characters get prepended to r.
Once all input digits have been processed, eval(r)
evaluates the generated expression.
If the input consists of a single strictly increasing sequence of (positive) digits, the expression evaluates to their sum.
For example, the integer 12345 results in the expression
5++4++3++2++1++0
, which yields 15 when evaluated. Note that each second + is a unary plus, so it doesn't affect the result. Dividing 1 by 15 is valid (the result is not important); the program exits normally.If the input consists of two strictly increasing sequences of digits, the expression consists of a simple comparison.
For example, the integer 12012 results in the expression
2++1++0==2++1++0
, which yields True when evaluated since both terms have sum 3. Dividing 1 by True (1) is valid (the result is not important); the program exits normally.On the other hand, the integer 12366 results in the expression
6==6++3++2++1++0
, which yields False when evaluated since the terms have sums 6 and 12. Dividing 1 by False (0) raises a ZeroDivisionError; the program exits with an error.If the input consists of three or more strictly increasing sequences of digits, the expression consists of a chained comparison, which returns True if and only if all involved comparisons return True.
For example, the integer 94536 results in the expression
6++3==5++4==9++0
, which yields True when evaluated since all terms have sum 9. As before, the program exits normally.On the other hand, the integer 17263 results in the expression
3==6++2==7++1++0
, which yields False when evaluated since the terms have sums 3, 8, and 8. As before, the program exits with an error.