54
\$\begingroup\$

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 or false, or 0/1, or any other distinct true / 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.
  • , 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 
\$\endgroup\$
18
  • 1
    \$\begingroup\$ Can you clarify about the output: does the truthy value need be the same regardless of what input is given? \$\endgroup\$ Commented Jan 30, 2017 at 19:12
  • 1
    \$\begingroup\$ @BusinessCat I've added a clarification. \$\endgroup\$ Commented Jan 30, 2017 at 20:01
  • \$\begingroup\$ What if your language's implementation of a string is a list of characters? Many of the answers posted here are using such languages... \$\endgroup\$ Commented Jan 30, 2017 at 20:40
  • 1
    \$\begingroup\$ If you really want distinct values for True and False you shouldn't say truthy or falsy. This implies that any values that evaluate to true or false are allowed. \$\endgroup\$ Commented Jan 30, 2017 at 21:50
  • 3
    \$\begingroup\$ related: Find the Wavy Words! \$\endgroup\$ Commented Jan 30, 2017 at 22:25

70 Answers 70

1 2
3
1
\$\begingroup\$

Standard ML (MLton), 99 93 bytes

open Char fun g(x::y::r)c=c(x,y)andalso g(y::r)c|g&c=1=1 (fn$ =>g$op<=orelse g$op>=)o explode 

Try it online! The last line is an anonymous function. Example usage: ((fn$ =>g$op<=orelse g$op>=)o explode) "somestring". Returns true or false.

Ungolfed code:

open Char fun g (x::y::r) c = c(x,y) andalso g (y::r) c | g _ c = true fun h s = g s op<= orelse g s op>= val f = h o explode 
\$\endgroup\$
1
\$\begingroup\$

APL NARS 30 19 chars

{(⊂⍵[⍋⍵])∊(⊂⍵),⊂⌽⍵} 

{(⍵[⍋⍵]≡⍵[⍳⍴⍵])∨⍵[⍋⍵]≡⍵[⌽⍳⍴⍵]}

Copy the algo of 'Emigna' in APL...Test

 f¨'ABCDEF' 'ZYX' 'tree' 'q' 1 1 1 1 f¨'ABCDC' 'yes' 'deed' 0 0 0 
\$\endgroup\$
4
  • \$\begingroup\$ Couple of questions: 1) link to interpreter? and 2) bytecount? I get 66 bytes, not 30 chars... \$\endgroup\$ Commented Dec 18, 2017 at 6:50
  • \$\begingroup\$ @steenbergh the request origin of Nars Apl is here nars2000.org the char count is 30 characters; the bytes count would be here 69 bytes. Each character seems more than 2 bytes...In a different code page would be possibly 30 bytes; but the advantage to have Unicode seems to me > of win some codegolf competition \$\endgroup\$ Commented Dec 18, 2017 at 7:32
  • \$\begingroup\$ @steenbergh this seems to run tio.run/##SyzI0U2pTMzJT///P/… \$\endgroup\$ Commented Dec 18, 2017 at 7:55
  • \$\begingroup\$ 15: {⍵(⌽⍵)∊⍨⊂⍵[⍋⍵]} \$\endgroup\$ Commented Mar 6, 2018 at 22:41
1
\$\begingroup\$

q/kdb+, 20 bytes

Solution:

{x in(asc;desc)@\:x} 

Examples:

q){x in(asc;desc)@\:x}"tree" 1b q){x in(asc;desc)@\:x}"yes" 0b q){x in(asc;desc)@\:x}"ABCDEF" 1b 

Explanation:

{x in(asc;desc)@\:x} / the solution { } / anonymous lambda function x / implicit input @\: / apply (@) each left (\:) to x (asc;desc) / list of two functions, sort ascending and descending respectively x in / is x in either sorted/unsorted list? 

Notes:

Closest equivalent in K4 would be {x in x@(>:;<:)@\:x} for 20 bytes too...

\$\endgroup\$
1
\$\begingroup\$

APL (Dyalog Unicode), 9 bytesSBCS

∨/⍬∘⍋⍷⍒,⍋ 

Try it online!

∨/ Is it anywhere true that (lit. OR-reduction of)

⍬∘⍋ the indices (viz. those that would put the argument into its current order)

 are found in (each position of)

 the indices that would put the argument into descending order

, followed by

 the indices that would put the argument into ascending order?

\$\endgroup\$
1
\$\begingroup\$

Retina, 26 bytes

O`. V` $ ¶$+¶$+1¶ (.+)¶\1¶ 

Try it online!

I still haven't seen a Retina program anywhere making use of the history feature, so here it is.

Explanation

O`. 

Sort the input

V` 

Reverse it

$ ¶$+¶$+1¶ 

Append at the end of the string a newline , followed by the original input to the program $+, another newline, the result of the first stage $+1, and a final newline. What we get is "input sorted descending¶original input¶input sorted ascending¶"

