13
\$\begingroup\$

Your challenge is to write a program, function, etc. that calculates if the passed string is "in order". That means that the characters of the string have character codes that are in order from smallest to largest. The smallest char code must be the first. By that I mean lowest unicode codepoints to highest. It doesn't matter what code page you language uses.

You must return one value if the input is "in order" and another if it is not. The values must be distinct, but there is no other restriction on the output values. For example, you may print/return/output true for !1AQaq¡± (in order) and false for aq!QA. The two distinct values don't need to be truthy or falsy or anything like that, just two distinct values. Repeated strings (eg. aa) are in order.

You only need to support up to unicode U+007E (~) (ascii 126)

However, the characters of your program must itself be in order. Good luck and happy ing!

\$\endgroup\$
  • \$\begingroup\$ You don't need to have truthy/falsy values? two truthys would work? \$\endgroup\$ – Rɪᴋᴇʀ Apr 13 '17 at 22:14
  • \$\begingroup\$ Also, is the smallest char code always at the first char? Or can it be reversed? \$\endgroup\$ – Rɪᴋᴇʀ Apr 13 '17 at 22:14
  • 11
    \$\begingroup\$ Code-golf honestly seems like a poor win condition for this form of restricted source. Code bowling for most unique characters would be more interesting. \$\endgroup\$ – fəˈnɛtɪk Apr 13 '17 at 22:50
  • 1
    \$\begingroup\$ @Pavel Pop cons don't work well for achieving a particular task in a particular way. \$\endgroup\$ – Dennis Apr 14 '17 at 1:02
  • 2
    \$\begingroup\$ is repeated string in order? for example, is "aa" in order? \$\endgroup\$ – tsh Apr 14 '17 at 3:05
14
\$\begingroup\$

Brachylog, 2 bytes

.o

Try it online!

Explanation

.o
.   Assert that {the input} equals the output of the last command in the program
 o  Sort {the input}

As a full program, an assertion failure gives false., any successful run that doesn't violate any assertions gives true.

\$\endgroup\$
9
\$\begingroup\$

Jelly, 2 bytes

Ṣ⁼

Try it online!

Explanation

Ṣ⁼
Ṣ    Sort {the input}
 ⁼   Compare that to {the input} for equality of the whole structure

⁼Ṣ also has the right functionality ("compare the input to the sorted input"), so it was just a case of running the two programs on themselves to figure out which was in order (I certainly don't have the Unicode codepoints of this part of Jelly's weird character set memorized).

\$\endgroup\$
  • \$\begingroup\$ Your submission is wrong in Jelly encoding, it needs to be ⁼Ṣ instead. You can see Jelly's codepage here. \$\endgroup\$ – Erik the Outgolfer Apr 14 '17 at 8:33
  • \$\begingroup\$ @EriktheOutgolfer That is easily fixed; ⁼Ṣ does exactly the same as Ṣ⁼. \$\endgroup\$ – steenbergh Apr 14 '17 at 10:50
  • \$\begingroup\$ @steenbergh This submission, as it is right now, is invalid. Even though the fix is easy, it hasn't been applied yet, and it's discouraged for others to edit code. \$\endgroup\$ – Erik the Outgolfer Apr 14 '17 at 10:51
  • 1
    \$\begingroup\$ @EriktheOutgolfer Discussion about this answer is mainly about which codepage to use: the challenge references Unicode (though doesn't explicitly tell us to use it) and Jelly has its own codepage. Whatever the outcome, fixing this answer is trivial. Therefor, I wouldn't go as far as calling this answer 'invalid' - I wouldn't even downvote it in its current state. \$\endgroup\$ – steenbergh Apr 14 '17 at 10:59
  • \$\begingroup\$ @steenbergh I haven't downvoted either, I was just notifying ais523 :) \$\endgroup\$ – Erik the Outgolfer Apr 14 '17 at 11:00
9
\$\begingroup\$

MATL, 5 bytes

GGS\a

Outputs 0 if input is in order, 1 otherwise.

Try it online!

Explanation

This computes the modulus of (the code points of) each char from the input with that at the same index in the sorted input. The input is in order if and only if all results are 0.

For example, consider the input string BCD!. Sorting it gives '!BCD. The arrays of code points are respectively [66 67 68 33] and [33 66 67 68]. Computing the moduli gives [0 1 1 33], so the input is not in order. Note how some results can be 0 even if the values were not the same (here that happens at the first position), but that cannot happen in all entries unless the input is in order.

G     % Push input string
GS    % Push input string and sort it
\     % Modulus, element-wise. This gives all zeros iff the input was in order
a     % Any: gives 1 if any entry is non-zero. Implicitly display
\$\endgroup\$
7
\$\begingroup\$

05AB1E, 3 bytes

D{å

Try it online!

Explanation:

D     Duplicate stack
 {    Sort the copy
  å   Check if the sorted copy is a substring of the original
      This will only return 1 if the original input is sorted, 0 otherwise.
\$\endgroup\$
  • \$\begingroup\$ D{Q also works... \$\endgroup\$ – Neil A. May 17 '17 at 21:44
  • 1
    \$\begingroup\$ @NeilA.It might do the same, but the characters are not in order. Requirement is that your source code passes the same test as the data does. This should yield 1. \$\endgroup\$ – steenbergh May 18 '17 at 5:48
6
\$\begingroup\$

Pyke, 2 bytes

Sq

Try it online!

S  - sorted(input)
 q - ^ == input
\$\endgroup\$
5
\$\begingroup\$

2sable, 2 bytes

Try it online!

Explanation

{    # sorted input
 Ê   # not equals (implicit input)

Outputs 0 if it is order, else 1.

\$\endgroup\$
  • 1
    \$\begingroup\$ @steenbergh: Ah, missed that part of the challenge. \$\endgroup\$ – Emigna Apr 14 '17 at 12:08
  • 1
    \$\begingroup\$ @steenbergh: Fixed! Fortunately we could output any distinct values :) \$\endgroup\$ – Emigna Apr 14 '17 at 12:13
2
\$\begingroup\$

Pyth, 2 bytes

<S

False means sorted, True means unsorted.

Test suite

This was fairly nontrivial to come up with. The most obvious solution to this problem, without the restricted source, is SI, invariant under sorting. But that's not sorted. Then I thought of qS, which implicitly uses the input variable twice, checking if it's equal to its sorted self. But while q < s, q > S, so this didn't work either. But < comes before S, and the only way that the sorted version can not be less than the original is if the original was sorted, since the sorted version is the lexicographically minimal permutation of the elements.

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

CGL (CGL Golfing Language), 4 bytes (non-competing)

-:Sc

Explanation:

- Decrement the stack counter so the current stack is where input is put
: Split the first element of the current stack (input) into an array of single strings, make that the next stack, and increment the stack counter
S Sort the current stack
c Compare the current stack and the one before, push that to the next stack and increment the stack counter
(implicit) Output the first element of the current stack, true if in order, false if not.

Non-competing because :, S, and c were implemented after the creation of this challenge.

\$\endgroup\$
  • \$\begingroup\$ Is there proof that this language was created before this challenge? \$\endgroup\$ – Kritixi Lithos Apr 14 '17 at 12:33
  • \$\begingroup\$ @KritixiLithos yes, and it is technically a valid language, but I will make this non-competing because the functions necessary to complete this were made after this challenge. CGL is still a work in progress, and I am using challenges to show me what new functions should be added. \$\endgroup\$ – programmer5000 Apr 14 '17 at 12:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.