6
\$\begingroup\$

Your inputs are a string and an integer. Your output is the 2d matrix of strings achieved from adding 0 to N spaces between each character and then deleting 0 to M length segments (leaving one char before each segment) for each permutation of N and M up to the integer input. (eg, when N == M you get back the original string)

Examples

Input of 0, edge case:
edge case

Input of 5, abcdefghijklmnopqrstuvwxz, one output per line, 'columns' of the 'matrix' separated with an extra newline for readability:

abcdefghijklmnopqrstuvwxz
a     f     k     p     u     
a  c  e  g  i  k  m  o  q  s  u  w  z 
a b c d e f g h i j k l m n o p q r s t u v w x z 
a  b  c  d  e  f  g  h  i  j  k  l  m  n  o  p  q  r  s  t  u  v  w  x  z  
a     b     c     d     e     f     g     h     i     j     k     l     m     n     o     p     q     r     s     t     u     v     w     x     z     

a    g    m    s    z
abcdefghijklmnopqrstuvwxz
a    e    i    m    q    u    z 
a    d    g    j    m    p    s    v    z 
a    c    e    g    i    k    m    o    q    s    u    w    z  
a    b    c    d    e    f    g    h    i    j    k    l    m    n    o    p    q    r    s    t    u    v    w    x    z    

a d g j m p s v z
a   f   k   p   u   
abcdefghijklmnopqrstuvwxz
a   d   g   j   m   p   s   v   z 
a b c d e f g h i j k l m n o p q r s t u v w x z 
a   b   c   d   e   f   g   h   i   j   k   l   m   n   o   p   q   r   s   t   u   v   w   x   z   

acegikmoqsuwz
a  f  k  p  u  
a  e  i  m  q  u  z
abcdefghijklmnopqrstuvwxz
a  c  e  g  i  k  m  o  q  s  u  w  z 
a  b  c  d  e  f  g  h  i  j  k  l  m  n  o  p  q  r  s  t  u  v  w  x  z 
 
adgjmpsvz
a f k p u 
acegikmoqsuwz
a d g j m p s v z
abcdefghijklmnopqrstuvwxz
a b c d e f g h i j k l m n o p q r s t u v w x z 

agmsz
afkpu
aeimquz
adgjmpsvz
acegikmoqsuwz
abcdefghijklmnopqrstuvwxz

Here's a pastebin of output for 59, 1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ~

Valid programs receive one integer and one string as input, and output (I+1)^2 strings corresponding to each permutation of N and M from 0 to I. The order of the strings you output does not matter. Whitespace after the last printable character in each string also does not matter.

This is , so shortest answer in bytes per language wins.

\$\endgroup\$
3
  • \$\begingroup\$ sandbox \$\endgroup\$
    – guest4308
    Commented Apr 29 at 4:10
  • \$\begingroup\$ Is a function that produces a 2d array of the resulting lines acceptable or must it be a 1d array of lines? \$\endgroup\$ Commented Apr 29 at 17:57
  • 1
    \$\begingroup\$ @JonathanAllan as long all the strings are there you should be fine; I don't think the finagling of output into a specific format is as interesting as the actual creation of the output. \$\endgroup\$
    – guest4308
    Commented Apr 29 at 22:28

10 Answers 10

3
\$\begingroup\$

Python, 65 bytes

