13
\$\begingroup\$

This is a challenge related to Write the shortest self-identifying program (a quine variant)

Write a function or a full program that compares lexicographically its source code and input, that is, output three distinct values of your choice telling if input / source code is Bigger, Equal or Less than source code / input.

Specifications

A Lexicographic order is a generalization of the alphabetical order of dictionaries to sequences of elements of an ordered set [Wikipedia].

In short it works just like dictionaries:
To determine if string A is greater than a string B

  • we compare the order of each first character (from the left).
    The string with bigger order element is greater.
  • If first character of each are equal we compare the second and so on.
  • If one string ends then that string is smaller.
  • If A = B they are equal.

The ordered set is the natural character encoding of the language you choose and your submission has to work correctly with it.
For example UTF or ASCII.

Examples

We use Less, Eq and Greater as output and ASCII as alphabet for clarity and simplicity.

source: "A"
input : "B"
output : Greater (input <=> source)

source: "a_X"
input : "A_x"
output : Greater (source <=> input)

source: "A,X"
input : "A,x"
output : Less (source <=> input)

source: "#!*"
input : " "
output : Greater (source <=> input)

source: "x!y"
input : "x!y"
output : Eq (source <=> input)

Rules

  • Standard rules apply and specifically reading your own source code is not allowed.

  • 0-length answers are disallowed.

  • input: a sequence of characters in any convenient format (string, list or array of characters, codepoints).
    You can handle empty input but it is not required.

  • output:
    Three distinct values of your choice meaning less equal and greater.
    You are free to choose to tell if input is <=> than source or if source is <=> than input, just be consistent and specify it.

  • This is , all usual rules apply.

Sandbox

\$\endgroup\$

9 Answers 9

3
\$\begingroup\$

R, 92 90 bytes

'->T;f=\\(a,b=paste(sQuote(T),T))(a>=b)+(a>b)' ->T;f=\(a,b=paste(sQuote(T),T))(a>=b)+(a>b)

Attempt This Online!

Outputs 2 if the input is greater than the code, 1 if they're equal and 0 otherwise.

Simple modification of @Dominic van Essen's R quine.

\$\endgroup\$
3
\$\begingroup\$

Jelly,  32  22 bytes

-10 thanks to Unrelated String! (Using M (-8) and using a full program (-2)).

“Ṿ;,ɠØJiⱮⱮM”Ṿ;,ɠØJiⱮⱮM

Expects the code using characters from the code-page from STDIN and prints 2 if equal to the code, 0 if less, or 1 if more.

* without the deprecated, but supported, alternative characters of and ṿ, or the newline alternative to .

Try it online!

How?

“Ṿ;,ɠØJiⱮⱮM”Ṿ;,ɠØJiⱮⱮM - Main Link: no arguments
“Ṿ;,ɠØJiⱮⱮM”           - "Ṿ;,ɠØJiⱮⱮM"
            Ṿ          - unevaluate -> "“Ṿ;$,⁸ØJiⱮⱮṢƑ+EƊ”"
             ;         - concatenate -> the source code = "“Ṿ;,ɠØJiⱮⱮM”Ṿ;,ɠØJiⱮⱮM"
               ɠ       - read a line from STDIN = I
              ,        - pair -> [source, I]
                ØJ     - Jelly's code-page
                  iⱮⱮ  - index of each character in each of [source, I]
                     M - maximal, 1-indexed, indices:
                                            source < I -> [2]
                                            source = I -> [1,2] 
                                            source > I -> [1]
\$\endgroup\$
3
2
\$\begingroup\$

05AB1E (legacy), 28 26 bytes

"34çì·‚Ð{QsË+"34çì·‚Ð{QsË+

-2 bytes by switching to the legacy version of 05AB1E, where double on string works

Outputs 0 if the source code is smaller; 1 if the input is smaller; and 2 if both are equal.

Try it online or verify a few example test cases.

Explanation:

It's based on this base quine: "34çì·"34çì·.

"34çì·‚Ð{QsË+"  # Push string "34çì·‚Ð{QsË+"
 34ç            # Push 34, converted to a character: '"'
    ì           # Prepend it to the string: '"34çì·‚Ð{QsË+'
     ·          # Double it: '"34çì·‚Ð{QsË+"34çì·‚Ð{QsË+'
      ‚         # Pair it with the (implicit) input
       Ð        # Triplicate it
        {       # (Lexicographically) sort the pair
         Q      # Check if the top two pairs are the same
          s     # Swap to get the remaining pair
           Ë    # Check if both values are the same
            +   # Add the two checks together
                # (after which the result is output implicitly)
\$\endgroup\$
2
\$\begingroup\$

PARI/GP, 31 bytes

f=(a)->if(a<s=Str("f="f),2,s<a)

Attempt This Online!

Outputs 1 if the input is greater than the code, 0 if they're equal and 2 otherwise.


PARI/GP, cheating, 28 bytes

(a)->if(a<s=Str(self),2,s<a)

Attempt This Online!

\$\endgroup\$
2
  • 3
    \$\begingroup\$ These are both cheating IMO. Our definition of "proper quine" requires "data that encodes both itself, and something else", which these don't have. (Not declaring this is definitely breaking the rules, but just my opinion) \$\endgroup\$
    – pxeger
    Apr 20 at 6:43
  • \$\begingroup\$ @pxeger By that definition, the self one is indeed cheating. But in f=(a)->if(a<s=Str("f="f),2,s<a), the string "f=" encodes both itself and the f= at the beginning of the code. \$\endgroup\$
    – alephalpha
    Apr 20 at 8:26
2
\$\begingroup\$

Ruby, 85 bytes

->a{z,m,w='->a{z,m,w=',',',';a<=>[z,z,m,m,m,w,w]*39.chr}';a<=>[z,z,m,m,m,w,w]*39.chr}

Try it online!

Explanation:

Not very golfy, I know.

I could take the answer to the linked question and replace '==' with '<=>' for 29 bytes, but I wanted to try something different, and opted not to use eval.

There is a lot of room for improvement, but I like the way it turned out.

\$\endgroup\$
2
\$\begingroup\$

Haskell, 42 bytes

compare$s++show s;s="compare$s++show s;s="

Try it online!

compare program Vs input.

Outputs Ord value :
EQ equal
GT program > input
LT otherwise

\$\endgroup\$
2
\$\begingroup\$

JavaScript, 23 bytes

f=s=>s<(F='f='+f)?0:F<s

Try it online!

Outputs 0 if input less than source, false for equal, and true for greater than.

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

Ruby, 74 49 bytes

p [*$<]*''<=><<'2'*2+?2
p [*$<]*''<=><<'2'*2+?2
2

Try it online!

Note trailing newline

Adapted from the typical Ruby quine. Outputs 0 if input is the same as source, and 1 or -1 if it is different. Thanks to Jo King for suggesting the use of [*$<]*'' to capture input.

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

Julia, 70 bytes

(q=:(show((ARGS[] > (Q = "(q=:($(q)))|>eval")) - (ARGS[] < Q))))|>eval

Try it online!

takes input as a single command line argument, and prints 0 for Equal, 1 for Greater and -1 for Less

Based on the following quine, which is explained in this answer by Dennis (adapted for Julia 1.0):
(q=:(print("(q=:($(q)))|>eval")))|>eval Try it online!

Sadly, all the spaces are neccessary, since the code is stored as an AST, and converting it to a string will put all the spaces back. Also, defining a function or using a macro don't work because of the lengthy comments that are automatically added in the AST

\$\endgroup\$

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.