Skip to main content
added 212 characters in body
Source Link
ovs
  • 60.6k
  • 3
  • 47
  • 161

Ruby, 5656 52 bytes

-2 bytes thanks to Jo King!
-2 bytes thanks to dingledooper!

->n{d=[];v=0;nv,*d=0;n.times{|i|d<<v+=v<i||(d|i|d<<v+=v<i||d!=d-[v-i]!=d)?i:-i};d}

Try it online!Try it online!

Commented:

->n{ ... }                         # lambda function taking an integer
  d=[];v=0v,*d=0                           # initialize list of sequence values d and previous value v
  n.times{|i| ... }                # run the following code for i = 0,1,...,n-1:
          v<i||(dv<i||d!=d-[v-i]!=d)?i:-i     #   i IF (v < i OR v-id in!= (d without v-i)) ELSE -i
       v+=                         #   add this to v
    d<<                            #   and append the new value of v to d
  d                                # return d

Ruby, 56 bytes

->n{d=[];v=0;n.times{|i|d<<v+=v<i||(d-[v-i]!=d)?i:-i};d}

Try it online!

Commented:

->n{ ... }                         # lambda function taking an integer
  d=[];v=0                         # initialize list of sequence values d and previous value v
  n.times{|i| ... }                # run the following code for i = 0,1,...,n-1:
          v<i||(d-[v-i]!=d)?i:-i   #   i IF (v < i OR v-i in d) ELSE -i
       v+=                         #   add this to v
    d<<                            #   and append the new value of v to d
  d                                # return d

Ruby, 56 52 bytes

-2 bytes thanks to Jo King!
-2 bytes thanks to dingledooper!

->n{v,*d=0;n.times{|i|d<<v+=v<i||d!=d-[v-i]?i:-i};d}

Try it online!

Commented:

->n{ ... }                         # lambda function taking an integer
  v,*d=0                           # initialize list of sequence values d and previous value v
  n.times{|i| ... }                # run the following code for i = 0,1,...,n-1:
          v<i||d!=d-[v-i]?i:-i     #   i IF (v < i OR d != (d without v-i)) ELSE -i
       v+=                         #   add this to v
    d<<                            #   and append the new value of v to d
  d                                # return d
Source Link
ovs
  • 60.6k
  • 3
  • 47
  • 161

Ruby, 56 bytes

->n{d=[];v=0;n.times{|i|d<<v+=v<i||(d-[v-i]!=d)?i:-i};d}

Try it online!

Commented:

->n{ ... }                         # lambda function taking an integer
  d=[];v=0                         # initialize list of sequence values d and previous value v
  n.times{|i| ... }                # run the following code for i = 0,1,...,n-1:
          v<i||(d-[v-i]!=d)?i:-i   #   i IF (v < i OR v-i in d) ELSE -i
       v+=                         #   add this to v
    d<<                            #   and append the new value of v to d
  d                                # return d