# Infinite quote escaping sequence

Related

We start with the string a, and forever append to the string a comma followed by the string quote-escaped, where quote-escaping means doubling all quotes in a string, and then surrounding it with an additional pair of quotes.

In the first few steps we will get:

• a
• a,'a'
• a,'a','a,''a'''
• a,'a','a,''a''','a,''a'',''a,''''a'''''''

If we continue to do that forever, we get the following infinite string: a,'a','a,''a''','a,''a'',''a,''''a''''''','a,''a'',''a,''''a'''''',''a,''''a'''',''''a,''''''''a''''''''''''''','a,''a'',''a,''''a'''''',''a,''''a'''',''''a,'''...

If create a sequence, which is 1 at indices that string contains a quotes, we'll get the following sequence:

1. 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, ...

Alternatively, we can look at the indices of quotes, and get the following sequence (this is 0-indexed):

1. 2, 4, 6, 9, 10, 12, 13, 14, 16, 19, 20, 22, 23, 25, 26, 29, 30, 31, 32, 34, 35, 36, 37, 38, 39, 40, 42, 45, 46, ...

## Rules

• You can choose whether you use 0-indexing or 1-indexing.
• If you output sequence 1, you can use any truthy/falsey values instead of 1/0, or any two different consistent values.
• If you output sequence 2 (the one with the indices), you can choose if those indices use 0-indexing or 1-indexing.
• You can use one of following output modes:
• Take $$\n\$$ as an input, and output the $$\n\$$-th element in the sequence.
• Take $$\n\$$ as an input, and output all elements in the sequence up to the $$\n\$$-th element.
• Take no input, and output the entire sequence forever.
• You can use any reasonable input/output format.
• Standard loopholes are disallowed.

This is code golf, so the shortest code wins.

# Python, 57 bytes (@Steffan)

a=b="/"
while[print(end=b)]:b="/\%s\\"%repr(a)[1:-1];a+=b

Attempt This Online!

### Old Python, 58 bytes

a=b="/"
while[print(end=b)]:b="/\\%s\\"%repr(a)[1:-1];a+=b

Attempt This Online!

Prints the entire sequence using / for 0 and \ for 1.

• Smart abuse of the behavior of repr on backslashes Jul 16, 2022 at 19:10
• You don't need to escape the backslash in \\%s for -1 byte Jul 16, 2022 at 19:14

# J, 22 19 bytes

