49
\$\begingroup\$

Challenge

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).

Restrictions

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

\$\endgroup\$
6
  • \$\begingroup\$ Does capitalisation matter? \$\endgroup\$
    – user41805
    Commented Dec 22, 2016 at 10:41
  • \$\begingroup\$ @KritixiLithos by default, yes. \$\endgroup\$ Commented Dec 22, 2016 at 10:41
  • \$\begingroup\$ @KritixiLithos It does indeed. \$\endgroup\$
    – Sanchises
    Commented Dec 22, 2016 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
    Commented Dec 22, 2016 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
    Commented Dec 22, 2016 at 16:43

31 Answers 31

25
\$\begingroup\$

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

\$\endgroup\$
4
  • 6
    \$\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\$ Commented Dec 22, 2016 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
    Commented Dec 22, 2016 at 11:49
  • \$\begingroup\$ @TidB hmm, I guess no? what input do you have in mind? \$\endgroup\$
    – Rod
    Commented Dec 22, 2016 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
    Commented Dec 22, 2016 at 12:02
12
\$\begingroup\$

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.

\$\endgroup\$
5
  • 4
    \$\begingroup\$ Hmmm I think Lenguage is the only language where people are outgolfed by three orders of magnitude... \$\endgroup\$
    – Sanchises
    Commented Dec 23, 2016 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
    Commented Dec 23, 2016 at 19:02
  • \$\begingroup\$ Wouldn't +[,.] make for a much better score? \$\endgroup\$
    – Sanchises
    Commented Dec 28, 2016 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
    Commented Dec 28, 2016 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
    Commented Dec 29, 2016 at 8:07
11
\$\begingroup\$

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.

\$\endgroup\$
13
  • 2
    \$\begingroup\$ Such a clever ruse, you almost won... Did you try the reverse too, I.e 0x00 bytes and 1-indexing? \$\endgroup\$
    – Sanchises
    Commented Dec 22, 2016 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
    Commented Dec 22, 2016 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
    Commented Dec 22, 2016 at 23:02
  • \$\begingroup\$ @Sanchises Could you confirm that is the case here? \$\endgroup\$
    – Dennis
    Commented Dec 22, 2016 at 23:16
  • 1
    \$\begingroup\$ Wouldn't ,[>.,] be shorter? \$\endgroup\$
    – Jo King
    Commented Feb 20, 2018 at 2:58
8
\$\begingroup\$

Jelly, 10 9 bytes

“ṾiЀƓv”v

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.
           Why?
             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.
           What?
             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.
\$\endgroup\$
8
\$\begingroup\$

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!

Explanation:

[(:<>)+,-.]
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
\$\endgroup\$
7
\$\begingroup\$

CJam, 14 12 bytes

