44
3
Given an input string S, return truthy
if all the letters in S are Lexically Ordered: their ASCII values need to be in either ascending or descending order. Return falsy
in other cases.
Input
- Input will be in the same case (all upper- or all lowercase). Your submission should be able to handle both.
- Input will consist of ASCII in the range
[A-Za-z]
only - Input length will be at least 1, up to whatever maximum your language supports.
- Input is a string - not a list of characters, not an array of ASCII-codepoints.
Output
- Output should be
true
orfalse
, or0/1
, or any other distincttrue / false
style output your language can provide. - All true cases need to have the same output, as well as all the false cases. No "False is 0, true is 1, 2, or 3".
Additional rules
- Standard loopholes are forbidden
- Answer must be a full program or a function, not a snippet or a REPL-entry.
- code-golf, shortest answer in bytes wins.
Test cases
Truthy
"ABCDEF"
"ZYX"
"no"
"tree" --> the multiple 'e's don't break the order
"q"
Falsy
"ABCDC"
"yes"
"deed"
Invalid
"Hello" --> invalid input - mixed case-, does not have to be handled
"" --> invalid input - length 0-, does not have to be handled
"\n
" --> invalid input - newline is not in range [A-Za-z]-, does not have to be handled
1Can you clarify about the output: does the truthy value need be the same regardless of what input is given? – Business Cat – 2017-01-30T19:12:25.793
1@BusinessCat I've added a clarification. – steenbergh – 2017-01-30T20:01:44.117
What if your language's implementation of a string is a list of characters? Many of the answers posted here are using such languages... – theonlygusti – 2017-01-30T20:40:35.067
@theonlygusti If your language of choice treats strings as lists of characters, that's fine. If your language has a string type, input may not be given as a list. – steenbergh – 2017-01-30T21:35:06.997
1If you really want distinct values for True and False you shouldn't say
truthy
orfalsy
. This implies that any values that evaluate totrue
orfalse
are allowed. – FlipTack – 2017-01-30T21:50:47.980@FlipTack You're right. What I mean is that it doesn't matter what value resembles TRUE or FALSE, as long as it is always the same value. – steenbergh – 2017-01-30T21:53:19.270
3
related: Find the Wavy Words!
– Titus – 2017-01-30T22:25:46.127Has any one got to deal well with all the invalid values? I found no one in a quick look – Patrick Bard – 2017-02-01T16:47:17.623
@PatrickBard No, they're marked invalid because those cases do not need to be handled. Submissions need not concern themselves with those inputs. – steenbergh – 2017-02-01T16:49:33.867
@steenbergh I see, I understood this different the first time I read. My mistake. – Patrick Bard – 2017-02-01T17:08:09.610
Can we output
0
for ordered, and1
for not? – caird coinheringaahing – 2018-03-06T19:38:06.827@cairdcoinheringaahing Yes you can, as long as it's consistent between all cases. – steenbergh – 2018-03-07T06:49:47.830
Some more test cases:
deer
,AAAAA
,aabb
(we can't infer direction from only first or last pair, order has to continue after a double letter). – Toby Speight – 2018-03-08T16:13:40.800@TobySpeight all truthy, and imo all covered by existing cases. – steenbergh – 2018-03-08T16:29:21.767
Sorry, I should have said that they are all truthy, and that I meant them as stress-tests for solutions that take unjustified short-cuts to pass the suite. – Toby Speight – 2018-03-08T17:48:40.743
@TobySpeight do you have an example of a solution that does this? - I'm a little reluctant to change a question this old... – steenbergh – 2018-03-09T06:28:20.643
No, I spotted the problem before I posted my answer. – Toby Speight – 2018-03-09T08:59:56.407