19
\$\begingroup\$

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

Your task is to output one of these numeric sequences.

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.

\$\endgroup\$

15 Answers 15

6
\$\begingroup\$

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.

\$\endgroup\$
2
  • 3
    \$\begingroup\$ Smart abuse of the behavior of repr on backslashes \$\endgroup\$
    – mousetail
    Jul 16 at 19:10
  • 2
    \$\begingroup\$ You don't need to escape the backslash in \\%s for -1 byte \$\endgroup\$
    – Steffan
    Jul 16 at 19:14
4
\$\begingroup\$

Vyxal, 16 bytes

0w?(:k≈$vß"1Wf)Ẏ

Try it Online!

Same idea as the Jelly answer.

\$\endgroup\$
4
\$\begingroup\$

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
      
\$\endgroup\$
2
  • 1
    \$\begingroup\$ {.1&(],1,~0 1,+#])&0 is 20 bytes, and can you do { instead of {. for the n-th value output format? \$\endgroup\$
    – ovs
    Jul 16 at 19:06
  • \$\begingroup\$ @ovs, I really like the idea of reusing the fixed left arg of 1 to save a but for >:! \$\endgroup\$
    – Jonah
    Jul 16 at 19:12
4
\$\begingroup\$

lin, 57 bytes

.\n0\;.n e*.n `t
dup \; `' `flat1`,0`,1`+ `+
dup \; e&
1,

Try it here! Returns an iterable with the first n elements.

The code above extremely inefficiently runs through n iterations. For testing purposes, the following code is equivalent:

100 ; `_
.\n0;.n `t
dup \; `' `flat1`,0`,1`+ `+ `size.n < \@ e&
dup \; e&
1,

Explanation

Prettified code:

.\n 0 \; .n e* .n `t
dup ( dup ( 1, ) e& ) `' `flat 1`, 0`, 1`+ `+
  • .\n 0 \; .n e* store the input as n, push 0, execute the next line n times...
    • dup ( dup ( 1, ) e& ) `' `flat top of stack with 1s replaced with 1 1
    • 1`, 0`, 1`+ `+ prepend 0 1, append 1, append result to existing sequence
\$\endgroup\$
3
\$\begingroup\$

Jelly, 13 bytes

0;x‘Ø1jƲŻ$$¡ḣ

A monadic Link that accepts n and yields the first n terms of the boolean is-a-quote sequence.

Try it online! Or see a longer prefix (by not heading) here.

How?

Starts with a zero (the initial a) and builds up the sequence exactly as described in the question, using zeros for non-quotes and ones for quotes.

0;x‘Ø1jƲŻ$$¡ḣ - Link: integer, n
0             - set the left argument, x, to zero
           ¡  - repeat n times:
          $   -   last two links as a monad - f(x):
         $    -     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
\$\endgroup\$
3
\$\begingroup\$

Haskell, 43 bytes

f n=iterate(\x->x++'b':show x)"a"!!n!!n<'a'

Attempt This Online!

f n is the nth element of sequence #1, as either True or False.

The string built by iterate has the same "shape" as the real string:

ab"a"b"ab\"a\""b"ab\"a\"b\"ab\\\"a\\\"\""…
a,'a','a,''a''','a,''a'',''a,''''a'''''''…

"\ both correspond to quotes, ab correspond to a,. So, we can use char<'a' to detect quotes.

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

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.

0Ç¡ḣ    Main link
 Ç¡     Repeat that n times
0       starting from 0,
   ḣ    and take the first n elements.
\$\endgroup\$
2
\$\begingroup\$

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

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

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.

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

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 because of this answer

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

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

Charcoal, 29 bytes

Nθ≔0ηW›θLη≔⁺⁺η0⪫11⪫⪪η1¦11η…ηθ

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

Nθ

Input n.

≔0η

Start with a string of 0 instead of a.

W›θLψ

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.

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

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

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!

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

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.

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

Haskell, 57 bytes

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

Attempt This Online!

\$\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.