{1&(],1,~0 1,+#])&0


Try it online!

-3 thanks to ovs!

Quite slow, as we iterate n times and take the nth result.

TIO link shows first 17 terms.

Approach is straightforward recursion:

• { nth item (0-indexed) from...
• 1&( )&0 Iterate n times, starting with 0, and using a constant left arg of 1...
• (],1,~0 1,+#]) Bookend +#] (to be explained below) with 0 1 and 1
• +#] The only interesting part, really. This is how we double up the quotes. Consider 0 0 1 0 1:
• + adds 1 to every element, taking advantage of the fixed left arg of 1:

1 1 2 1 2

• #] use that as a duplication mask for 0 0 1 0 1:

0 0 1 1 0 1 1

• {.1&(],1,~0 1,+#])&0 is 20 bytes, and can you do { instead of {. for the n-th value output format?
– ovs
Jul 16, 2022 at 19:06
• @ovs, I really like the idea of reusing the fixed left arg of 1 to save a but for >:! Jul 16, 2022 at 19:12

$- last two links as a monad - g(x): Ʋ - last four links as a monad - h(x): e.g. [0,0,1,0,1] ‘ - increment [1,1,2,1,2] x - repeat (double the quotes) [0,0,1,1,0,1,1] Ø1 - [1,1] j - join (wrap in quotes) [1,0,0,1,1,0,1,1,1] Ż - prepend a zero (prepend a comma) [0,1,0,0,1,1,0,1,1,1] ; - concatenate that to x ḣ - head to index n  # Jelly, 14 bytes ;Ø.;x‘$;1
0Ç¡ḣ


Try it online!

Full program yielding first $$\n\$$ elements of the quote/non-quote sequence. Exponentially slow; better tested with manual control of the number of iterations.

;Ø.;x‘$;1 Monadic helper link: iterate ;Ø. Append [0, 1]. ; Append x$      the argument with its elements repeated by
‘       themselves incremented.
;1    Append 1.

Ç¡     Repeat that n times
0       starting from 0,
ḣ    and take the first n elements.


# Batch Script, 80 bytes

Outputs the first n elements of sequence 1,

CMD/VON/CSET s=0^&(FOR /L %%Q in (1,1,8)DO @SET s=!s!01!s:1=11!1)^&ECHO !s:~,%1!


so ie if file is called quote.bat

CALL quote.bat 5


gives you

00101


I hardcoded 8 iterations because that's the maximum amount before the string gets too big and CMD fails to parse it. So I'm not too sure if this answer counts, but I thought it would be neat to post it anyway.

Edit : minus a bunch of bytes thanks to Neil

• I'm not sure p is necessary, is it? (Also, I tried 9 iterations and CMD simply crashed on me. Oops.)
– Neil
Jul 17, 2022 at 0:04
• Ahhhh thanks, that was big oversight on my part lol Jul 17, 2022 at 14:55

# Ruby, 52 47 bytes

f=->n,r=?0{r[n]||f[n,r+"01#{r.gsub ?1,'11'}1"]}


Try it online!

Returns the nth element.

# Python 3, 109, 71, 64, 63, 62 bytes

a=w='0'
while[print(end=w)]:w=f"01{a.replace('1','11')}1";a+=w


Try it online!

-45 thanks to @Steffan

-1 thanks to @AnttiP

## Explanation (Outdated):

a=w="0"


Self-explanatory

while 1:


Repeat infinite times.

print(end=w+"0");


print w with 0. There's "0" because "0" is a comma.

w="1"+


w is "1" and...

a.replace("1","11")+


double the ones of a and...

"1";


one.

a="00"+w


Put two zeroes and w.

• Why not just use raw 0 and 1 instead of using a' and replacing them? Try it online! Jul 16, 2022 at 18:51
• This actually doesn't produce correct output. 64 bytes that does, though Jul 16, 2022 at 19:13
• Use a format string to save a byte: Try it online! Jul 17, 2022 at 6:45

# Charcoal, 29 bytes

Ｎθ≔0ηＷ›θＬη≔⁺⁺η0⪫11⪫⪪η1¦11η…ηθ


Try it online! Link is to verbose version of code. Outputs the first n elements of sequence 1. Explanation:

Ｎθ


Input n.

≔0η


Start with a string of 0 instead of a.

Ｗ›θＬψ


Repeat until there are enough elements.

≔⁺⁺η0⪫11⪫⪪η1¦11η


Extend the string by appending its quotation, but use 0 instead of a comma and 1 instead of a quote.

…ηθ


Output the first n elements.

# 05AB1E, 14 bytes

ÎFDX3:5šbJÀ«I£


Outputs the first $$\n\$$ items of the binary sequence as string.

Try it online.

Explanation:

Î              # Push 0 and the input
F             # Pop and loop the input amount of times:
D            #  Duplicate the current string
X3:         #  Replace all 1 with 3
5š       #  Convert the string to a list of digits, and prepend a 5
b      #  Convert each digit to a binary string
#  (3 become 11; 5 becomes 101; 0 and 1 remain unchanged)
J     #  Join it back together to a single string
À    #  Rotate it once towards the right, so the leading 1 is trailing
«   #  Append it to the previous string we've duplicated
I£ #  Only leave the first input amount of characters
# (after the loop, the result is output implicitly)


# JavaScript (ES6),  50  49 bytes

Saved 1 byte thanks to @tsh

Returns the $$\n\$$-th term of the binary sequence, 0-indexed, where 0's are encoded with 2's.

f=(n,s='2')=>s[n]||f(n,s+21+s.replace(/1/g,11)+1)


Try it online!

• By applying the "any two different consistent values" rule, you can use other number instead of 0 to save a byte.
– tsh
Jul 18, 2022 at 1:55

# Bash, 61 bytes

for((a=0;${#a}<=$1;));do a+=01${a//1/11}1;done;echo${a:\$1:1}


Try it online!

Returns the $$\0\$$-based $$\n^{\text{th}}\$$ element of the binary sequence.

s>>=id
s=[0]:[0:1:[b|b<-a,c<-[0..b]]++[1]|a<-scanl1(++)s]

Attempt This Online!

# APL (Dyalog Extended), 27 bytes

{{∊⍵0 1(,⍨¨@(⍸⍵)⊢⍵)1}⍣⍵⊢,0}
`

Try it online!

• Hey, welcome back to PPCG! Btw converting this to a tradfn saves some bytes. Besides, I think porting Jonah's solution would be shorter. Jan 6 at 17:26