Skip to main content
deleted 105 characters in body
Source Link
J. Sallé
  • 3.5k
  • 10
  • 24

EDIT: As pointed out by @GalenIvanov, these yield the wrong result because they were made from my original tradfn. I'll leave them here for the time being.

  • Dfn: {⍵⍴(1⍵⍴1,∊{1∊1,⍨⍵⍴0 2 4 6 8}¨⍳⍵⍨¨j⍴¨(+\¯1+j←⍳⍵)⌽¨⊂0,2×⍳4} Try it online!Try it online!
  • Tacit: K⍴1⊢⍴1,∘∊{1∘∊1,⍨⍵⍴0 2 4 6 8}¨∘⍳K←⍨¨⍳⍴¨(⊂0,2×⍳4)⌽⍨¨(+\¯1+⍳) Try it online!Try it online!

EDIT: As pointed out by @GalenIvanov, these yield the wrong result because they were made from my original tradfn. I'll leave them here for the time being.

  • Dfn: {⍵⍴1,∊1,⍨¨j⍴¨(+\¯1+j←⍳⍵)⌽¨⊂0,2×⍳4} Try it online!
  • Tacit: ⊢⍴1,∘∊1,⍨¨⍳⍴¨(⊂0,2×⍳4)⌽⍨¨(+\¯1+⍳) Try it online!
new TIO link
Source Link
J. Sallé
  • 3.5k
  • 10
  • 24

APL (Dyalog Unicode), 52 5959 56 bytes

r←c k r n←1 0r←~n←0 :For j :In ⍳k n+←j-1 r,←1,⍨j⍴n⌽0,2×⍳4 :EndForEnd r←k⍴r 

Try it online!Try it online!

Thanks to @GalenIvanov for pointing out an error in the function. Thanks to @Adám for 3 bytes.

r←c k ⍝ The function c takes the argument k and results in r r n←1 0 ⍝ Initializing the variables r and n, and setting them to 1 and 0, respectively. :For j :In ⍳k ⍝ For loop. ⍳k yields [1, 2, 3, ..., k], and j is the control variable. n+←j-1 ⍝ Accumulates j-1 into n, so it follows the progression (0, 1, 3, 6, 10, 15...) r,←1,⍨j⍴n⌽0,2×⍳4 ⍝ This line is explained below. :EndForEnd  Pretty self explanatory :pEnds the loop r←k⍴r ⍝ return the first k items of r. ⍝ ⍴ actually reshapes the vector r to the shape of k; ⍝ since k is a scalar, ⍴ reshapes r to a vector with k items. 2×⍳4 ⍝ APL is 1-indexed by default, so this yields the vector 2 4 6 8 0, ⍝ Prepend a 0 to it. We now have 0 2 4 6 8 n⌽ ⍝ Rotate the vector n times to the left. j⍴ ⍝ Reshape it to have j items, which cycles the vector. 1,⍨ ⍝ Append a 1, then r,← ⍝ Append everything to r. 

APL (Dyalog Unicode), 52 59 bytes

r←c k r n←1 0 :For j :In ⍳k n+←j-1 r,←1,⍨j⍴n⌽0,2×⍳4 :EndFor r←k⍴r 

Try it online!

Thanks to @GalenIvanov for pointing out an error in the function.

r←c k ⍝ The function c takes the argument k and results in r r n←1 0 ⍝ Initializing the variables r and n, and setting them to 1 and 0, respectively. :For j :In ⍳k ⍝ For loop. ⍳k yields [1, 2, 3, ..., k], and j is the control variable. n+←j-1 ⍝ Accumulates j-1 into n, so it follows the progression (0, 1, 3, 6, 10, 15...) r,←1,⍨j⍴n⌽0,2×⍳4 ⍝ This line is explained below. :EndFor  Pretty self explanatory :p r←k⍴r ⍝ return the first k items of r. ⍝ ⍴ actually reshapes the vector r to the shape of k; ⍝ since k is a scalar, ⍴ reshapes r to a vector with k items. 2×⍳4 ⍝ APL is 1-indexed by default, so this yields the vector 2 4 6 8 0, ⍝ Prepend a 0 to it. We now have 0 2 4 6 8 n⌽ ⍝ Rotate the vector n times to the left. j⍴ ⍝ Reshape it to have j items, which cycles the vector. 1,⍨ ⍝ Append a 1, then r,← ⍝ Append everything to r. 

APL (Dyalog Unicode), 52 59 56 bytes

r←c k r←~n←0 :For j :In ⍳k n+←j-1 r,←1,⍨j⍴n⌽0,2×⍳4 :End r←k⍴r 

Try it online!

Thanks to @GalenIvanov for pointing out an error in the function. Thanks to @Adám for 3 bytes.

r←c k ⍝ The function c takes the argument k and results in r r n←1 0 ⍝ Initializing the variables r and n, and setting them to 1 and 0, respectively. :For j :In ⍳k ⍝ For loop. ⍳k yields [1, 2, 3, ..., k], and j is the control variable. n+←j-1 ⍝ Accumulates j-1 into n, so it follows the progression (0, 1, 3, 6, 10, 15...) r,←1,⍨j⍴n⌽0,2×⍳4 ⍝ This line is explained below. :End  Ends the loop r←k⍴r ⍝ return the first k items of r. ⍝ ⍴ actually reshapes the vector r to the shape of k; ⍝ since k is a scalar, ⍴ reshapes r to a vector with k items. 2×⍳4 ⍝ APL is 1-indexed by default, so this yields the vector 2 4 6 8 0, ⍝ Prepend a 0 to it. We now have 0 2 4 6 8 n⌽ ⍝ Rotate the vector n times to the left. j⍴ ⍝ Reshape it to have j items, which cycles the vector. 1,⍨ ⍝ Append a 1, then r,← ⍝ Append everything to r. 
Code now yields the correct result.
Source Link
J. Sallé
  • 3.5k
  • 10
  • 24

APL (Dyalog Unicode), 5252 59 bytes

r←c k r←1r n←1 0 :For j :In ⍳k n+←j-1 r,←1,⍨j⍴0⍨j⍴n⌽0,2×⍳4 :EndFor r←k⍴r 

Try it online!Try it online!

This is a tradfn (traditional function) taking one argument k and returning the first k items of the sequence.

Thanks to @GalenIvanov for pointing out an error in the function.

r←c k  ⍝ The function c takes the argument k and results in r r←1r n←1 0 ⍝ Initializing the variablevariables r and n, and setting itthem to 1 and 0, respectively. :For j :In ⍳k  ⍝ For loop. ⍳k yields [1, 2, 3, ..., k], and j is the control variable. n+←j-1 ⍝ Accumulates j-1 into n, so it follows the progression (0, 1, 3, 6, 10, 15...) r,←1,⍨j⍴0⍨j⍴n⌽0,2×⍳4 ⍝ This line is explained below. :EndFor  ⍝ Pretty self explanatory :p r←k⍴r  ⍝ return the first k items of r.   ⍝ ⍴ actually reshapes the vector r to the shape of k;   ⍝ since k is a scalar, ⍴ reshapes r to a vector with k items.   2×⍳4 ⍝ APL is 1-indexed by default, so this yields the vector 2 4 6 8   0, ⍝ Prepend a 0 to it. We now have 0 2 4 6 8  n⌽  ⍝ Rotate the vector n times to the left. j⍴  ⍝ Reshape it to have j items, which cycles the vector. 1,⍨  ⍝ Append a 1, then r,←  ⍝ Append everything to r. 

Below are a Dfn(direct function) and a tacit function that also solve the challenge, both kindly provided by @Adám.

EDIT: As pointed out by @GalenIvanov, these yield the wrong result because they were made from my original tradfn. I'll leave them here for the time being.

APL (Dyalog Unicode), 52 bytes

r←c k r←1 :For j :In ⍳k r,←1,⍨j⍴0,2×⍳4 :EndFor r←k⍴r 

Try it online!

This is a tradfn (traditional function) taking one argument k and returning the first k items of the sequence.

r←c k ⍝ The function c takes the argument k and results in r r←1 ⍝ Initializing the variable r and setting it to 1 :For j :In ⍳k ⍝ For loop. ⍳k yields [1, 2, 3, ..., k], and j is the control variable. r,←1,⍨j⍴0,2×⍳4 ⍝ This line is explained below. :EndFor ⍝ Pretty self explanatory :p r←k⍴r ⍝ return the first k items of r. ⍝ ⍴ actually reshapes the vector r to the shape of k; ⍝ since k is a scalar, ⍴ reshapes r to a vector with k items. 2×⍳4 ⍝ APL is 1-indexed by default, so this yields the vector 2 4 6 8 0, ⍝ Prepend a 0 to it. We now have 0 2 4 6 8 j⍴ ⍝ Reshape it to have j items, which cycles the vector. 1,⍨ ⍝ Append a 1, then r,← ⍝ Append everything to r. 

Below are a Dfn(direct function) and a tacit function that also solve the challenge, both kindly provided by @Adám.

APL (Dyalog Unicode), 52 59 bytes

r←c k r n←1 0 :For j :In ⍳k n+←j-1 r,←1,⍨j⍴n⌽0,2×⍳4 :EndFor r←k⍴r 

Try it online!

This is a tradfn (traditional function) taking one argument k and returning the first k items of the sequence.

Thanks to @GalenIvanov for pointing out an error in the function.

r←c k  ⍝ The function c takes the argument k and results in r r n←1 0 ⍝ Initializing the variables r and n, and setting them to 1 and 0, respectively. :For j :In ⍳k  ⍝ For loop. ⍳k yields [1, 2, 3, ..., k], and j is the control variable. n+←j-1 ⍝ Accumulates j-1 into n, so it follows the progression (0, 1, 3, 6, 10, 15...) r,←1,⍨j⍴n⌽0,2×⍳4 ⍝ This line is explained below. :EndFor  ⍝ Pretty self explanatory :p r←k⍴r  ⍝ return the first k items of r.   ⍝ ⍴ actually reshapes the vector r to the shape of k;   ⍝ since k is a scalar, ⍴ reshapes r to a vector with k items.   2×⍳4 ⍝ APL is 1-indexed by default, so this yields the vector 2 4 6 8   0, ⍝ Prepend a 0 to it. We now have 0 2 4 6 8  n⌽  ⍝ Rotate the vector n times to the left. j⍴  ⍝ Reshape it to have j items, which cycles the vector. 1,⍨  ⍝ Append a 1, then r,←  ⍝ Append everything to r. 

Below are a Dfn(direct function) and a tacit function that also solve the challenge, both kindly provided by @Adám.

EDIT: As pointed out by @GalenIvanov, these yield the wrong result because they were made from my original tradfn. I'll leave them here for the time being.

Source Link
J. Sallé
  • 3.5k
  • 10
  • 24
Loading