{sq\f#p_~}_~

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

Try it online!

\$\endgroup\$
6
\$\begingroup\$

Javascript, 34 bytes

f=a=>a.map(v=>('f='+f).indexOf(v))

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

\$\endgroup\$
6
  • \$\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
    Commented Dec 22, 2016 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
    Commented Dec 22, 2016 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
    Commented Dec 22, 2016 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\$ Commented Dec 22, 2016 at 20:12
  • \$\begingroup\$ @manonthemat "You can assume that the input is a string (or array)" \$\endgroup\$
    – LarsW
    Commented Dec 22, 2016 at 20:17
5
\$\begingroup\$

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!

\$\endgroup\$
1
  • \$\begingroup\$ 119 bytes \$\endgroup\$
    – c--
    Commented Jun 22, 2023 at 17:10
5
\$\begingroup\$

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

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

><> (Fish) 70 bytes

 #.0+4*a5;!?l|!?f4*b+l1--naolc3*1+0.01?!|~ed+0.0+2e-{:;!?+1:i-1:r}+2:"

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

\$\endgroup\$
1
  • \$\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
    Commented Dec 22, 2016 at 16:31
3
\$\begingroup\$

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 :(

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

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 :)

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

05AB1E, 19 bytes

"'ìsvDyk,"'"ìsvDyk,

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.

\$\endgroup\$
4
  • \$\begingroup\$ @Sanchises works for me! \$\endgroup\$ Commented Apr 2, 2018 at 18:35
  • \$\begingroup\$ Cool cool cool! \$\endgroup\$
    – Sanchises
    Commented Apr 2, 2018 at 18:54
  • \$\begingroup\$ @MagicOctopusUrn Well done!! \$\endgroup\$
    – Luis Mendo
    Commented Apr 3, 2018 at 6:36
  • \$\begingroup\$ "'ìIk"'"ìIk - 11 bytes \$\endgroup\$ Commented May 22, 2023 at 15:51
3
\$\begingroup\$

Thunno 2, 10 bytes

"Ṙs€Ȯ"Ṙs€Ȯ

Attempt This Online!

Explanation

"Ṙs€Ȯ"Ṙs€Ȯ  # Implicit input
"Ṙs€Ȯ"      # Push the string "Ṙs€Ȯ"
      Ṙ     # Surround it by quotes
       s€Ȯ  # Get the index of each character
            # of the input in the string
            # Implicit output
\$\endgroup\$
2
  • \$\begingroup\$ Can you show this works with multiple characters simultaneously? The challenge specifies a string, not a single character. \$\endgroup\$
    – Sanchises
    Commented Jun 22, 2023 at 16:00
  • \$\begingroup\$ @Sanchises sorry, I didn't read that properly. It should be fixed now. \$\endgroup\$
    – The Thonnu
    Commented Jun 22, 2023 at 16:03
2
\$\begingroup\$

JavaScript, 39 bytes

p=s=>[...s].map(c=>`p=${p}`.indexOf(c))

console.log( p('mapP') )

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

Pyth, 11 bytes

xL_+N"N+_Lx

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
\$\endgroup\$
2
\$\begingroup\$

SmileBASIC, 128 96 88 86 bytes

?R<3+CD,4LINPUT(S$)WHILE""<S$?INSTR("?R<3+CD,4LINPUT(S$)WHILE"+CHR$(34),SHIFT(S$))WEND

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.

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

Japt -m, 2 bytes

Takes input as a character array. Output is 0-based, using -1 for invalid characters. String input could also be used but there would be no separator between the output indices, which shouldn't be an issue as it would be unambiguous seeing as they can only be 0 & -1.

bb

Try it

bb     :Implicit map of each U in input array/string
b      :First 0-based index in U of
 b     :  Literal "b"

Alternatively:

aa

Try it

a being lastIndexOf.

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

Java 8, 172 122 119 bytes

a->/*.forEch(Systemupin"\dxO)+ ;:*/a.forEach(c->System.out.print("a->/*.forEch(Systemupin\"\\dxO)+ ;:".indexOf(c)+" "))

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

Try it online.

Explanation:

a->                    // Method with character-array parameter and no return-type
  /*.forEch(Systemupin"\dxO)+ ;:*/
                       //  Comment containing the remaining characters of the code
  a.forEach(c->        //  Loop over the input-array:
    System.out.print(  //   Print
      "a->/*.forEch(Systemupin\"\\dxO)+ ;:"
                       //   from a String containing all the characters used in the code
      .indexOf(c)+" "))//   the index of the char, plus a space as delimiter
\$\endgroup\$
0
1
\$\begingroup\$

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:

print(d("a,b(]q"))
#[0, 1, 2, 8, 15, -1]
\$\endgroup\$
1
\$\begingroup\$

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.

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

Husk, 12 bytes

m€`:'""m€`:'

Try it online!

Explanation

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]
\$\endgroup\$
1
\$\begingroup\$

J, 31 22 bytes

11|1+i.~&'11|1+i.~&'''

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.

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

><>, 31 bytes

'rd3*i:0(?;}{:}-&b0&?.75*l-nao]

Try it online!

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

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

Perl 5 with -pl, 43 bytes

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

$s=q{$_=index"\$s=q{$s};eval\$s",$_};eval$s

Try it online!

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

Stax, 19 bytes

"'sym[]I+"'"s+ym[]I

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.

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

SAS, 124 60 bytes;

The new answer is shift from pure SAS 4GL to SAS macrolanguage.