(.+)¶\1¶ 

Count the pairs of consecutive identical lines. This can be either 0, if the input was unordered, or 1 if the input was ordered.

\$\endgroup\$
1
\$\begingroup\$

Uiua SBCS, 9 8 bytes

∊⊏⊟⊃⍖⍏.. 

Try it!

\$\endgroup\$
1
\$\begingroup\$

AArch64 machine code, 48 bytes

$ objdump -dh f.o f.o: file format elf64-littleaarch64 Sections: Idx Name Size VMA LMA File off Algn 0 .text 00000030 0000000000000000 0000000000000000 00000040 2**2 CONTENTS, ALLOC, LOAD, READONLY, CODE 1 .data 00000000 0000000000000000 0000000000000000 00000070 2**0 CONTENTS, ALLOC, LOAD, DATA 2 .bss 00000000 0000000000000000 0000000000000000 00000070 2**0 ALLOC Disassembly of section .text: 0000000000000000 <f>: 0: 38401509 ldrb w9, [x8], #1 4: d280002b mov x11, #0x1 // #1 8: d280002c mov x12, #0x1 // #1 c: 14000005 b 20 <f+0x20> 10: 6b0a013f cmp w9, w10 14: 9a8bc3eb csel x11, xzr, x11, gt 18: 9a8cb3ec csel x12, xzr, x12, lt // lt = tstop 1c: 2a0a03e9 mov w9, w10 20: 3840150a ldrb w10, [x8], #1 24: 35ffff6a cbnz w10, 10 <f+0x10> 28: aa0c0160 orr x0, x11, x12 2c: d65f03c0 ret 

Synopsis

adr x8,.L.string bl f // 1-bit value is stored to x0 ret .data: .L.string: .asciz "string" 

Algorithm

Kinda ported smls's Perl 6 answer. Here is equivalent one:

def f(s: str) -> bool: flag1, flag2 = True, True for a,b in zip(s[:-1], s[1:]): flag1 = False if a>b else flag1 flag2 = False if a<b else flag2 return flag1 or flag2 

Original source

/// fn (x8: [*:0]const u8) x0: u1 /// x0: 1 for truthy .globl f f: ldrb w9,[x8],1 mov x11,1; mov x12,1 b 2f 1: cmp w9,w10 csel x11,xzr,x11,gt csel x12,xzr,x12,lt mov w9,w10 2: ldrb w10,[x8],1 cbnz w10,1b orr x0,x11,x12 ret 

How I tested

$ as -o f.o f.s $ cat main.s //! Usage: $0 INPUT //! Exit status shows .globl _start _start: ldr x8,[sp,16] bl f mov x8,93 svc 0 $ as -o main.o main.s $ ld -o main main.o f.o $ for x in ABCDEF ZYX no tree q ABCDC yes deed; do ./main "$x"; echo "$x: $?"; done ABCDEF: 1 ZYX: 1 no: 1 tree: 1 q: 1 ABCDC: 0 yes: 0 deed: 0 $ 
\$\endgroup\$
1
\$\begingroup\$

JavaScript (Node.js), 42 bytes

([l,...x])=>x.some(c=>c!=l&&x-(x=l<(l=c))) 

Try it online!

1 extra byte if must match true and false

At least not longer than sort

\$\endgroup\$
1
\$\begingroup\$

Vyxal 3, 5 bytes

⇄⎂/S∨ 

Vyxal It Online!

⇄⎂/S∨­⁡​‎‏​⁢⁠⁡‌⁢​‎‎⁡⁠⁡‏‏​⁡⁠⁡‌⁣​‎‎⁡⁠⁢‏‏​⁡⁠⁡‌⁤​‎‎⁡⁠⁣‏⁠‎⁡⁠⁤‏‏​⁡⁠⁡‌⁢⁡​‎‎⁡⁠⁢⁡‏‏​⁡⁠⁡‌­ # ‎⁡bottom of stack has infinite copies of input ⇄ # ‎⁢reverse first ⎂ # ‎⁣to both the top 2: /S # ‎⁤are they invariant by sort? ∨ # ‎⁢⁡logical or 💎 

Created with the help of Luminespire.

<script type="vyxal3"> ⇄⎂/S∨ </script> <script> args=[["ABCEF"],["ZYX"],["no"],["tree"],["q"],["ABCDC"],["yes"],["deed"]] </script> <script src="https://themoonisacheese.github.io/snippeterpreter/snippet.js" type="module"/>

\$\endgroup\$
0
\$\begingroup\$

SmileBASIC, 91 89 84 bytes

DIM A[1]INPUT S$WHILE LEN(S$)>1PUSH A,ASC(POP(S$))-ASC(POP(S$))WEND?MIN(A)*MAX(A)==0 

Makes an array of the differences between consecutive characters (and an extra 0), and checks if either the min or max is 0.

\$\endgroup\$
1 2
3

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.