lambda n,s:[(i//-~n*" ").join(s)[::i%-~n+1]for i in range(~n*~n)]

Attempt This Online!

Outputs in reverse order

\$\endgroup\$
4
  • 2
    \$\begingroup\$ 68 bytes by merging the loops \$\endgroup\$ Commented Apr 29 at 10:51
  • 2
    \$\begingroup\$ 67 bytes \$\endgroup\$ Commented Apr 29 at 11:50
  • 1
    \$\begingroup\$ 65 bytes (from Mukudan314's) \$\endgroup\$
    – Arnauld
    Commented Apr 29 at 14:38
  • \$\begingroup\$ Thanks both of you. \$\endgroup\$
    – corvus_192
    Commented Apr 29 at 15:27
3
\$\begingroup\$

Jelly, 11 bytes

If it were required a byte could be appended to yield a flat list of the lines rather than a table TIO.

Ż⁶xⱮ⁹jⱮmþ‘{

A dyadic Link that accepts the non-negative integer on the left and the list of characters on the right, and yields a table of the divisibility patterns.

Try it online! (Footer reorders to, and formats as, the challenge example.)

How?

Ż⁶xⱮ⁹jⱮmþ‘{ - Link: integer N, list of characters, S
Ż           - zero range {N} -> [0,1,2,...,N]
 ⁶          - space character
  xⱮ        - map with times -> ["", " ", "  ", ...]
    ⁹jⱮ     - join {S} with each {space-string} -> Spaced-out-Ss
         ‘{ - increment chain's left argument -> N+1
        þ   - table of {Spaced-out-Ss} × {Sizes = range(N+1) -> [1..N+1]} with ×:
       m    -   modulo-slice {Spaced-out-S} in steps of size {Size}
\$\endgroup\$
2
\$\begingroup\$

JavaScript (Node.js), 112 bytes

s=>n=>(g=i=>[...--i?g(i):[],([...s].map((t,k)=>a[k*~(i%n)/~(i/n)]=t,a=[]),[...a].map(c=>c||' ').join``)])(++n*n)

Try it online!

Silly

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

Charcoal, 18 17 bytes

E⊕θE⊕θΦ⪫η× ι¬﹪ξ⊕λ

Try it online! Link is to verbose version of code. Takes the integer as the first input an the string as the second input and appears to output in reverse order compared to the test case. Explanation:

  θ                 First input
 ⊕                  Incremented
E                   Map over implicit range
     θ              First input
    ⊕               Incremented
   E                Map over implicit range
        η           Second input
       ⪫            Join characters with
                    Literal string ` `
         ×          Repeated by
           ι        Outer value
      Φ             Filtered where
              ξ     Innermost index
             ﹪      Modulo
                λ   Inner value
               ⊕    Incremented
            ¬       Is zero
                    Implicitly print
\$\endgroup\$
2
\$\begingroup\$

Perl 5 + -lF -M5.10.0, 54 bytes

//,map{$"=" "x$';say"@F"=~s/.\K.{$_}//gr}@;for@;=0..<>

Try it online!

Explanation

A pretty literal run through of the requirements. Iterates for the range of 0 to I (<> reads STDIN) storing in @;. For each M in the range, first perform an empty m//atch (//) against M ($_ is implicitly set in the postfix for and m// implicitly matches against $_ when not specified) which stores the string after the match in $' (since the m//atch is empty, this stores the whole string in $'), then map over the range (@;) setting the record separator ($") to M ($') spaces, and then printing (say which includes a trailing newline) the result of interpolating the input (which is an implicit list in @F from -F), which is implicitly joined by $" (the required number of spaces) and s///ubstituting any character ., followed by N other characters with nothing. The \K in the search ensures the first char is kept.

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

Retina, 90 86 bytes

.+(¶(.+))
$2$&*$1
(?<=(¶.*)+.)(?=.)
$#1* 
%(`.+
$&$+0*$(¶$&
(.)(?<=(¶.*)+)(?<-2>.)+
$1

Try it online! Takes the integer as the first input and the string as the second input. Explanation:

.+(¶(.+))
$2$&*$1

Create copies of the input.

(?<=(¶.*)+.)(?=.)
$#2* 

Space each copy out according to its line number.

%(`

Apply to each spaced-out copy separately.

.+
$&$+0*$(¶$&

Create copies of the spaced-out copies.

(.)(?<=(¶.*)+)(?<-2>.)+
$1

Delete segments according to the line number of the copy within its group.

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

JavaScript (ES6), 80 bytes

Expects (integer)(string), where string is passed as an array of characters.

(n,p=q=++n)=>g=(s,k=0,c=k%p?" ":s[k/p])=>c?c+g(s,k+q):--p||--q*(p=n)?`
`+g(s):""

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ looks like this one deletes the chars before it adds the spaces, which results in a different output. you should see the original string I+1 times and only see a string of regex /a *g *m *s *z/ twice, not six times for the given example. \$\endgroup\$
    – guest4308
    Commented Apr 29 at 15:28
  • 1
    \$\begingroup\$ @guest4308 Should now be fixed. \$\endgroup\$
    – Arnauld
    Commented Apr 29 at 15:57
1
\$\begingroup\$

05AB1E, 12 11 bytes

ÝεúíJ¹>Lδιø

Inputs in the order \$n,s\$, where \$s\$ is a list of characters.
Outputs as a matrix of strings, including trailing spaces.

Try it online. (Feel free to remove the footer to see the actual result.)

Explanation:

Ý            # Push a list in the range [0, first (implicit) input-integer]
 ε           # Map over this range:
  ú          #  Pad each character in the second (implicit) input-list with that amount
             #  of leading spaces
   í         #  Then reverse each inner string so the spaces are trailing
    J        #  Join it together to a single string
     ¹>L     #  Push a list in the range [1,input+1]
        δ    #  Apply double-vectorized:
         ι   #   Uninterleave
          ø  #  Zip/transpose; swapping rows/columns, discarding any trailing items of
             #  unequal sized rows
             # (after which the resulting matrix of strings is output implicitly)

There are some equal-bytes alternatives for ÝεúíJ¹>L, like Ýð×εý¹Ý>; >L©εjíJ®; etc.

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

Scala 3, 117 bytes

A port of @corvus_192's Python answer in Scala.


Golfed version. Attmpt This Online!

(n,s)=>for(i<-0 to n*(n+2))yield{val z=i/(n+1);s.flatMap(_+" "*z).dropRight(z).sliding(1,i%(n+1)+1).flatten.mkString}

Ungolfed version. Attempt This Online!

object Main {
  def transformString(n: Int, s: String): Seq[String] = {
    val results = for (i <- 0 until (n + 1) * (n + 1)) yield {
      val step = i % (n + 1) + 1
      val spaceCount = i / (n + 1)
      val spacedString = s.flatMap(c => c.toString + " " * spaceCount).dropRight(spaceCount).sliding(1, step).flatten.mkString
      spacedString
    }
    results
  }

  def main(args: Array[String]): Unit = {
    val resultStrings = transformString(5, "abcdefghijklmnopqrstuvwxz")
    resultStrings.foreach(println)
  }
}
\$\endgroup\$
0
\$\begingroup\$

sed 4.2.2 -r, 147 bytes

s/[0-9]+/echo {0..&}/e
h
s//s^(.).{,&}^\\1^mgp;g;/g
x
s//echo {0..&}|sed 's%[^ ]%%g;s%.*%s^.* ^^gm;s^.^\\\&\&^gm;H;g%';/ge
G
s/.* /sed -rn '&'<<</e

Try it online!

initially, I tried for an ultra-recursive one-liner [265 bytes] but it turns out escaping things takes a lot of space. even though it makes each individual part longer, each time I removed a subprocess I got some savings: [197 bytes], [185 bytes], [166 bytes], [158 bytes]. each one also re-ordered the output, so there's one nice sed script here for each unique ordering that others have submitted to this question. the string is pretty fragile; many special chars will break the script, and to handle numbers an extra \b needs to be added after the +. it'll also break if the string is a shell command that has output without any input like env

ultimately, this still works by creating a sed script and executing it; that last e flag executes this sed script with the string as input:

sed 4.2.2 -rn, 279 bytes

s^.* ^^gm;s^.^&^gm;H;g
s^.* ^^gm;s^.^& ^gm;H;g
s^.* ^^gm;s^.^&  ^gm;H;g
s^.* ^^gm;s^.^&   ^gm;H;g
s^.* ^^gm;s^.^&    ^gm;H;g
s^.* ^^gm;s^.^&     ^gm;H;g
s^(.).{,0}^\1^mgp;g; s^(.).{,1}^\1^mgp;g; s^(.).{,2}^\1^mgp;g; s^(.).{,3}^\1^mgp;g; s^(.).{,4}^\1^mgp;g; s^(.).{,5}^\1^mgp;g; 

Try it online!

that's what it looks like with 5 as the integer; essentially there's a newline-seperated pattern, then a space-seperated pattern.

\$\endgroup\$

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.