The code:

%let S=%nrstr(%macro _(t);data;s=&t;do i=1to length(s);p=find(symget("S"),char(s,i));put p@;end;run;%mend;%_);%unquote(&S())

To run it, insert your 'string' enclosed in single quotes between brackets after &S, for example:

...%unquote(&S('!#&%macro _sS@'))

and in the log you will see:

0 0 20 1 2 3 4 5 6 7 8 18 57 77

To escape single quote in the string write it doubled: 'AB''CD'.


[EDIT:] 60 bytes answer was suggested for rejection as "a snippet, meaning it cannot just assume variables are defined". In context of SAS 4GL programming I agree to disagree with that opinion. ;-)

I keep it below for further comments.


Code assume that the string is given in a variable s and the source code is stored in macroVariable s.

The code:

do i=1to length(s);p=find(symget("S"),char(s,i));put p@;end;

Process goes as follows:

  1. store source code in macrovariable S by runing:
data _null_;
  call symputX("S",'do i=1to length(s);p=find(symget("S"),char(s,i));put p@;end;',"G");
run;
  1. assign string "i S;abcd{" to variable s and resolve ( by &S. ) the macrovariable S:
data;
s="i S;abcd{";
&S.
run;

The Log says:

1    data _null_;
2    call symputX("S",'do i=1to length(s);p=find(symget("S"),char(s,i));put p@
2  ! ;end;',"G");
3    run;

NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


4
5    data;
6    s="i S;abcd{";
7    &S.
8    run;

4 3 35 19 41 0 39 1 0
NOTE: The data set WORK.DATA14 has 1 observations and 3 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds

You can run:

%put %length(%superq(S));

to get source code length.

\$\endgroup\$
9
  • \$\begingroup\$ Your submission must be either a function or a full-program, not just a snippet, meaning it cannot just assume variables are defined. Also, for quine challenges, the main part of the challenge is getting the program’s own source code without using any external sources like reading the file or having it already defined. \$\endgroup\$ Commented May 23, 2023 at 20:59
  • \$\begingroup\$ Ok, will update the answer. \$\endgroup\$ Commented May 23, 2023 at 21:05
  • \$\begingroup\$ Is there any way SAS can take input or define a function? As it stands, hard-coding the input still does not confirm to the rules. \$\endgroup\$
    – Sanchises
    Commented May 24, 2023 at 4:20
  • \$\begingroup\$ It's much more complex then it seems... I will split my answer in several comments since there is not enough characters in the comment box. \$\endgroup\$ Commented May 24, 2023 at 11:04
  • \$\begingroup\$ /* 1 */ SAS is tool/language dedicated to data processing. Mainly in tabular form (but not exclusively). For SAS 4GL in general there are 4 standard ways of passing data: 1) by SAS dataset, 2) by text file (also "inline" inside your code), 3) by a macrovariable, or 4) hardcoding in data step. There are 4 standard interfaces through which you can execute code in SAS: a) DMS - SAS Display Manager, b) EG - SAS Enterprise Guide, c) SAS Studio, or d) in command line. \$\endgroup\$ Commented May 24, 2023 at 11:05
1
\$\begingroup\$

JavaScript, 59 57 bytes

-2 Bytes thanks to Shaggy

a=>(m='Opined.fx,"+)',a.map(x=>("a=>(m='"+m).indexOf(x)))

Without reading source.

Attempt This Online!

\$\endgroup\$
2
  • \$\begingroup\$ 57 bytes: a=>(m=",.px'+)indeOf",a.map(x=>('a=>(m="'+m).indexOf(x))) \$\endgroup\$
    – Shaggy
    Commented May 23, 2023 at 15:35
  • \$\begingroup\$ @Shaggy Ah, smart. I don't usually think about variable names as long as they are 1 byte... \$\endgroup\$ Commented May 24, 2023 at 23:25
0
\$\begingroup\$

Burlesque, 27 bytes

#Qup~-"#Q"j?+wd\[Ppm{pPjFi}

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
\$\endgroup\$
0
\$\begingroup\$

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

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

Try it online!

\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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