Write a non-empty program/function p that, given a non-empty input string s, outputs the position of the first occurrence of each character of s in the source code of p.

For example, if your program is

main() { cout << magic << cin }
^0   ^5   ^10  ^15  ^20  ^25

and it receives an input abcd{, the output should be

[1, x, 9, x, 7] (0-based)        [2, x, 10, x, 8] (1-based)

Here, x represents any output that is not a valid output for a character position (e.g., a negative number, 0 if you use 1-based indexing, NaN, Inf, the string potato, a number greater than your program's length, etc).


Reading the source code is not allowed (like in a proper quine). The use of comments is allowed, but does count towards your score.

Input and output can be done in a reasonable format, but must be unambiguous (only additional delimiters, no rand stream and claiming that the answer is somewhere in there), consistent (e.g., the x from above should always be the same value) and human-readable; for example, a string or a character array. You can assume that the input is a string (or array) of printable ASCII characters; no need to handle the entire Unicode set.

Custom code-page or non-printable ascii in your code?

If your language uses a custom code-page (Jelly, APL, etc), you must take that into account (so a program €æÆ must output [1, x, 2] for an input €%æ). Using only non-ASCII characters to output -1 always (since the input is ASCII-only) is not a valid solution. You may assume that your program natively accepts your custom codepage, i.e., if your program has a method of converting a character A to an integer 65 (ASCII encoding), you may assume that it now converts the 65th character in your codepage to 65.

Inspired on the following challenge: Positional Awareness

  • \$\begingroup\$ Does capitalisation matter? \$\endgroup\$ – user41805 Dec 22 '16 at 10:41
  • \$\begingroup\$ @KritixiLithos by default, yes. \$\endgroup\$ – Martin Ender Dec 22 '16 at 10:41
  • \$\begingroup\$ @KritixiLithos It does indeed. \$\endgroup\$ – Sanchises Dec 22 '16 at 10:54
  • \$\begingroup\$ If my program only uses indices 0 to 9, do I need a separator or could I output, e.g., 01030708070? \$\endgroup\$ – Dennis Dec 22 '16 at 16:40
  • \$\begingroup\$ @Dennis No, you do not. It's unambiguous, consistent and human-readable. Requiring a separator would not add anything interesting to the challenge, so by all means abuse your low byte count. ;) \$\endgroup\$ – Sanchises Dec 22 '16 at 16:43

26 Answers 26


Python2, 55 Bytes

a=" )dfi(+m,nprut.';";print map(('a="'+a).find,input())

Starts with a string that contains all the characters used in the code, and then search the indexes

  • 5
    \$\begingroup\$ I don't see how this is the boring answer. I think using the standard quine is a lot less interesting than this. :) \$\endgroup\$ – Martin Ender Dec 22 '16 at 11:06
  • \$\begingroup\$ Since this is Python 2, wouldn't this break on most inputs?. If it does break, you'd have to use raw_input. \$\endgroup\$ – TidB Dec 22 '16 at 11:49
  • \$\begingroup\$ @TidB hmm, I guess no? what input do you have in mind? \$\endgroup\$ – Rod Dec 22 '16 at 11:56
  • \$\begingroup\$ @Rod Nevermind, I was just being a little dumb. It'll always work when you input an iterable. Silly me. \$\endgroup\$ – TidB Dec 22 '16 at 12:02

Lenguage, 56,623 bytes

Below is a hexdump of the first 256 bytes. The remaining bytes can be chosen arbitrarily.

0000000: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f  ................
0000010: 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f  ................
0000020: 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f   !"#$%&'()*+,-./
0000030: 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f  0123456789:;<=>?
0000040: 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f  @ABCDEFGHIJKLMNO
0000050: 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f  PQRSTUVWXYZ[\]^_
0000060: 60 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f  `abcdefghijklmno
0000070: 70 71 72 73 74 75 76 77 78 79 7a 7b 7c 7d 7e 7f  pqrstuvwxyz{|}~.
0000080: 80 81 82 83 84 85 86 87 88 89 8a 8b 8c 8d 8e 8f  ................
0000090: 90 91 92 93 94 95 96 97 98 99 9a 9b 9c 9d 9e 9f  ................
00000a0: a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af  ................
00000b0: b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 ba bb bc bd be bf  ................
00000c0: c0 c1 c2 c3 c4 c5 c6 c7 c8 c9 ca cb cc cd ce cf  ................
00000d0: d0 d1 d2 d3 d4 d5 d6 d7 d8 d9 da db dc dd de df  ................
00000e0: e0 e1 e2 e3 e4 e5 e6 e7 e8 e9 ea eb ec ed ee ef  ................
00000f0: f0 f1 f2 f3 f4 f5 f6 f7 f8 f9 fa fb fc fd fe ff  ................

Output is in bytes, as customary for brainfuck et al.

How it works

This is a simple cat program, specifically ,[.,].

The source code contains all 256 byte values in order, so each byte's index in it matches its value.

  • 4
    \$\begingroup\$ Hmmm I think Lenguage is the only language where people are outgolfed by three orders of magnitude... \$\endgroup\$ – Sanchises Dec 23 '16 at 9:03
  • 2
    \$\begingroup\$ Lenguage is also the only language that constantly finds ways to cheat that wouldn't even be remotely competitive in other languages. :P \$\endgroup\$ – Dennis Dec 23 '16 at 19:02
  • \$\begingroup\$ Wouldn't +[,.] make for a much better score? \$\endgroup\$ – Sanchises Dec 28 '16 at 20:38
  • \$\begingroup\$ @Sanchises That would save roughly 12,000 bytes, but it would also print an extra null byte at the end. \$\endgroup\$ – Dennis Dec 28 '16 at 22:19
  • 2
    \$\begingroup\$ Well, I suppose the null byte is in the input (even though it's function is to terminate the string), and would be at the 0th position in your program... ;) \$\endgroup\$ – Sanchises Dec 29 '16 at 8:07

Lenguage, 1.22e7 bytes

Consists of 12263215 NUL bytes, (Hex 0x00).

Outputs a NUL for every character that doesn't appear in the source.

The ruse is that the input will never contain a NUL, so we always output the amount of NULs that there are characters in the input.

This translates to the following Brainfuck program


And with a breakdown...

,[    ,]    #Basic Input loop.
  [-]       #Zero out the cell.
     .      #Print it (A NUL).

This just shows the sheer power of Lenguage as a golfing language. Fear it.

  • 2
    \$\begingroup\$ Such a clever ruse, you almost won... Did you try the reverse too, I.e 0x00 bytes and 1-indexing? \$\endgroup\$ – Sanchises Dec 22 '16 at 22:49
  • \$\begingroup\$ I would have loved to, but Brainfuck/Lenguage (Or atleast, the interpreter I'm using) cannot differentiate between EOF and 0x00, So I would be unable to actually answer the challenge. \$\endgroup\$ – ATaco Dec 22 '16 at 22:58
  • \$\begingroup\$ Brainfuck et al. are usually allowed to print integers as bytes, i.e., you'd print SOH for 1, NUL for 0. \$\endgroup\$ – Dennis Dec 22 '16 at 23:02
  • \$\begingroup\$ @Sanchises Could you confirm that is the case here? \$\endgroup\$ – Dennis Dec 22 '16 at 23:16
  • 1
    \$\begingroup\$ Wouldn't ,[>.,] be shorter? \$\endgroup\$ – Jo King Feb 20 '18 at 2:58

Jelly, 10 9 bytes


Try it online!

How it works

“ṾiЀƓv”v  Main link. No arguments.

“ṾiЀƓv”   Set the left argument and the return value to s := 'ṾiЀƓv'.
        v  Execute the string s as a monadic Jelly program with argument s.

 Ṿ         Uneval; yield a string representation of s, i.e., r := '“ṾiЀƓv”'.
     Ɠ     Read one line from STDIN and evaluate it like Python would.
  iЀ      Find the index of each character in the input in r.
      v    Eval the list of indices as a monadic Jelly program with argument s.
             This is the shortest way to add the character 'v' to the string s,
             meaning that we can use r without having to append anything.
             The v atom vectorizes at depth 1 for its left argument, meaning that
             it acts on arrays of numbers and/or characters. When fed an array of
             integers, it first converts them to strings, then concatenates the
             strings and evaluates them as a Jelly program. For example, the array
             [1, 2, 3] gets cast to the string '123', then evaluates, yielding 123.
             Something slightly different happens if the array starts with a 0. For
             example, the array [0, 1, 2] gets cast to '012' just as before, but
             Jelly views '0' and '12' as two separate tokens; numeric literals
             cannot start with a 0. Since the Jelly program is monadic, the first
             token – '0' – sets the return value to 0. Since the second token –
             '12' – is also a niladic link, the previous return value is printed
             before changing the return value to 12. Then, the program finishes
             and the last return value is printed implicitly.

pbrain, 402 356 340 338 329 bytes


Phew, @KritixiLithos and I have been working on this for 4 days now.

Prints 0x00 if input char isn't in program, index of the char (1-based) in hex otherwise. Try it online!


All chars listed here; like other submissions 
@KritixiLithos added this part; I don't know what it does but saves the program
Comparison ;calculates z=x!=y and puts it in between x and y
Start; X _ _ _ Y
End;   X Z _ _ Y
Function to add 91 to the tape
Function to add 40 to the tape
Function to add 58 to the tape
Function to add 60 to the tape
Function to add 62 to the tape
Function to add 41 to the tape
Function to add 43 to the tape
Function to add 44 to the tape
Function to add 45 to the tape
Function to add 46 to the tape
Function to add 93 to the tape

This last function is the loop. It loops through the selected characters [(:<>)+,-.] in order and compares the input with the character. Now I'm going to give a deeper explanation on how this loop works.

12-n n+2 _ n+2: _ _ _ i _ _ _ _ _ _;  n=loop counter
                  ^                ;  i=input

The stack looks like that while in a loop. The loop will run until 12-n is 0. Then we have the counter which is n+2. This counter is also the number of the function for each of the selected characters. So when n=0, n+2 will be corresponding to the first character, ie [. >[->+>+<<]>>[-<<+>>]<: does just that, it converts the counter to the character.

Once the pointer is where the caret is, we will compare the character produced from the counter variable with the input while preserving them.

12-n n+2 _ n+2: Z _ _ i _ _ _ _ _ _;  n=loop counter
                ^                  ;  i=input

Z is 0 when the character is equal to the input, or some other non-zero integer otherwise.

Now we come up with an if-statement to check this equality.


If Z is non-zero, ie the character and the input are not the same, we increment the next memory place.

After we come out of this if-statement, we decrement the next memory place. Now this memory place contains !Z. Finally using this, we output the index of the character if it matches with the input and then exit the loop forcibly. Else, we continue on with the loop until either it is over or a match is found.

Clears first byte; goes to position to start program
Loops inputs

CJam, 14 12 bytes


Uses 0-based indexing and -1 for characters that don't appear in the source.

Try it online!


Javascript, 34 bytes


It takes input as array of strings, x is -1 (0-based indexing).

  • \$\begingroup\$ It's allowed, since that method is also acceptable for quines. It doesn't open its source file and read it or use a variable initialized to the source. \$\endgroup\$ – mbomb007 Dec 22 '16 at 19:07
  • 1
    \$\begingroup\$ @mbomb007 I can't speak for all JavaScript engines but in Firefox Function.toString works by reading the source. At one point it would crash in debug builds if the source was no longer there when it tried to read it. (I haven't tried it recently because debug builds are so crashy in general.) \$\endgroup\$ – Neil Dec 22 '16 at 19:40
  • \$\begingroup\$ I don't think it's any different than doing s='s=%s;print s%%s';print s%s in Python. It doesn't include the f=, so it's okay \$\endgroup\$ – mbomb007 Dec 22 '16 at 19:47
  • 1
    \$\begingroup\$ You can not really do that, because the input a is supposed to be a string. There is no map function for strings. \$\endgroup\$ – manonthemat Dec 22 '16 at 20:12
  • \$\begingroup\$ @manonthemat "You can assume that the input is a string (or array)" \$\endgroup\$ – LarsW Dec 22 '16 at 20:17

C, 153 152 143 bytes

char s[99],p[]="odeflnrti%()*+-0;<={}\\";c;f(char*i){sprintf(s,"char s[99],p[]=\"%s",p);for(c=0;c<strlen(i);)printf("%d ",strchr(s,i[c++])-s);}

Try it online!


Ruby, 41 88 86 71 69 67 61 56 bytes

a='p$<.chrs{| #index};"';$<.chars{|c|p"a='#{a}".index c}

Thx Lynn for killing 6 bytes

  • 1
    \$\begingroup\$ a='p$<.chrsm{| #index};"';p$<.chars.map{|c|"a='#{a}".index c} should work too, taking input from STDIN. \$\endgroup\$ – Lynn Dec 22 '16 at 12:23

><> (Fish) 70 bytes


Probably the longest ><> 1 liner I've ever made.

It will print the output for each character found on a separate line (0 indexed).

A non found character will always print the length of the code + 1 (I could change this if deemed not okay in it's current state) so in this case 71 will always be the "Not found" characters.

I'll run up an explanation once I get the time.

Some test cases;

##K = 1\n1\n71

#"# = 1\n69\n1

Try it online

><> language

  • \$\begingroup\$ I think 71 is fine as an output for not found. It's consistent, unambiguous and human-readable, which I think is more important than it being "...any output that is not a positive integer". I expanded the rules to reflect this decision. \$\endgroup\$ – Sanchises Dec 22 '16 at 16:31

Perl 6, 50 52 bytes

{(('R~.index$_) for}\\'R~'{((\'').index($_) for $_)}

Translation of G B's Ruby solution and Rod's Python solution.

A lambda that inputs a list of characters and outputs a list of zero-based indexes (Nil for nonexistent characters).

EDIT: Fixed an oversight - required adding 2 bytes :(


Clojure, 43 56 48 bytes

Edit: Damn I forgot about 2! Increased from 43 to 56.

Edit 2: Updated the sample code below this text, updated the number of bytes not to include (def f ...) but just the hash-map part.

{\{ 0\\   1\  3\0   4\1 10\3 14\4 20\2 34 \} 43}

The hash-map consists only of characters 01234{\\}, and it encodes their locations. In Clojure hash-maps can be used functions, as shown in this complete example (f could be replaced by the hash-map definition):

; Keeping track of the zero-based index:
;      00000000001111111111222222222233333333334444444444
;      01234567890123456789012345678901234567890123456789
(def f {\{ 0\\   1\  3\0   4\1 10\3 14\4 20\2 34 \} 43})

(map f "0123456789{} \\abcdef") ; (4 10 34 14 20 nil nil nil nil nil 0 43 3 1 nil nil nil nil nil nil)
(apply str (keys f))            ; " 01234{\\}"

I guess this counts :)


JavaScript, 39 bytes


console.log( p('mapP') )

  • 1
    \$\begingroup\$ can you explain [...s] please? \$\endgroup\$ – Erresen Dec 22 '16 at 11:55
  • \$\begingroup\$ It is known as spread operator, you can see a bit more in the ES6 golfing tips. \$\endgroup\$ – Washington Guedes Dec 22 '16 at 11:58
  • 2
    \$\begingroup\$ Reading the source code is not allowed \$\endgroup\$ – Arnauld Dec 22 '16 at 12:27

Pyth, 11 bytes


A program that takes input of a "quoted string", with any quotes in the string escaped with a preceding \, and prints a list of zero-indexed values with -1 for characters not in the source.

Try it online!

How it works

xL_+N"N+_Lx    Program. Input: Q
xL_+N"N+_Lx"Q  Implicit quote closure and implicit input
     "N+_Lx"   Yield the string "N+_Lx"
   +N          Prepend a quote
  _            Reverse
 L          Q  Map over Q:
x               Yield the index of the character in the string
               Implicitly print

05AB1E, 19 bytes


Try it online!

This outputs -1 in place of missing chars.

Luis Mendo posted this (slightly modified) on Golf you a quine for great good! , adding "s" and "k" to that quine results in this answer as well. However, I can't take credit for that trivial of a modification... Luis, you can message me if you'd like to repost this and I'll just delete it. If you want to see my progress before finding that question, view edits. Well... It was significantly like his at one point.

  • \$\begingroup\$ @Sanchises works for me! \$\endgroup\$ – Magic Octopus Urn Apr 2 '18 at 18:35
  • \$\begingroup\$ Cool cool cool! \$\endgroup\$ – Sanchises Apr 2 '18 at 18:54
  • \$\begingroup\$ @MagicOctopusUrn Well done!! \$\endgroup\$ – Luis Mendo Apr 3 '18 at 6:36

SmileBASIC, 128 96 88 86 bytes


An important thing to realize is that this is not really a quine challenge. You only need the source code up to the last unique character.

I put at least 1 of each character at the beginning of the code: ?R<3+CD,4LINPUT(S$)WHILE" so I only have to store a copy of the program up to the first quotation mark.


Python, 90 88 bytes

a,b,d=" ()+.7:[]efilmnor","a,b,d=\"",lambda e:[[b.find(d),a.find(d)+7][d in a]for d in e]

Test case:

#[0, 1, 2, 8, 15, -1]

Stacked, noncompeting, 36 bytes

When I said this language was still in development, I meant it. Apparently, prompt used to consume the entire stack. This is why I can't have nice things. Try it here!

[tostr ':!' + prompt CS index out]:!

This is the standard quine framework. Basically, : duplicates the function [...] on the stack, which is then executed with !. Then, the inside of [...] executes with the function on the stack. It casts it to a string, appends :! (the program itself), then takes a string input with prompt. CS converts it to a character string. A character string is a bit different from a regular string in that it has operators vectorize over it. In this case, index vectorizes over the input, yielding each index of the input string in the program, finally being outputted.

For input Hello, World!, this gives:

(-1 27 -1 -1 2 -1 6 -1 2 5 -1 26 9)

I tried using the one without a quine (i.e. encoding the string of characters that appear in your source), but there is only one type of quotation mark in Stacked, namely, ', so it would be longer to do that type of solution.


Husk, 12 bytes


Try it online!


The explanation is using ¨ to delimit strings and ' to delimit characters:

m€`:'""m€`:'  -- implicit input, for example: ¨m"a1`¨
      "m€`:'  -- string literal: ¨m€`:'¨
  `:'"        -- append character '"': ¨m€`:'"¨
m             -- map function over each character (example with 'a'):
 €            -- | index of first occurrence (1-indexed): 0
              -- : [1,6,0,0,3]

Java 8, 172 122 bytes

a->{/*.indexOf(c)+\" ;}orh:Systmup*/for(char c:a)System.out.print("a->{/*.indexOf(c)+\\\" ;}orh:Systmup".indexOf(c)+" ");}

0-indexed, and gives -1 for characters that aren't part of the source code.


Try it online.

a->{                         // Method with character-array parameter and no return-type
  /*.indexOf(c)+\" ;}orh:Systmup*/
                             //  Comment containing the remaining characters of the code
  for(char c:a)              //  Loop over the input-array
    System.out.print(        //   Print:
      "a->{/*.indexOf(c)+\\\" ;}orh:Systmup"
                             //    String containing all the characters used in the code
      .indexOf(c)+" ");}     //    Print the index of the char, plus a space as delimiter

J, 31 22 bytes


Try it online!

1-indexed, 0 for characters that aren’t present in the code. '' stands for a single quote. Find each character in the string 11|1+i.~&', add 1, modulo 11.


><>, 31 bytes


Try it online!

Output is 1-indexed, with 32 meaning the character is not in the code.


Perl 5 with -pl, 43 bytes

Uses newline separated input and prints -1 for characters not appearing in the program.


Try it online!

  • \$\begingroup\$ @Sanchises Fixed now, sorry about that. Clearly didn't read properly! \$\endgroup\$ – Dom Hastings Feb 20 '18 at 8:33
  • \$\begingroup\$ No problem. Thanks for bringing some new life into this challenge! \$\endgroup\$ – Sanchises Feb 20 '18 at 10:19

Stax, 19 bytes


Run and debug it

Outputs 0-based index, one character per line. Turns out it's shorter than modifying the "34bL"34bL quine I wrote earlier.


Burlesque, 27 bytes


Try it online!

Returns list of indices with -1 for each not present.

#Q      #Push remaining code to stack
up      #Turn to string
~-      #Remove brackets
"#Q"j?+ #Add the "#Q" to the beginning
wd\[    #Remove all spaces
Pp      #Push to virtual stack
m{      #Map (apply to each char in input)
  pP    #Pop from other stack
  jFi}  #Find character in string

Python 2, 49 48 bytes

-1 byte thanks to Jo King

c='print map(("c=%r;ex"%c).find,input())';exec c

Try it online!

Python 3, 53 52 51 bytes

-1 byte thanks to Jo King


Try it online!


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.