Is this number a hill number?
up vote
16
down vote
favorite
A hill number is a number that has the same digit in the first & the last, but that's not all. In a hill number the first digits are strictly increasing, and the last digits are strictly decreasing. The largest digit can be repeated.
Here is an example of a hill number:
12377731 | 1237... | ...731
^ same ^ | strictly increasing | strictly decreasing
---------+---------------------+---------------------
12377731
^^^ okay because largest digit can be repeated
This is not:
4588774 | ...8774
| ^^ not the largest digit
| so this has to be strictly decreasing
| but it's not, so not a hill number
Challenge
Given a positive integer, write a full program or a function that returns truthy for hill numbers but falsy on other values.
Notes:
- Input & output can be in any reasonable format.
- Taking input as a list of digits is not considered reasonable.
- Taking input as a list of digits is not considered reasonable.
- This is code-golf so shortest answer in each language wins!
Test Cases
12321 -> Truthy
1233321 -> Truthy
99 -> Truthy
3 -> Truthy
234567992 -> Truthy
1232 -> Falsy
778896 -> Falsy
23232 -> Falsy
45566554 -> Falsy
5645 -> Falsy
code-golf decision-problem number-theory
add a comment |
up vote
16
down vote
favorite
A hill number is a number that has the same digit in the first & the last, but that's not all. In a hill number the first digits are strictly increasing, and the last digits are strictly decreasing. The largest digit can be repeated.
Here is an example of a hill number:
12377731 | 1237... | ...731
^ same ^ | strictly increasing | strictly decreasing
---------+---------------------+---------------------
12377731
^^^ okay because largest digit can be repeated
This is not:
4588774 | ...8774
| ^^ not the largest digit
| so this has to be strictly decreasing
| but it's not, so not a hill number
Challenge
Given a positive integer, write a full program or a function that returns truthy for hill numbers but falsy on other values.
Notes:
- Input & output can be in any reasonable format.
- Taking input as a list of digits is not considered reasonable.
- Taking input as a list of digits is not considered reasonable.
- This is code-golf so shortest answer in each language wins!
Test Cases
12321 -> Truthy
1233321 -> Truthy
99 -> Truthy
3 -> Truthy
234567992 -> Truthy
1232 -> Falsy
778896 -> Falsy
23232 -> Falsy
45566554 -> Falsy
5645 -> Falsy
code-golf decision-problem number-theory
5
What about222222222
? Is it a flat hill number?
– frarugi87
23 hours ago
222222222
is a hill number, largest digit is 2 and thus can be repeated
– u_ndefined
1 hour ago
Is a string reasonable?
– Sanchises
1 hour ago
@Sanchises yes, but list of chars is not
– u_ndefined
1 hour ago
@frarugi87 See comment above.
– Dennis♦
1 hour ago
add a comment |
up vote
16
down vote
favorite
up vote
16
down vote
favorite
A hill number is a number that has the same digit in the first & the last, but that's not all. In a hill number the first digits are strictly increasing, and the last digits are strictly decreasing. The largest digit can be repeated.
Here is an example of a hill number:
12377731 | 1237... | ...731
^ same ^ | strictly increasing | strictly decreasing
---------+---------------------+---------------------
12377731
^^^ okay because largest digit can be repeated
This is not:
4588774 | ...8774
| ^^ not the largest digit
| so this has to be strictly decreasing
| but it's not, so not a hill number
Challenge
Given a positive integer, write a full program or a function that returns truthy for hill numbers but falsy on other values.
Notes:
- Input & output can be in any reasonable format.
- Taking input as a list of digits is not considered reasonable.
- Taking input as a list of digits is not considered reasonable.
- This is code-golf so shortest answer in each language wins!
Test Cases
12321 -> Truthy
1233321 -> Truthy
99 -> Truthy
3 -> Truthy
234567992 -> Truthy
1232 -> Falsy
778896 -> Falsy
23232 -> Falsy
45566554 -> Falsy
5645 -> Falsy
code-golf decision-problem number-theory
A hill number is a number that has the same digit in the first & the last, but that's not all. In a hill number the first digits are strictly increasing, and the last digits are strictly decreasing. The largest digit can be repeated.
Here is an example of a hill number:
12377731 | 1237... | ...731
^ same ^ | strictly increasing | strictly decreasing
---------+---------------------+---------------------
12377731
^^^ okay because largest digit can be repeated
This is not:
4588774 | ...8774
| ^^ not the largest digit
| so this has to be strictly decreasing
| but it's not, so not a hill number
Challenge
Given a positive integer, write a full program or a function that returns truthy for hill numbers but falsy on other values.
Notes:
- Input & output can be in any reasonable format.
- Taking input as a list of digits is not considered reasonable.
- Taking input as a list of digits is not considered reasonable.
- This is code-golf so shortest answer in each language wins!
Test Cases
12321 -> Truthy
1233321 -> Truthy
99 -> Truthy
3 -> Truthy
234567992 -> Truthy
1232 -> Falsy
778896 -> Falsy
23232 -> Falsy
45566554 -> Falsy
5645 -> Falsy
code-golf decision-problem number-theory
code-golf decision-problem number-theory
edited 1 hour ago
asked yesterday
u_ndefined
59913
59913
5
What about222222222
? Is it a flat hill number?
– frarugi87
23 hours ago
222222222
is a hill number, largest digit is 2 and thus can be repeated
– u_ndefined
1 hour ago
Is a string reasonable?
– Sanchises
1 hour ago
@Sanchises yes, but list of chars is not
– u_ndefined
1 hour ago
@frarugi87 See comment above.
– Dennis♦
1 hour ago
add a comment |
5
What about222222222
? Is it a flat hill number?
– frarugi87
23 hours ago
222222222
is a hill number, largest digit is 2 and thus can be repeated
– u_ndefined
1 hour ago
Is a string reasonable?
– Sanchises
1 hour ago
@Sanchises yes, but list of chars is not
– u_ndefined
1 hour ago
@frarugi87 See comment above.
– Dennis♦
1 hour ago
5
5
What about
222222222
? Is it a flat hill number?– frarugi87
23 hours ago
What about
222222222
? Is it a flat hill number?– frarugi87
23 hours ago
222222222
is a hill number, largest digit is 2 and thus can be repeated– u_ndefined
1 hour ago
222222222
is a hill number, largest digit is 2 and thus can be repeated– u_ndefined
1 hour ago
Is a string reasonable?
– Sanchises
1 hour ago
Is a string reasonable?
– Sanchises
1 hour ago
@Sanchises yes, but list of chars is not
– u_ndefined
1 hour ago
@Sanchises yes, but list of chars is not
– u_ndefined
1 hour ago
@frarugi87 See comment above.
– Dennis♦
1 hour ago
@frarugi87 See comment above.
– Dennis♦
1 hour ago
add a comment |
17 Answers
17
active
oldest
votes
up vote
6
down vote
Jelly, 8 bytes
_ƝṠÞ+SƊƑ
Try it online!
How it works
_ƝṠÞ+SƊƑ Main link. Argument: n (integer)
_Ɲ Take the differences of neighboring digits.
This maps n = abcd to [a-b, b-c, c-d].
Ƒ Fixed; apply the link to the left and return 1 if the result is equal to
its argument, 0 if not.
Ɗ Drei; combine the three links to the left into a monadic chain.
ṠÞ Sort the differences by their signs (negative, zero, positive).
S Take the sum of the differences, yielding 0 if and only if the
first digit is equal to the last.
+ Add the sum to each difference.
add a comment |
up vote
4
down vote
Pyth, 16 bytes
&SI_._MJ.+jQT!sJ
Try the test suite.
jQT input in base 10
J.+ J = differences: [3,1,4,1] -> [-2,3,-3]
._M Signs of each element of J
_ Reverse the list
SI and check if it is Invariant under Sorting.
If this is true, J consists of some positive numbers,
followed by some 0s, followed by some negative numbers,
which is what we want.
!sJ Now we check the other hill condition by ensuring
sum(differences) = 0; i.e. the first and last digit are equal.
& We take the logical AND of both conditions.
add a comment |
up vote
4
down vote
Jelly, 11 bytes
DIµṠNṢƑaS¬$
Explanation:
D Convert to a list of Digits.
I Increments; compute differences between successive elements.
µ Start new µonadic link.
Ṡ Find Ṡign of each increment
N then negate;
ṢƑ is the result invariant under Ṣorting?
If so, the increments consist of some positive numbers,
followed by some 0s, followed by some negative numbers,
which is what we want.
a Logical AND this result with
S¬$ logical NOT of the Sum of the increments.
If the sum of the increments is zero, first and last digits are equal.
Try it online!
add a comment |
up vote
4
down vote
JavaScript (ES6), 62 54 bytes
Takes input as a string. Returns a Boolean value.
s=>s[-[...s].some(p=q=n=>q>(q=Math.sign(p-(p=n))))]==p
Try it online!
Commented
s => // s = input string
s[ // we will eventually access either s[0] or s[-1]
-[...s].some( // depending on the result of this some()
p = q = // initialize p and q to non-numeric values
n => // for each digit n:
q > ( // compare q with
q = // the new value of q,
Math.sign( // defined as the sign of
p - (p = n) // the difference between the current digit and the previous one
)) // yield true if the previous q is greater than the new q
) // s[-1] being undefined, a truhty some() will force the test to fail
] == p // otherwise: test if the 1st digit s[0] is equal to the last digit p
JavaScript (ES6), 65 bytes
A solution using a regular expression. Takes input as a string. Returns $0$ or $1$.
s=>/N(,-d+)*(,0)*[^0-]*$/.test([...s].map(p=v=>p-(p=v)))&p==s[0]
Try it online!
How?
We first convert the number to a list of pairwise digit differences in $[-9,9]$:
[...s].map(p = v => p - (p = v))
Example:
"234567992" --> [ NaN, -1, -1, -1, -1, -1, -2, 0, 7 ]
This array is coerced to a string, which gives:
"NaN,-1,-1,-1,-1,-1,-2,0,7"
We apply the following regular expression:
+-----------------------> the second 'N' of 'NaN'
| +------------------> a sequence of negative numbers
| | +------------> a sequence of zeros
| | | +------> a sequence of positive numbers
| | | | +---> end of string
| | | | |
|/¨¨¨¨¨¨/¨¨¨/¨¨¨¨|
/N(,-d+)*(,0)*[^0-]*$/
Finally, we also test if the last digit p
is equal to the first digit s[0]
.
You can save 5 bytes by taking input as an array of digits.
– Shaggy
19 hours ago
@Shaggy I wish I could but this is apparently not allowed .
– Arnauld
19 hours ago
From the spec, with original emphasis: "Input & output can be in any reasonable format" - we usually consider a digit array a reasonable format for an integer.
– Shaggy
19 hours ago
@J.Doe The OP hasn't confirmed yet if numbers such as 222222222 are hill numbers or not. (If not, I think several answers are going to be invalidated.)
– Arnauld
13 hours ago
add a comment |
up vote
3
down vote
Python 2, 114 112 bytes
lambda n:all((n[0]==n[-1])*sorted(set(x))==list(x)[::d]for x,d in zip(n.split(max(n)*n.count(max(n)),1),[1,-1]))
Try it online!
add a comment |
up vote
3
down vote
Perl 6, 39 bytes
{.[0]==.tail&&[<=] $_ Z<=>.skip}o*.comb
Try it online!
Explanation
{ ... }o.comb # Split into digits and feed into block
.[0]==.tail # First element equals last
&& # and
$_ Z<=>.skip # Pairwise application of three-way comparator
[<=] # Results never decrease
I was literally seconds away from posting this lol.
– Jo King
yesterday
add a comment |
up vote
2
down vote
05AB1E, 19 17 13 12 bytes
¥D.±Â{RQsO_*
-5 bytes by creating a port of @lirtosiast's Pyth answer.
Try it online or verify all test cases.
Explanation:
¥ # Push the deltas of the digits of the (implicit) input
# i.e. 4588774 → [1,3,0,-1,0,-3]
D # Duplicate this list
.± # Get the sign of each
# [1,3,0,-1,0,-3] → [1,1,0,-1,0,-1]
 # Bifurcate (short for DR: Duplicate and Reverse copy)
# i.e. [1,1,0,-1,0,-1] → [-1,0,-1,0,1,1]
{ # Sort the copy
# i.e. [-1,0,-1,0,1,1] → [-1,-1,0,0,1,1]
R # Reverse it
# i.e. [1,1,0,0,-1,-1]
Q # And check if they are equal
# i.e. [1,1,0,-1,0,-1] and [1,1,0,0,-1,-1] → 0 (falsey)
s # Swap to get the list of deltas again
O # Take the sum
# i.e. [1,3,0,-1,0,-3] → 0
_ # And check if it's exactly 0
# 0 → 1 (truthy)
* # Check if both are truthy (and output implicitly)
# i.e. 0 and 1 → 0 (falsey)
Â{RQ
can alternatively be (Â{Q
for the same byte-count, where (
negates each sign: Try it online.
add a comment |
up vote
1
down vote
J, 23 bytes
[:((0=+/)**-:*/:*)2-/]
Idea stolen from the Jelly answers. Just wanted to see how short I could make it in J.
Try it online!
add a comment |
up vote
1
down vote
R, 65 bytes
Takes strings. Took the idea for checking sort invariance from the Pyth answer.
function(a)!sum(d<-diff(utf8ToInt(a)))&all(sort(k<-sign(d),T)==k)
Try it online!
add a comment |
up vote
0
down vote
Retina 0.8.2, 52 bytes
.
$*1;$&$*1,
(1+),1
,
^(1+);(,1+;)*(,;)*(1+,;)*1,$
Try it online! Link includes test cases. Explanation:
.
$*1;$&$*1,
Convert each digit to unary twice, separated by ;
s and terminated by ,
s. However, you can then think of the result as the first digit, a ;
, then all the pairs of adjacent digits, the digits of each pair separated by ,
and the pairs separated by ;
s, then another ;
, then the last digit, then a final ,
.
(1+),1
,
Subtract the pairs of adjacent digits. This leaves ;,;
for equal digits and 1
s on the greater side for unequal digits. (This could be done as part of the following regex but obviously that wouldn't be so golfy.)
^(1+);(,1+;)*(,;)*(1+,;)*1,$
Match the first digit, then any number of pairs of ascending digits, then any number of pairs of equal digits, then any number of pairs of descending digits, then match the first digit again at the very end.
add a comment |
up vote
0
down vote
Japt, 11 bytes
Takes input as a digit array.
ä-
eUñg)«Ux
Try it or run all test cases
add a comment |
up vote
0
down vote
Mathematica/Wolfram Language, 69 bytes
Pure function. Takes input as an integer, returns True
or False
.
Sort[x=Sign[-Differences[y=IntegerDigits[#]]]]==x&&First[y]==Last[y]&
Explanation:
The first clause checks the "hilliness":
IntegerDigits
: Get digits from integer. Store iny
.
-Differences
: Take successive differences and flip signs.
Sign
: Replace each entry with +1 if positive, 0 if zero, and -1 if negative. Store inx
.
Sort
: Sort list of +1, 0, -1 from smallest to largest. Compare to original list inx
.
The second clause checks whether the first and last digits are equal.
The fact thatIntegerDigits
andDifferences
are rather long function names is a bit annoying.
– Michael Seifert
19 hours ago
add a comment |
up vote
0
down vote
Red, 181 bytes
func[n][m: last sort copy t: s: form n
parse t[opt[copy a to m(a: sort unique a)]copy b thru any m
opt[copy c to end(c: sort/reverse unique c)]](s = rejoin[a b c])and(s/1 = last s)]
Try it online!
More readable:
f: func[n][
t: s: form n
m: last sort copy t
parse t [ opt [ copy a to m (a: sort unique a) ]
copy b thru any m
opt [ copy c to end (c: sort/reverse unique c) ]
]
(s = rejoin [ a b c ]) and (s/1 = last s)
]
add a comment |
up vote
0
down vote
Powershell, 77 bytes
($x=-join("$($args|%{"-$_;$_"})"|iex))-match'^(-d)+0*d+$'-and$x[1]-eq$x[-1]
Less golfed test script:
$f = {
# $args = 1,2,3,3,3,2,1
$a=$args|%{"-$_;$_"} # "-1;1","-2;2","-3;3","-3;3","-3;3","-2;2","-1;1"
$d="$a" # "-1;1 -2;2 -3;3 -3;3 -3;3 -2;2 -1;1"
$x=-join($d|Invoke-Expression) # "-1-1-100111"
$x-match'^(-d)+0*d+$'-and$x[1]-eq$x[-1] # $true or $false
}
@(
,($True , 1,2,3,2,1 )
,($True , 1,2,3,3,3,2,1 )
,($True , 9,9 )
,($True , 3 )
,($True , 2,3,4,5,6,7,9,9,2 )
,($False, 1,2,3,2 )
,($False, 7,7,8,8,9,6 )
,($False, 2,3,2,3,2 )
,($False, 4,5,5,6,6,5,5,4 )
,($False, 5,6,4,5 )
) | % {
$expected,$a = $_
$result = &$f @a
"$($result-eq$expected): $result"
}
Output:
True: True
True: True
True: True
True: True
True: True
True: False
True: False
True: False
True: False
True: False
add a comment |
up vote
0
down vote
MATL, 12 bytes
dZSd1<AGds~*
Try it online!
(note: the TIO version uses H
instead of G
to process all test cases at once.)
Explanation
Input is a string of digits. Output is a 1
or 0
. The number 222222
is a hill number according to this program. Saved 2 bytes by copying Dennis' method for checking equality of the first and last digits.
d % Takes the difference between digits
ZS % Calculate the sign.
d % Take the difference again.
1<A % A number is a hill number if `A`ll these differences are < 1.
Gds % Push the input, and sum all the differences.
~ % Negate
* % Multiply the two tests (=logical AND).
add a comment |
up vote
0
down vote
C# (Visual C# Interactive Compiler), 172 bytes
x=>{var s=x+"";var m=s.OrderBy(c=>c).Last();return s[0]==s.Last()&Enumerable.Range(1,s.Length-1).All(i=>i>s.LastIndexOf(m)?s[i-1]>s[i]:i>s.IndexOf(m)?m==s[i]:s[i-1]<s[i]);}
Try it online!
Here is an overview of how this works...
- Convert input
int
intostring
- Find the largest digit
- Ensure the first and last digits are the same
- Ensure digits after the last occurrence of the largest digit are decreasing
- Ensure digits between the first and last occurrence of the largest digit are equal to the largest digit
- Ensure digits before the first occurrence of the largest digit are increasing
add a comment |
up vote
0
down vote
Python 2, 62 bytes
lambda s:s[:eval('<='.join(map(str,map(cmp,s,s[1:]))))]==s[-1]
Takes input as a string and returns a Boolean.
Try it online!
add a comment |
17 Answers
17
active
oldest
votes
17 Answers
17
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
down vote
Jelly, 8 bytes
_ƝṠÞ+SƊƑ
Try it online!
How it works
_ƝṠÞ+SƊƑ Main link. Argument: n (integer)
_Ɲ Take the differences of neighboring digits.
This maps n = abcd to [a-b, b-c, c-d].
Ƒ Fixed; apply the link to the left and return 1 if the result is equal to
its argument, 0 if not.
Ɗ Drei; combine the three links to the left into a monadic chain.
ṠÞ Sort the differences by their signs (negative, zero, positive).
S Take the sum of the differences, yielding 0 if and only if the
first digit is equal to the last.
+ Add the sum to each difference.
add a comment |
up vote
6
down vote
Jelly, 8 bytes
_ƝṠÞ+SƊƑ
Try it online!
How it works
_ƝṠÞ+SƊƑ Main link. Argument: n (integer)
_Ɲ Take the differences of neighboring digits.
This maps n = abcd to [a-b, b-c, c-d].
Ƒ Fixed; apply the link to the left and return 1 if the result is equal to
its argument, 0 if not.
Ɗ Drei; combine the three links to the left into a monadic chain.
ṠÞ Sort the differences by their signs (negative, zero, positive).
S Take the sum of the differences, yielding 0 if and only if the
first digit is equal to the last.
+ Add the sum to each difference.
add a comment |
up vote
6
down vote
up vote
6
down vote
Jelly, 8 bytes
_ƝṠÞ+SƊƑ
Try it online!
How it works
_ƝṠÞ+SƊƑ Main link. Argument: n (integer)
_Ɲ Take the differences of neighboring digits.
This maps n = abcd to [a-b, b-c, c-d].
Ƒ Fixed; apply the link to the left and return 1 if the result is equal to
its argument, 0 if not.
Ɗ Drei; combine the three links to the left into a monadic chain.
ṠÞ Sort the differences by their signs (negative, zero, positive).
S Take the sum of the differences, yielding 0 if and only if the
first digit is equal to the last.
+ Add the sum to each difference.
Jelly, 8 bytes
_ƝṠÞ+SƊƑ
Try it online!
How it works
_ƝṠÞ+SƊƑ Main link. Argument: n (integer)
_Ɲ Take the differences of neighboring digits.
This maps n = abcd to [a-b, b-c, c-d].
Ƒ Fixed; apply the link to the left and return 1 if the result is equal to
its argument, 0 if not.
Ɗ Drei; combine the three links to the left into a monadic chain.
ṠÞ Sort the differences by their signs (negative, zero, positive).
S Take the sum of the differences, yielding 0 if and only if the
first digit is equal to the last.
+ Add the sum to each difference.
edited 22 hours ago
answered 23 hours ago
Dennis♦
184k32293729
184k32293729
add a comment |
add a comment |
up vote
4
down vote
Pyth, 16 bytes
&SI_._MJ.+jQT!sJ
Try the test suite.
jQT input in base 10
J.+ J = differences: [3,1,4,1] -> [-2,3,-3]
._M Signs of each element of J
_ Reverse the list
SI and check if it is Invariant under Sorting.
If this is true, J consists of some positive numbers,
followed by some 0s, followed by some negative numbers,
which is what we want.
!sJ Now we check the other hill condition by ensuring
sum(differences) = 0; i.e. the first and last digit are equal.
& We take the logical AND of both conditions.
add a comment |
up vote
4
down vote
Pyth, 16 bytes
&SI_._MJ.+jQT!sJ
Try the test suite.
jQT input in base 10
J.+ J = differences: [3,1,4,1] -> [-2,3,-3]
._M Signs of each element of J
_ Reverse the list
SI and check if it is Invariant under Sorting.
If this is true, J consists of some positive numbers,
followed by some 0s, followed by some negative numbers,
which is what we want.
!sJ Now we check the other hill condition by ensuring
sum(differences) = 0; i.e. the first and last digit are equal.
& We take the logical AND of both conditions.
add a comment |
up vote
4
down vote
up vote
4
down vote
Pyth, 16 bytes
&SI_._MJ.+jQT!sJ
Try the test suite.
jQT input in base 10
J.+ J = differences: [3,1,4,1] -> [-2,3,-3]
._M Signs of each element of J
_ Reverse the list
SI and check if it is Invariant under Sorting.
If this is true, J consists of some positive numbers,
followed by some 0s, followed by some negative numbers,
which is what we want.
!sJ Now we check the other hill condition by ensuring
sum(differences) = 0; i.e. the first and last digit are equal.
& We take the logical AND of both conditions.
Pyth, 16 bytes
&SI_._MJ.+jQT!sJ
Try the test suite.
jQT input in base 10
J.+ J = differences: [3,1,4,1] -> [-2,3,-3]
._M Signs of each element of J
_ Reverse the list
SI and check if it is Invariant under Sorting.
If this is true, J consists of some positive numbers,
followed by some 0s, followed by some negative numbers,
which is what we want.
!sJ Now we check the other hill condition by ensuring
sum(differences) = 0; i.e. the first and last digit are equal.
& We take the logical AND of both conditions.
answered yesterday
lirtosiast
15.5k436104
15.5k436104
add a comment |
add a comment |
up vote
4
down vote
Jelly, 11 bytes
DIµṠNṢƑaS¬$
Explanation:
D Convert to a list of Digits.
I Increments; compute differences between successive elements.
µ Start new µonadic link.
Ṡ Find Ṡign of each increment
N then negate;
ṢƑ is the result invariant under Ṣorting?
If so, the increments consist of some positive numbers,
followed by some 0s, followed by some negative numbers,
which is what we want.
a Logical AND this result with
S¬$ logical NOT of the Sum of the increments.
If the sum of the increments is zero, first and last digits are equal.
Try it online!
add a comment |
up vote
4
down vote
Jelly, 11 bytes
DIµṠNṢƑaS¬$
Explanation:
D Convert to a list of Digits.
I Increments; compute differences between successive elements.
µ Start new µonadic link.
Ṡ Find Ṡign of each increment
N then negate;
ṢƑ is the result invariant under Ṣorting?
If so, the increments consist of some positive numbers,
followed by some 0s, followed by some negative numbers,
which is what we want.
a Logical AND this result with
S¬$ logical NOT of the Sum of the increments.
If the sum of the increments is zero, first and last digits are equal.
Try it online!
add a comment |
up vote
4
down vote
up vote
4
down vote
Jelly, 11 bytes
DIµṠNṢƑaS¬$
Explanation:
D Convert to a list of Digits.
I Increments; compute differences between successive elements.
µ Start new µonadic link.
Ṡ Find Ṡign of each increment
N then negate;
ṢƑ is the result invariant under Ṣorting?
If so, the increments consist of some positive numbers,
followed by some 0s, followed by some negative numbers,
which is what we want.
a Logical AND this result with
S¬$ logical NOT of the Sum of the increments.
If the sum of the increments is zero, first and last digits are equal.
Try it online!
Jelly, 11 bytes
DIµṠNṢƑaS¬$
Explanation:
D Convert to a list of Digits.
I Increments; compute differences between successive elements.
µ Start new µonadic link.
Ṡ Find Ṡign of each increment
N then negate;
ṢƑ is the result invariant under Ṣorting?
If so, the increments consist of some positive numbers,
followed by some 0s, followed by some negative numbers,
which is what we want.
a Logical AND this result with
S¬$ logical NOT of the Sum of the increments.
If the sum of the increments is zero, first and last digits are equal.
Try it online!
edited yesterday
answered yesterday
lirtosiast
15.5k436104
15.5k436104
add a comment |
add a comment |
up vote
4
down vote
JavaScript (ES6), 62 54 bytes
Takes input as a string. Returns a Boolean value.
s=>s[-[...s].some(p=q=n=>q>(q=Math.sign(p-(p=n))))]==p
Try it online!
Commented
s => // s = input string
s[ // we will eventually access either s[0] or s[-1]
-[...s].some( // depending on the result of this some()
p = q = // initialize p and q to non-numeric values
n => // for each digit n:
q > ( // compare q with
q = // the new value of q,
Math.sign( // defined as the sign of
p - (p = n) // the difference between the current digit and the previous one
)) // yield true if the previous q is greater than the new q
) // s[-1] being undefined, a truhty some() will force the test to fail
] == p // otherwise: test if the 1st digit s[0] is equal to the last digit p
JavaScript (ES6), 65 bytes
A solution using a regular expression. Takes input as a string. Returns $0$ or $1$.
s=>/N(,-d+)*(,0)*[^0-]*$/.test([...s].map(p=v=>p-(p=v)))&p==s[0]
Try it online!
How?
We first convert the number to a list of pairwise digit differences in $[-9,9]$:
[...s].map(p = v => p - (p = v))
Example:
"234567992" --> [ NaN, -1, -1, -1, -1, -1, -2, 0, 7 ]
This array is coerced to a string, which gives:
"NaN,-1,-1,-1,-1,-1,-2,0,7"
We apply the following regular expression:
+-----------------------> the second 'N' of 'NaN'
| +------------------> a sequence of negative numbers
| | +------------> a sequence of zeros
| | | +------> a sequence of positive numbers
| | | | +---> end of string
| | | | |
|/¨¨¨¨¨¨/¨¨¨/¨¨¨¨|
/N(,-d+)*(,0)*[^0-]*$/
Finally, we also test if the last digit p
is equal to the first digit s[0]
.
You can save 5 bytes by taking input as an array of digits.
– Shaggy
19 hours ago
@Shaggy I wish I could but this is apparently not allowed .
– Arnauld
19 hours ago
From the spec, with original emphasis: "Input & output can be in any reasonable format" - we usually consider a digit array a reasonable format for an integer.
– Shaggy
19 hours ago
@J.Doe The OP hasn't confirmed yet if numbers such as 222222222 are hill numbers or not. (If not, I think several answers are going to be invalidated.)
– Arnauld
13 hours ago
add a comment |
up vote
4
down vote
JavaScript (ES6), 62 54 bytes
Takes input as a string. Returns a Boolean value.
s=>s[-[...s].some(p=q=n=>q>(q=Math.sign(p-(p=n))))]==p
Try it online!
Commented
s => // s = input string
s[ // we will eventually access either s[0] or s[-1]
-[...s].some( // depending on the result of this some()
p = q = // initialize p and q to non-numeric values
n => // for each digit n:
q > ( // compare q with
q = // the new value of q,
Math.sign( // defined as the sign of
p - (p = n) // the difference between the current digit and the previous one
)) // yield true if the previous q is greater than the new q
) // s[-1] being undefined, a truhty some() will force the test to fail
] == p // otherwise: test if the 1st digit s[0] is equal to the last digit p
JavaScript (ES6), 65 bytes
A solution using a regular expression. Takes input as a string. Returns $0$ or $1$.
s=>/N(,-d+)*(,0)*[^0-]*$/.test([...s].map(p=v=>p-(p=v)))&p==s[0]
Try it online!
How?
We first convert the number to a list of pairwise digit differences in $[-9,9]$:
[...s].map(p = v => p - (p = v))
Example:
"234567992" --> [ NaN, -1, -1, -1, -1, -1, -2, 0, 7 ]
This array is coerced to a string, which gives:
"NaN,-1,-1,-1,-1,-1,-2,0,7"
We apply the following regular expression:
+-----------------------> the second 'N' of 'NaN'
| +------------------> a sequence of negative numbers
| | +------------> a sequence of zeros
| | | +------> a sequence of positive numbers
| | | | +---> end of string
| | | | |
|/¨¨¨¨¨¨/¨¨¨/¨¨¨¨|
/N(,-d+)*(,0)*[^0-]*$/
Finally, we also test if the last digit p
is equal to the first digit s[0]
.
You can save 5 bytes by taking input as an array of digits.
– Shaggy
19 hours ago
@Shaggy I wish I could but this is apparently not allowed .
– Arnauld
19 hours ago
From the spec, with original emphasis: "Input & output can be in any reasonable format" - we usually consider a digit array a reasonable format for an integer.
– Shaggy
19 hours ago
@J.Doe The OP hasn't confirmed yet if numbers such as 222222222 are hill numbers or not. (If not, I think several answers are going to be invalidated.)
– Arnauld
13 hours ago
add a comment |
up vote
4
down vote
up vote
4
down vote
JavaScript (ES6), 62 54 bytes
Takes input as a string. Returns a Boolean value.
s=>s[-[...s].some(p=q=n=>q>(q=Math.sign(p-(p=n))))]==p
Try it online!
Commented
s => // s = input string
s[ // we will eventually access either s[0] or s[-1]
-[...s].some( // depending on the result of this some()
p = q = // initialize p and q to non-numeric values
n => // for each digit n:
q > ( // compare q with
q = // the new value of q,
Math.sign( // defined as the sign of
p - (p = n) // the difference between the current digit and the previous one
)) // yield true if the previous q is greater than the new q
) // s[-1] being undefined, a truhty some() will force the test to fail
] == p // otherwise: test if the 1st digit s[0] is equal to the last digit p
JavaScript (ES6), 65 bytes
A solution using a regular expression. Takes input as a string. Returns $0$ or $1$.
s=>/N(,-d+)*(,0)*[^0-]*$/.test([...s].map(p=v=>p-(p=v)))&p==s[0]
Try it online!
How?
We first convert the number to a list of pairwise digit differences in $[-9,9]$:
[...s].map(p = v => p - (p = v))
Example:
"234567992" --> [ NaN, -1, -1, -1, -1, -1, -2, 0, 7 ]
This array is coerced to a string, which gives:
"NaN,-1,-1,-1,-1,-1,-2,0,7"
We apply the following regular expression:
+-----------------------> the second 'N' of 'NaN'
| +------------------> a sequence of negative numbers
| | +------------> a sequence of zeros
| | | +------> a sequence of positive numbers
| | | | +---> end of string
| | | | |
|/¨¨¨¨¨¨/¨¨¨/¨¨¨¨|
/N(,-d+)*(,0)*[^0-]*$/
Finally, we also test if the last digit p
is equal to the first digit s[0]
.
JavaScript (ES6), 62 54 bytes
Takes input as a string. Returns a Boolean value.
s=>s[-[...s].some(p=q=n=>q>(q=Math.sign(p-(p=n))))]==p
Try it online!
Commented
s => // s = input string
s[ // we will eventually access either s[0] or s[-1]
-[...s].some( // depending on the result of this some()
p = q = // initialize p and q to non-numeric values
n => // for each digit n:
q > ( // compare q with
q = // the new value of q,
Math.sign( // defined as the sign of
p - (p = n) // the difference between the current digit and the previous one
)) // yield true if the previous q is greater than the new q
) // s[-1] being undefined, a truhty some() will force the test to fail
] == p // otherwise: test if the 1st digit s[0] is equal to the last digit p
JavaScript (ES6), 65 bytes
A solution using a regular expression. Takes input as a string. Returns $0$ or $1$.
s=>/N(,-d+)*(,0)*[^0-]*$/.test([...s].map(p=v=>p-(p=v)))&p==s[0]
Try it online!
How?
We first convert the number to a list of pairwise digit differences in $[-9,9]$:
[...s].map(p = v => p - (p = v))
Example:
"234567992" --> [ NaN, -1, -1, -1, -1, -1, -2, 0, 7 ]
This array is coerced to a string, which gives:
"NaN,-1,-1,-1,-1,-1,-2,0,7"
We apply the following regular expression:
+-----------------------> the second 'N' of 'NaN'
| +------------------> a sequence of negative numbers
| | +------------> a sequence of zeros
| | | +------> a sequence of positive numbers
| | | | +---> end of string
| | | | |
|/¨¨¨¨¨¨/¨¨¨/¨¨¨¨|
/N(,-d+)*(,0)*[^0-]*$/
Finally, we also test if the last digit p
is equal to the first digit s[0]
.
edited 23 hours ago
answered yesterday
Arnauld
69.2k585293
69.2k585293
You can save 5 bytes by taking input as an array of digits.
– Shaggy
19 hours ago
@Shaggy I wish I could but this is apparently not allowed .
– Arnauld
19 hours ago
From the spec, with original emphasis: "Input & output can be in any reasonable format" - we usually consider a digit array a reasonable format for an integer.
– Shaggy
19 hours ago
@J.Doe The OP hasn't confirmed yet if numbers such as 222222222 are hill numbers or not. (If not, I think several answers are going to be invalidated.)
– Arnauld
13 hours ago
add a comment |
You can save 5 bytes by taking input as an array of digits.
– Shaggy
19 hours ago
@Shaggy I wish I could but this is apparently not allowed .
– Arnauld
19 hours ago
From the spec, with original emphasis: "Input & output can be in any reasonable format" - we usually consider a digit array a reasonable format for an integer.
– Shaggy
19 hours ago
@J.Doe The OP hasn't confirmed yet if numbers such as 222222222 are hill numbers or not. (If not, I think several answers are going to be invalidated.)
– Arnauld
13 hours ago
You can save 5 bytes by taking input as an array of digits.
– Shaggy
19 hours ago
You can save 5 bytes by taking input as an array of digits.
– Shaggy
19 hours ago
@Shaggy I wish I could but this is apparently not allowed .
– Arnauld
19 hours ago
@Shaggy I wish I could but this is apparently not allowed .
– Arnauld
19 hours ago
From the spec, with original emphasis: "Input & output can be in any reasonable format" - we usually consider a digit array a reasonable format for an integer.
– Shaggy
19 hours ago
From the spec, with original emphasis: "Input & output can be in any reasonable format" - we usually consider a digit array a reasonable format for an integer.
– Shaggy
19 hours ago
@J.Doe The OP hasn't confirmed yet if numbers such as 222222222 are hill numbers or not. (If not, I think several answers are going to be invalidated.)
– Arnauld
13 hours ago
@J.Doe The OP hasn't confirmed yet if numbers such as 222222222 are hill numbers or not. (If not, I think several answers are going to be invalidated.)
– Arnauld
13 hours ago
add a comment |
up vote
3
down vote
Python 2, 114 112 bytes
lambda n:all((n[0]==n[-1])*sorted(set(x))==list(x)[::d]for x,d in zip(n.split(max(n)*n.count(max(n)),1),[1,-1]))
Try it online!
add a comment |
up vote
3
down vote
Python 2, 114 112 bytes
lambda n:all((n[0]==n[-1])*sorted(set(x))==list(x)[::d]for x,d in zip(n.split(max(n)*n.count(max(n)),1),[1,-1]))
Try it online!
add a comment |
up vote
3
down vote
up vote
3
down vote
Python 2, 114 112 bytes
lambda n:all((n[0]==n[-1])*sorted(set(x))==list(x)[::d]for x,d in zip(n.split(max(n)*n.count(max(n)),1),[1,-1]))
Try it online!
Python 2, 114 112 bytes
lambda n:all((n[0]==n[-1])*sorted(set(x))==list(x)[::d]for x,d in zip(n.split(max(n)*n.count(max(n)),1),[1,-1]))
Try it online!
answered yesterday
TFeld
13.5k21139
13.5k21139
add a comment |
add a comment |
up vote
3
down vote
Perl 6, 39 bytes
{.[0]==.tail&&[<=] $_ Z<=>.skip}o*.comb
Try it online!
Explanation
{ ... }o.comb # Split into digits and feed into block
.[0]==.tail # First element equals last
&& # and
$_ Z<=>.skip # Pairwise application of three-way comparator
[<=] # Results never decrease
I was literally seconds away from posting this lol.
– Jo King
yesterday
add a comment |
up vote
3
down vote
Perl 6, 39 bytes
{.[0]==.tail&&[<=] $_ Z<=>.skip}o*.comb
Try it online!
Explanation
{ ... }o.comb # Split into digits and feed into block
.[0]==.tail # First element equals last
&& # and
$_ Z<=>.skip # Pairwise application of three-way comparator
[<=] # Results never decrease
I was literally seconds away from posting this lol.
– Jo King
yesterday
add a comment |
up vote
3
down vote
up vote
3
down vote
Perl 6, 39 bytes
{.[0]==.tail&&[<=] $_ Z<=>.skip}o*.comb
Try it online!
Explanation
{ ... }o.comb # Split into digits and feed into block
.[0]==.tail # First element equals last
&& # and
$_ Z<=>.skip # Pairwise application of three-way comparator
[<=] # Results never decrease
Perl 6, 39 bytes
{.[0]==.tail&&[<=] $_ Z<=>.skip}o*.comb
Try it online!
Explanation
{ ... }o.comb # Split into digits and feed into block
.[0]==.tail # First element equals last
&& # and
$_ Z<=>.skip # Pairwise application of three-way comparator
[<=] # Results never decrease
answered yesterday
nwellnhof
5,9981123
5,9981123
I was literally seconds away from posting this lol.
– Jo King
yesterday
add a comment |
I was literally seconds away from posting this lol.
– Jo King
yesterday
I was literally seconds away from posting this lol.
– Jo King
yesterday
I was literally seconds away from posting this lol.
– Jo King
yesterday
add a comment |
up vote
2
down vote
05AB1E, 19 17 13 12 bytes
¥D.±Â{RQsO_*
-5 bytes by creating a port of @lirtosiast's Pyth answer.
Try it online or verify all test cases.
Explanation:
¥ # Push the deltas of the digits of the (implicit) input
# i.e. 4588774 → [1,3,0,-1,0,-3]
D # Duplicate this list
.± # Get the sign of each
# [1,3,0,-1,0,-3] → [1,1,0,-1,0,-1]
 # Bifurcate (short for DR: Duplicate and Reverse copy)
# i.e. [1,1,0,-1,0,-1] → [-1,0,-1,0,1,1]
{ # Sort the copy
# i.e. [-1,0,-1,0,1,1] → [-1,-1,0,0,1,1]
R # Reverse it
# i.e. [1,1,0,0,-1,-1]
Q # And check if they are equal
# i.e. [1,1,0,-1,0,-1] and [1,1,0,0,-1,-1] → 0 (falsey)
s # Swap to get the list of deltas again
O # Take the sum
# i.e. [1,3,0,-1,0,-3] → 0
_ # And check if it's exactly 0
# 0 → 1 (truthy)
* # Check if both are truthy (and output implicitly)
# i.e. 0 and 1 → 0 (falsey)
Â{RQ
can alternatively be (Â{Q
for the same byte-count, where (
negates each sign: Try it online.
add a comment |
up vote
2
down vote
05AB1E, 19 17 13 12 bytes
¥D.±Â{RQsO_*
-5 bytes by creating a port of @lirtosiast's Pyth answer.
Try it online or verify all test cases.
Explanation:
¥ # Push the deltas of the digits of the (implicit) input
# i.e. 4588774 → [1,3,0,-1,0,-3]
D # Duplicate this list
.± # Get the sign of each
# [1,3,0,-1,0,-3] → [1,1,0,-1,0,-1]
 # Bifurcate (short for DR: Duplicate and Reverse copy)
# i.e. [1,1,0,-1,0,-1] → [-1,0,-1,0,1,1]
{ # Sort the copy
# i.e. [-1,0,-1,0,1,1] → [-1,-1,0,0,1,1]
R # Reverse it
# i.e. [1,1,0,0,-1,-1]
Q # And check if they are equal
# i.e. [1,1,0,-1,0,-1] and [1,1,0,0,-1,-1] → 0 (falsey)
s # Swap to get the list of deltas again
O # Take the sum
# i.e. [1,3,0,-1,0,-3] → 0
_ # And check if it's exactly 0
# 0 → 1 (truthy)
* # Check if both are truthy (and output implicitly)
# i.e. 0 and 1 → 0 (falsey)
Â{RQ
can alternatively be (Â{Q
for the same byte-count, where (
negates each sign: Try it online.
add a comment |
up vote
2
down vote
up vote
2
down vote
05AB1E, 19 17 13 12 bytes
¥D.±Â{RQsO_*
-5 bytes by creating a port of @lirtosiast's Pyth answer.
Try it online or verify all test cases.
Explanation:
¥ # Push the deltas of the digits of the (implicit) input
# i.e. 4588774 → [1,3,0,-1,0,-3]
D # Duplicate this list
.± # Get the sign of each
# [1,3,0,-1,0,-3] → [1,1,0,-1,0,-1]
 # Bifurcate (short for DR: Duplicate and Reverse copy)
# i.e. [1,1,0,-1,0,-1] → [-1,0,-1,0,1,1]
{ # Sort the copy
# i.e. [-1,0,-1,0,1,1] → [-1,-1,0,0,1,1]
R # Reverse it
# i.e. [1,1,0,0,-1,-1]
Q # And check if they are equal
# i.e. [1,1,0,-1,0,-1] and [1,1,0,0,-1,-1] → 0 (falsey)
s # Swap to get the list of deltas again
O # Take the sum
# i.e. [1,3,0,-1,0,-3] → 0
_ # And check if it's exactly 0
# 0 → 1 (truthy)
* # Check if both are truthy (and output implicitly)
# i.e. 0 and 1 → 0 (falsey)
Â{RQ
can alternatively be (Â{Q
for the same byte-count, where (
negates each sign: Try it online.
05AB1E, 19 17 13 12 bytes
¥D.±Â{RQsO_*
-5 bytes by creating a port of @lirtosiast's Pyth answer.
Try it online or verify all test cases.
Explanation:
¥ # Push the deltas of the digits of the (implicit) input
# i.e. 4588774 → [1,3,0,-1,0,-3]
D # Duplicate this list
.± # Get the sign of each
# [1,3,0,-1,0,-3] → [1,1,0,-1,0,-1]
 # Bifurcate (short for DR: Duplicate and Reverse copy)
# i.e. [1,1,0,-1,0,-1] → [-1,0,-1,0,1,1]
{ # Sort the copy
# i.e. [-1,0,-1,0,1,1] → [-1,-1,0,0,1,1]
R # Reverse it
# i.e. [1,1,0,0,-1,-1]
Q # And check if they are equal
# i.e. [1,1,0,-1,0,-1] and [1,1,0,0,-1,-1] → 0 (falsey)
s # Swap to get the list of deltas again
O # Take the sum
# i.e. [1,3,0,-1,0,-3] → 0
_ # And check if it's exactly 0
# 0 → 1 (truthy)
* # Check if both are truthy (and output implicitly)
# i.e. 0 and 1 → 0 (falsey)
Â{RQ
can alternatively be (Â{Q
for the same byte-count, where (
negates each sign: Try it online.
edited yesterday
answered yesterday
Kevin Cruijssen
34.1k554181
34.1k554181
add a comment |
add a comment |
up vote
1
down vote
J, 23 bytes
[:((0=+/)**-:*/:*)2-/]
Idea stolen from the Jelly answers. Just wanted to see how short I could make it in J.
Try it online!
add a comment |
up vote
1
down vote
J, 23 bytes
[:((0=+/)**-:*/:*)2-/]
Idea stolen from the Jelly answers. Just wanted to see how short I could make it in J.
Try it online!
add a comment |
up vote
1
down vote
up vote
1
down vote
J, 23 bytes
[:((0=+/)**-:*/:*)2-/]
Idea stolen from the Jelly answers. Just wanted to see how short I could make it in J.
Try it online!
J, 23 bytes
[:((0=+/)**-:*/:*)2-/]
Idea stolen from the Jelly answers. Just wanted to see how short I could make it in J.
Try it online!
edited 17 hours ago
answered 17 hours ago
Jonah
1,941816
1,941816
add a comment |
add a comment |
up vote
1
down vote
R, 65 bytes
Takes strings. Took the idea for checking sort invariance from the Pyth answer.
function(a)!sum(d<-diff(utf8ToInt(a)))&all(sort(k<-sign(d),T)==k)
Try it online!
add a comment |
up vote
1
down vote
R, 65 bytes
Takes strings. Took the idea for checking sort invariance from the Pyth answer.
function(a)!sum(d<-diff(utf8ToInt(a)))&all(sort(k<-sign(d),T)==k)
Try it online!
add a comment |
up vote
1
down vote
up vote
1
down vote
R, 65 bytes
Takes strings. Took the idea for checking sort invariance from the Pyth answer.
function(a)!sum(d<-diff(utf8ToInt(a)))&all(sort(k<-sign(d),T)==k)
Try it online!
R, 65 bytes
Takes strings. Took the idea for checking sort invariance from the Pyth answer.
function(a)!sum(d<-diff(utf8ToInt(a)))&all(sort(k<-sign(d),T)==k)
Try it online!
edited 7 hours ago
answered 14 hours ago
J.Doe
1,961112
1,961112
add a comment |
add a comment |
up vote
0
down vote
Retina 0.8.2, 52 bytes
.
$*1;$&$*1,
(1+),1
,
^(1+);(,1+;)*(,;)*(1+,;)*1,$
Try it online! Link includes test cases. Explanation:
.
$*1;$&$*1,
Convert each digit to unary twice, separated by ;
s and terminated by ,
s. However, you can then think of the result as the first digit, a ;
, then all the pairs of adjacent digits, the digits of each pair separated by ,
and the pairs separated by ;
s, then another ;
, then the last digit, then a final ,
.
(1+),1
,
Subtract the pairs of adjacent digits. This leaves ;,;
for equal digits and 1
s on the greater side for unequal digits. (This could be done as part of the following regex but obviously that wouldn't be so golfy.)
^(1+);(,1+;)*(,;)*(1+,;)*1,$
Match the first digit, then any number of pairs of ascending digits, then any number of pairs of equal digits, then any number of pairs of descending digits, then match the first digit again at the very end.
add a comment |
up vote
0
down vote
Retina 0.8.2, 52 bytes
.
$*1;$&$*1,
(1+),1
,
^(1+);(,1+;)*(,;)*(1+,;)*1,$
Try it online! Link includes test cases. Explanation:
.
$*1;$&$*1,
Convert each digit to unary twice, separated by ;
s and terminated by ,
s. However, you can then think of the result as the first digit, a ;
, then all the pairs of adjacent digits, the digits of each pair separated by ,
and the pairs separated by ;
s, then another ;
, then the last digit, then a final ,
.
(1+),1
,
Subtract the pairs of adjacent digits. This leaves ;,;
for equal digits and 1
s on the greater side for unequal digits. (This could be done as part of the following regex but obviously that wouldn't be so golfy.)
^(1+);(,1+;)*(,;)*(1+,;)*1,$
Match the first digit, then any number of pairs of ascending digits, then any number of pairs of equal digits, then any number of pairs of descending digits, then match the first digit again at the very end.
add a comment |
up vote
0
down vote
up vote
0
down vote
Retina 0.8.2, 52 bytes
.
$*1;$&$*1,
(1+),1
,
^(1+);(,1+;)*(,;)*(1+,;)*1,$
Try it online! Link includes test cases. Explanation:
.
$*1;$&$*1,
Convert each digit to unary twice, separated by ;
s and terminated by ,
s. However, you can then think of the result as the first digit, a ;
, then all the pairs of adjacent digits, the digits of each pair separated by ,
and the pairs separated by ;
s, then another ;
, then the last digit, then a final ,
.
(1+),1
,
Subtract the pairs of adjacent digits. This leaves ;,;
for equal digits and 1
s on the greater side for unequal digits. (This could be done as part of the following regex but obviously that wouldn't be so golfy.)
^(1+);(,1+;)*(,;)*(1+,;)*1,$
Match the first digit, then any number of pairs of ascending digits, then any number of pairs of equal digits, then any number of pairs of descending digits, then match the first digit again at the very end.
Retina 0.8.2, 52 bytes
.
$*1;$&$*1,
(1+),1
,
^(1+);(,1+;)*(,;)*(1+,;)*1,$
Try it online! Link includes test cases. Explanation:
.
$*1;$&$*1,
Convert each digit to unary twice, separated by ;
s and terminated by ,
s. However, you can then think of the result as the first digit, a ;
, then all the pairs of adjacent digits, the digits of each pair separated by ,
and the pairs separated by ;
s, then another ;
, then the last digit, then a final ,
.
(1+),1
,
Subtract the pairs of adjacent digits. This leaves ;,;
for equal digits and 1
s on the greater side for unequal digits. (This could be done as part of the following regex but obviously that wouldn't be so golfy.)
^(1+);(,1+;)*(,;)*(1+,;)*1,$
Match the first digit, then any number of pairs of ascending digits, then any number of pairs of equal digits, then any number of pairs of descending digits, then match the first digit again at the very end.
answered 21 hours ago
Neil
78k744175
78k744175
add a comment |
add a comment |
up vote
0
down vote
Japt, 11 bytes
Takes input as a digit array.
ä-
eUñg)«Ux
Try it or run all test cases
add a comment |
up vote
0
down vote
Japt, 11 bytes
Takes input as a digit array.
ä-
eUñg)«Ux
Try it or run all test cases
add a comment |
up vote
0
down vote
up vote
0
down vote
Japt, 11 bytes
Takes input as a digit array.
ä-
eUñg)«Ux
Try it or run all test cases
Japt, 11 bytes
Takes input as a digit array.
ä-
eUñg)«Ux
Try it or run all test cases
answered 20 hours ago
Shaggy
18.1k21663
18.1k21663
add a comment |
add a comment |
up vote
0
down vote
Mathematica/Wolfram Language, 69 bytes
Pure function. Takes input as an integer, returns True
or False
.
Sort[x=Sign[-Differences[y=IntegerDigits[#]]]]==x&&First[y]==Last[y]&
Explanation:
The first clause checks the "hilliness":
IntegerDigits
: Get digits from integer. Store iny
.
-Differences
: Take successive differences and flip signs.
Sign
: Replace each entry with +1 if positive, 0 if zero, and -1 if negative. Store inx
.
Sort
: Sort list of +1, 0, -1 from smallest to largest. Compare to original list inx
.
The second clause checks whether the first and last digits are equal.
The fact thatIntegerDigits
andDifferences
are rather long function names is a bit annoying.
– Michael Seifert
19 hours ago
add a comment |
up vote
0
down vote
Mathematica/Wolfram Language, 69 bytes
Pure function. Takes input as an integer, returns True
or False
.
Sort[x=Sign[-Differences[y=IntegerDigits[#]]]]==x&&First[y]==Last[y]&
Explanation:
The first clause checks the "hilliness":
IntegerDigits
: Get digits from integer. Store iny
.
-Differences
: Take successive differences and flip signs.
Sign
: Replace each entry with +1 if positive, 0 if zero, and -1 if negative. Store inx
.
Sort
: Sort list of +1, 0, -1 from smallest to largest. Compare to original list inx
.
The second clause checks whether the first and last digits are equal.
The fact thatIntegerDigits
andDifferences
are rather long function names is a bit annoying.
– Michael Seifert
19 hours ago
add a comment |
up vote
0
down vote
up vote
0
down vote
Mathematica/Wolfram Language, 69 bytes
Pure function. Takes input as an integer, returns True
or False
.
Sort[x=Sign[-Differences[y=IntegerDigits[#]]]]==x&&First[y]==Last[y]&
Explanation:
The first clause checks the "hilliness":
IntegerDigits
: Get digits from integer. Store iny
.
-Differences
: Take successive differences and flip signs.
Sign
: Replace each entry with +1 if positive, 0 if zero, and -1 if negative. Store inx
.
Sort
: Sort list of +1, 0, -1 from smallest to largest. Compare to original list inx
.
The second clause checks whether the first and last digits are equal.
Mathematica/Wolfram Language, 69 bytes
Pure function. Takes input as an integer, returns True
or False
.
Sort[x=Sign[-Differences[y=IntegerDigits[#]]]]==x&&First[y]==Last[y]&
Explanation:
The first clause checks the "hilliness":
IntegerDigits
: Get digits from integer. Store iny
.
-Differences
: Take successive differences and flip signs.
Sign
: Replace each entry with +1 if positive, 0 if zero, and -1 if negative. Store inx
.
Sort
: Sort list of +1, 0, -1 from smallest to largest. Compare to original list inx
.
The second clause checks whether the first and last digits are equal.
answered 19 hours ago
Michael Seifert
1514
1514
The fact thatIntegerDigits
andDifferences
are rather long function names is a bit annoying.
– Michael Seifert
19 hours ago
add a comment |
The fact thatIntegerDigits
andDifferences
are rather long function names is a bit annoying.
– Michael Seifert
19 hours ago
The fact that
IntegerDigits
and Differences
are rather long function names is a bit annoying.– Michael Seifert
19 hours ago
The fact that
IntegerDigits
and Differences
are rather long function names is a bit annoying.– Michael Seifert
19 hours ago
add a comment |
up vote
0
down vote
Red, 181 bytes
func[n][m: last sort copy t: s: form n
parse t[opt[copy a to m(a: sort unique a)]copy b thru any m
opt[copy c to end(c: sort/reverse unique c)]](s = rejoin[a b c])and(s/1 = last s)]
Try it online!
More readable:
f: func[n][
t: s: form n
m: last sort copy t
parse t [ opt [ copy a to m (a: sort unique a) ]
copy b thru any m
opt [ copy c to end (c: sort/reverse unique c) ]
]
(s = rejoin [ a b c ]) and (s/1 = last s)
]
add a comment |
up vote
0
down vote
Red, 181 bytes
func[n][m: last sort copy t: s: form n
parse t[opt[copy a to m(a: sort unique a)]copy b thru any m
opt[copy c to end(c: sort/reverse unique c)]](s = rejoin[a b c])and(s/1 = last s)]
Try it online!
More readable:
f: func[n][
t: s: form n
m: last sort copy t
parse t [ opt [ copy a to m (a: sort unique a) ]
copy b thru any m
opt [ copy c to end (c: sort/reverse unique c) ]
]
(s = rejoin [ a b c ]) and (s/1 = last s)
]
add a comment |
up vote
0
down vote
up vote
0
down vote
Red, 181 bytes
func[n][m: last sort copy t: s: form n
parse t[opt[copy a to m(a: sort unique a)]copy b thru any m
opt[copy c to end(c: sort/reverse unique c)]](s = rejoin[a b c])and(s/1 = last s)]
Try it online!
More readable:
f: func[n][
t: s: form n
m: last sort copy t
parse t [ opt [ copy a to m (a: sort unique a) ]
copy b thru any m
opt [ copy c to end (c: sort/reverse unique c) ]
]
(s = rejoin [ a b c ]) and (s/1 = last s)
]
Red, 181 bytes
func[n][m: last sort copy t: s: form n
parse t[opt[copy a to m(a: sort unique a)]copy b thru any m
opt[copy c to end(c: sort/reverse unique c)]](s = rejoin[a b c])and(s/1 = last s)]
Try it online!
More readable:
f: func[n][
t: s: form n
m: last sort copy t
parse t [ opt [ copy a to m (a: sort unique a) ]
copy b thru any m
opt [ copy c to end (c: sort/reverse unique c) ]
]
(s = rejoin [ a b c ]) and (s/1 = last s)
]
answered 19 hours ago
Galen Ivanov
5,90711032
5,90711032
add a comment |
add a comment |
up vote
0
down vote
Powershell, 77 bytes
($x=-join("$($args|%{"-$_;$_"})"|iex))-match'^(-d)+0*d+$'-and$x[1]-eq$x[-1]
Less golfed test script:
$f = {
# $args = 1,2,3,3,3,2,1
$a=$args|%{"-$_;$_"} # "-1;1","-2;2","-3;3","-3;3","-3;3","-2;2","-1;1"
$d="$a" # "-1;1 -2;2 -3;3 -3;3 -3;3 -2;2 -1;1"
$x=-join($d|Invoke-Expression) # "-1-1-100111"
$x-match'^(-d)+0*d+$'-and$x[1]-eq$x[-1] # $true or $false
}
@(
,($True , 1,2,3,2,1 )
,($True , 1,2,3,3,3,2,1 )
,($True , 9,9 )
,($True , 3 )
,($True , 2,3,4,5,6,7,9,9,2 )
,($False, 1,2,3,2 )
,($False, 7,7,8,8,9,6 )
,($False, 2,3,2,3,2 )
,($False, 4,5,5,6,6,5,5,4 )
,($False, 5,6,4,5 )
) | % {
$expected,$a = $_
$result = &$f @a
"$($result-eq$expected): $result"
}
Output:
True: True
True: True
True: True
True: True
True: True
True: False
True: False
True: False
True: False
True: False
add a comment |
up vote
0
down vote
Powershell, 77 bytes
($x=-join("$($args|%{"-$_;$_"})"|iex))-match'^(-d)+0*d+$'-and$x[1]-eq$x[-1]
Less golfed test script:
$f = {
# $args = 1,2,3,3,3,2,1
$a=$args|%{"-$_;$_"} # "-1;1","-2;2","-3;3","-3;3","-3;3","-2;2","-1;1"
$d="$a" # "-1;1 -2;2 -3;3 -3;3 -3;3 -2;2 -1;1"
$x=-join($d|Invoke-Expression) # "-1-1-100111"
$x-match'^(-d)+0*d+$'-and$x[1]-eq$x[-1] # $true or $false
}
@(
,($True , 1,2,3,2,1 )
,($True , 1,2,3,3,3,2,1 )
,($True , 9,9 )
,($True , 3 )
,($True , 2,3,4,5,6,7,9,9,2 )
,($False, 1,2,3,2 )
,($False, 7,7,8,8,9,6 )
,($False, 2,3,2,3,2 )
,($False, 4,5,5,6,6,5,5,4 )
,($False, 5,6,4,5 )
) | % {
$expected,$a = $_
$result = &$f @a
"$($result-eq$expected): $result"
}
Output:
True: True
True: True
True: True
True: True
True: True
True: False
True: False
True: False
True: False
True: False
add a comment |
up vote
0
down vote
up vote
0
down vote
Powershell, 77 bytes
($x=-join("$($args|%{"-$_;$_"})"|iex))-match'^(-d)+0*d+$'-and$x[1]-eq$x[-1]
Less golfed test script:
$f = {
# $args = 1,2,3,3,3,2,1
$a=$args|%{"-$_;$_"} # "-1;1","-2;2","-3;3","-3;3","-3;3","-2;2","-1;1"
$d="$a" # "-1;1 -2;2 -3;3 -3;3 -3;3 -2;2 -1;1"
$x=-join($d|Invoke-Expression) # "-1-1-100111"
$x-match'^(-d)+0*d+$'-and$x[1]-eq$x[-1] # $true or $false
}
@(
,($True , 1,2,3,2,1 )
,($True , 1,2,3,3,3,2,1 )
,($True , 9,9 )
,($True , 3 )
,($True , 2,3,4,5,6,7,9,9,2 )
,($False, 1,2,3,2 )
,($False, 7,7,8,8,9,6 )
,($False, 2,3,2,3,2 )
,($False, 4,5,5,6,6,5,5,4 )
,($False, 5,6,4,5 )
) | % {
$expected,$a = $_
$result = &$f @a
"$($result-eq$expected): $result"
}
Output:
True: True
True: True
True: True
True: True
True: True
True: False
True: False
True: False
True: False
True: False
Powershell, 77 bytes
($x=-join("$($args|%{"-$_;$_"})"|iex))-match'^(-d)+0*d+$'-and$x[1]-eq$x[-1]
Less golfed test script:
$f = {
# $args = 1,2,3,3,3,2,1
$a=$args|%{"-$_;$_"} # "-1;1","-2;2","-3;3","-3;3","-3;3","-2;2","-1;1"
$d="$a" # "-1;1 -2;2 -3;3 -3;3 -3;3 -2;2 -1;1"
$x=-join($d|Invoke-Expression) # "-1-1-100111"
$x-match'^(-d)+0*d+$'-and$x[1]-eq$x[-1] # $true or $false
}
@(
,($True , 1,2,3,2,1 )
,($True , 1,2,3,3,3,2,1 )
,($True , 9,9 )
,($True , 3 )
,($True , 2,3,4,5,6,7,9,9,2 )
,($False, 1,2,3,2 )
,($False, 7,7,8,8,9,6 )
,($False, 2,3,2,3,2 )
,($False, 4,5,5,6,6,5,5,4 )
,($False, 5,6,4,5 )
) | % {
$expected,$a = $_
$result = &$f @a
"$($result-eq$expected): $result"
}
Output:
True: True
True: True
True: True
True: True
True: True
True: False
True: False
True: False
True: False
True: False
edited 18 hours ago
answered 18 hours ago
mazzy
1,747313
1,747313
add a comment |
add a comment |
up vote
0
down vote
MATL, 12 bytes
dZSd1<AGds~*
Try it online!
(note: the TIO version uses H
instead of G
to process all test cases at once.)
Explanation
Input is a string of digits. Output is a 1
or 0
. The number 222222
is a hill number according to this program. Saved 2 bytes by copying Dennis' method for checking equality of the first and last digits.
d % Takes the difference between digits
ZS % Calculate the sign.
d % Take the difference again.
1<A % A number is a hill number if `A`ll these differences are < 1.
Gds % Push the input, and sum all the differences.
~ % Negate
* % Multiply the two tests (=logical AND).
add a comment |
up vote
0
down vote
MATL, 12 bytes
dZSd1<AGds~*
Try it online!
(note: the TIO version uses H
instead of G
to process all test cases at once.)
Explanation
Input is a string of digits. Output is a 1
or 0
. The number 222222
is a hill number according to this program. Saved 2 bytes by copying Dennis' method for checking equality of the first and last digits.
d % Takes the difference between digits
ZS % Calculate the sign.
d % Take the difference again.
1<A % A number is a hill number if `A`ll these differences are < 1.
Gds % Push the input, and sum all the differences.
~ % Negate
* % Multiply the two tests (=logical AND).
add a comment |
up vote
0
down vote
up vote
0
down vote
MATL, 12 bytes
dZSd1<AGds~*
Try it online!
(note: the TIO version uses H
instead of G
to process all test cases at once.)
Explanation
Input is a string of digits. Output is a 1
or 0
. The number 222222
is a hill number according to this program. Saved 2 bytes by copying Dennis' method for checking equality of the first and last digits.
d % Takes the difference between digits
ZS % Calculate the sign.
d % Take the difference again.
1<A % A number is a hill number if `A`ll these differences are < 1.
Gds % Push the input, and sum all the differences.
~ % Negate
* % Multiply the two tests (=logical AND).
MATL, 12 bytes
dZSd1<AGds~*
Try it online!
(note: the TIO version uses H
instead of G
to process all test cases at once.)
Explanation
Input is a string of digits. Output is a 1
or 0
. The number 222222
is a hill number according to this program. Saved 2 bytes by copying Dennis' method for checking equality of the first and last digits.
d % Takes the difference between digits
ZS % Calculate the sign.
d % Take the difference again.
1<A % A number is a hill number if `A`ll these differences are < 1.
Gds % Push the input, and sum all the differences.
~ % Negate
* % Multiply the two tests (=logical AND).
edited 3 hours ago
answered 3 hours ago
Sanchises
5,71212351
5,71212351
add a comment |
add a comment |
up vote
0
down vote
C# (Visual C# Interactive Compiler), 172 bytes
x=>{var s=x+"";var m=s.OrderBy(c=>c).Last();return s[0]==s.Last()&Enumerable.Range(1,s.Length-1).All(i=>i>s.LastIndexOf(m)?s[i-1]>s[i]:i>s.IndexOf(m)?m==s[i]:s[i-1]<s[i]);}
Try it online!
Here is an overview of how this works...
- Convert input
int
intostring
- Find the largest digit
- Ensure the first and last digits are the same
- Ensure digits after the last occurrence of the largest digit are decreasing
- Ensure digits between the first and last occurrence of the largest digit are equal to the largest digit
- Ensure digits before the first occurrence of the largest digit are increasing
add a comment |
up vote
0
down vote
C# (Visual C# Interactive Compiler), 172 bytes
x=>{var s=x+"";var m=s.OrderBy(c=>c).Last();return s[0]==s.Last()&Enumerable.Range(1,s.Length-1).All(i=>i>s.LastIndexOf(m)?s[i-1]>s[i]:i>s.IndexOf(m)?m==s[i]:s[i-1]<s[i]);}
Try it online!
Here is an overview of how this works...
- Convert input
int
intostring
- Find the largest digit
- Ensure the first and last digits are the same
- Ensure digits after the last occurrence of the largest digit are decreasing
- Ensure digits between the first and last occurrence of the largest digit are equal to the largest digit
- Ensure digits before the first occurrence of the largest digit are increasing
add a comment |
up vote
0
down vote
up vote
0
down vote
C# (Visual C# Interactive Compiler), 172 bytes
x=>{var s=x+"";var m=s.OrderBy(c=>c).Last();return s[0]==s.Last()&Enumerable.Range(1,s.Length-1).All(i=>i>s.LastIndexOf(m)?s[i-1]>s[i]:i>s.IndexOf(m)?m==s[i]:s[i-1]<s[i]);}
Try it online!
Here is an overview of how this works...
- Convert input
int
intostring
- Find the largest digit
- Ensure the first and last digits are the same
- Ensure digits after the last occurrence of the largest digit are decreasing
- Ensure digits between the first and last occurrence of the largest digit are equal to the largest digit
- Ensure digits before the first occurrence of the largest digit are increasing
C# (Visual C# Interactive Compiler), 172 bytes
x=>{var s=x+"";var m=s.OrderBy(c=>c).Last();return s[0]==s.Last()&Enumerable.Range(1,s.Length-1).All(i=>i>s.LastIndexOf(m)?s[i-1]>s[i]:i>s.IndexOf(m)?m==s[i]:s[i-1]<s[i]);}
Try it online!
Here is an overview of how this works...
- Convert input
int
intostring
- Find the largest digit
- Ensure the first and last digits are the same
- Ensure digits after the last occurrence of the largest digit are decreasing
- Ensure digits between the first and last occurrence of the largest digit are equal to the largest digit
- Ensure digits before the first occurrence of the largest digit are increasing
edited 18 mins ago
answered 2 hours ago
dana
1714
1714
add a comment |
add a comment |
up vote
0
down vote
Python 2, 62 bytes
lambda s:s[:eval('<='.join(map(str,map(cmp,s,s[1:]))))]==s[-1]
Takes input as a string and returns a Boolean.
Try it online!
add a comment |
up vote
0
down vote
Python 2, 62 bytes
lambda s:s[:eval('<='.join(map(str,map(cmp,s,s[1:]))))]==s[-1]
Takes input as a string and returns a Boolean.
Try it online!
add a comment |
up vote
0
down vote
up vote
0
down vote
Python 2, 62 bytes
lambda s:s[:eval('<='.join(map(str,map(cmp,s,s[1:]))))]==s[-1]
Takes input as a string and returns a Boolean.
Try it online!
Python 2, 62 bytes
lambda s:s[:eval('<='.join(map(str,map(cmp,s,s[1:]))))]==s[-1]
Takes input as a string and returns a Boolean.
Try it online!
answered 12 mins ago
Dennis♦
184k32293729
184k32293729
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f176224%2fis-this-number-a-hill-number%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
5
What about
222222222
? Is it a flat hill number?– frarugi87
23 hours ago
222222222
is a hill number, largest digit is 2 and thus can be repeated– u_ndefined
1 hour ago
Is a string reasonable?
– Sanchises
1 hour ago
@Sanchises yes, but list of chars is not
– u_ndefined
1 hour ago
@frarugi87 See comment above.
– Dennis♦
1 hour ago