Is this word Lexically Ordered?

Python 2, 53 44 40 39 bytes

lambda a:`sorted(a)`[2::5]in(a,a[::-1])

Try it online!


Haskell, 33 bytes

(%)=scanl1
f s=s==max%s||s==min%s

Try it online!

Thanks to Ørjan Johansen for 1 byte with aliasing scanl1 infix.

Haskell is an interesting language to golf sorting-based challenges because it does not have a built-in sort, barring a lengthy import Data.List. This encourages finding a way to do the task by hand without explicitly sorting.

The code uses scanl1, which folds an operation over the list from left to right, keeping track of the intermediate results. So, scanl1 max has the effect of listing the cumulative maxima of the list, i.e. the maxima of progressively longer prefixes. For example, scanl1 max [3,1,2,5,4] == [3,3,3,5,5].

The same with min checks whether the list is decreasing. The code checks the two cases and combines them with ||.

Compare to other expressions:

(%)=scanl1;f s=s==max%s||s==min%s

f s=or[s==scanl1 q s|q<-[min,max]]
f s=s==scanl1 max s||s==scanl1 min s
f s=any(\q->scanl1 q s==s)[min,max]
f s=any((==s).(`scanl1`s))[min,max]
f s=elem s$(`scanl1`s)<$>[min,max]

Perl 6, 25 bytes

{[le] .comb or[ge] .comb}

How it works:

  • .comb splits the input into a sequence of characters.
  • le and ge are the "less or equal" and "greater or equal" string comparison operators.
  • [ ] around an infix operator, reduces ("folds") the argument list with that operator. (It's smart enough to return True if the input has only zero or one characters.)
  • or returns True if the expressions on either side of it is true.