22
\$\begingroup\$

Given a string of text, output it as a 'tower'.

Each slice of the string (of the form 0:n) is repeated 5*n times, so the first character is repeated 5 times, then the first and the second 10 times, etc.

Examples:

'hello' ->

['h']  
['h']  
['h']  
['h']  
['h']  
['h', 'e']  
['h', 'e']  
['h', 'e']  
['h', 'e']  
['h', 'e']  
['h', 'e']  
['h', 'e']  
['h', 'e']  
['h', 'e']  
['h', 'e']  
['h', 'e', 'l']  
['h', 'e', 'l']  
['h', 'e', 'l']  
['h', 'e', 'l']  
['h', 'e', 'l']  
['h', 'e', 'l']  
['h', 'e', 'l']  
['h', 'e', 'l']  
['h', 'e', 'l']  
['h', 'e', 'l']  
['h', 'e', 'l']  
['h', 'e', 'l']  
['h', 'e', 'l']  
['h', 'e', 'l']  
['h', 'e', 'l']  
['h', 'e', 'l', 'l']  
['h', 'e', 'l', 'l']  
['h', 'e', 'l', 'l']  
['h', 'e', 'l', 'l']  
['h', 'e', 'l', 'l']  
['h', 'e', 'l', 'l']  
['h', 'e', 'l', 'l']  
['h', 'e', 'l', 'l']  
['h', 'e', 'l', 'l']  
['h', 'e', 'l', 'l']  
['h', 'e', 'l', 'l']  
['h', 'e', 'l', 'l']  
['h', 'e', 'l', 'l']  
['h', 'e', 'l', 'l']  
['h', 'e', 'l', 'l']  
['h', 'e', 'l', 'l']  
['h', 'e', 'l', 'l']  
['h', 'e', 'l', 'l']  
['h', 'e', 'l', 'l']  
['h', 'e', 'l', 'l']  
['h', 'e', 'l', 'l', 'o']  
['h', 'e', 'l', 'l', 'o']  
['h', 'e', 'l', 'l', 'o']  
['h', 'e', 'l', 'l', 'o']  
['h', 'e', 'l', 'l', 'o']  
['h', 'e', 'l', 'l', 'o']  
['h', 'e', 'l', 'l', 'o']  
['h', 'e', 'l', 'l', 'o']  
['h', 'e', 'l', 'l', 'o']  
['h', 'e', 'l', 'l', 'o']  
['h', 'e', 'l', 'l', 'o']  
['h', 'e', 'l', 'l', 'o']  
['h', 'e', 'l', 'l', 'o']  
['h', 'e', 'l', 'l', 'o']  
['h', 'e', 'l', 'l', 'o']  
['h', 'e', 'l', 'l', 'o']  
['h', 'e', 'l', 'l', 'o']  
['h', 'e', 'l', 'l', 'o']  
['h', 'e', 'l', 'l', 'o']  
['h', 'e', 'l', 'l', 'o']  
['h', 'e', 'l', 'l', 'o']  
['h', 'e', 'l', 'l', 'o']  
['h', 'e', 'l', 'l', 'o']  
['h', 'e', 'l', 'l', 'o']  
['h', 'e', 'l', 'l', 'o']  


'cat' ->

['c']  
['c']  
['c']  
['c']  
['c']  
['c', 'a']  
['c', 'a']  
['c', 'a']  
['c', 'a']  
['c', 'a']  
['c', 'a']  
['c', 'a']  
['c', 'a']  
['c', 'a']  
['c', 'a']  
['c', 'a', 't']  
['c', 'a', 't']  
['c', 'a', 't']  
['c', 'a', 't']  
['c', 'a', 't']  
['c', 'a', 't']  
['c', 'a', 't']  
['c', 'a', 't']  
['c', 'a', 't']  
['c', 'a', 't']  
['c', 'a', 't']  
['c', 'a', 't']  
['c', 'a', 't']  
['c', 'a', 't']  
['c', 'a', 't']  

Rules:

You can output each layer as a list of characters or just a string of them joined together.

\$\endgroup\$
  • 1
    \$\begingroup\$ welcome to PPCG! Nice challenge. \$\endgroup\$ – Giuseppe Aug 19 '18 at 19:37
  • \$\begingroup\$ I tried to clean up the formatting and explain the challenge a little bit better. Did I understand the challenge right? \$\endgroup\$ – Rɪᴋᴇʀ Aug 19 '18 at 19:39
  • 2
    \$\begingroup\$ Can we take the input as a list of characters ? \$\endgroup\$ – JayCe Aug 19 '18 at 19:54
  • 5
    \$\begingroup\$ Can we output a 2D-array of strings like so: [["c","c","c","c","c"],["ca","ca","ca","ca","ca","ca","ca","ca","ca","ca"],...]? \$\endgroup\$ – Shaggy Aug 19 '18 at 20:48
  • 3
    \$\begingroup\$ Are outputs with leading or trailing newlines acceptable? Can we assume inputs do not contain newlines? \$\endgroup\$ – redundancy Aug 20 '18 at 0:12

43 Answers 43

12
\$\begingroup\$

R, 48 bytes

function(s)substring(s,1,rep(x<-1:nchar(s),x*5))

Try it online!

Returns a list of strings.

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ I had missed the obvious golf here! nice solution I tried different approaches but so far all are far longer than this. \$\endgroup\$ – JayCe Aug 19 '18 at 20:21
9
\$\begingroup\$

05AB1E, 6 bytes

ηā5*ÅΓ

Try it online!

Returns a list of string.

Explanation

     ÅΓ # Run-length decode...
η       # ... the prefixes of the input
 ā5*и   # ... with the length range multiplied by 5 -- [5, 10, 15, 20, 25]
|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ @KevinCruijssen Thanks for noticing that ! I should not golf in the morning without a coffee first :-( \$\endgroup\$ – Kaldo Aug 20 '18 at 9:09
  • 1
    \$\begingroup\$ Using run length decoding saves 3 bytes: ηā5*ÅΓ \$\endgroup\$ – Adnan Aug 20 '18 at 10:01
  • \$\begingroup\$ @Adnan Brilliant, thanks ! I think it deserves its own answer though, you've reduced the byte count by 33%... I'll revert to my original solution if you decide to post it yourself. \$\endgroup\$ – Kaldo Aug 20 '18 at 12:06
  • \$\begingroup\$ Nice one, I had ηvyg5*Fy= for 8. \$\endgroup\$ – Magic Octopus Urn Aug 21 '18 at 20:00
7
\$\begingroup\$

Haskell, 36 bytes

f""=[]
f s=f(init s)++(s<$s<*[1..5])

Try it online!

|improve this answer|||||
\$\endgroup\$
6
\$\begingroup\$

Stax, 8 bytes

äï▄;♫├W^

Run and debug it

Unpacked, ungolfed, and commented, it looks like this.

|[F for each prefix of the input
  i^5*  5*(i+1) where i is the iteration index
  DQ    that many times, peek and print to output

Run this one

|improve this answer|||||
\$\endgroup\$
6
\$\begingroup\$

TI-Basic (TI-84 Plus CE), 29 bytes (27 tokens)

For(A,1,length(Ans
For(B,1,5A
Disp sub(Ans,1,A
End
End

Explanation:

For(A,1,length(Ans # 9 bytes, 8 tokens: for A from 1 to the length of the string
For(B,1,5A         # 8 bytes, 8 tokens:  5*A times
Disp sub(Ans,1,A   # 9 bytes, 8 tokens:   Print the first A characters of the string 
End                # 2 bytes, 2 tokens:  end loop
End                # 1 byte,  1 token:  end loop
|improve this answer|||||
\$\endgroup\$
6
\$\begingroup\$

Retina, 15 bytes

.
$.>`*5*$($>`¶

Try it online! Link includes test cases. Explanation:

.

Match each character in the string.

$.>`*5*$($>`¶

$` is the prefix of the match. Retina then provides two modifiers, > modifies it to be in the context of the string between successive matches, while . takes the length. We therefore start with the prefix of the suffix, which is equivalent to the match including its prefix. This saves 2 bytes over using overlapping matches. The $( then concatenates that with a newline, the 5* repeats it, and then the $.>` repeats it a further number of times given by its length.

|improve this answer|||||
\$\endgroup\$
6
\$\begingroup\$

Canvas, 6 bytes

[³5×*P

Try it here!

Explanation:

[      for each prefix
 ³5×     1-indexed counter * 5
    *    repeat the prefix vertically that many times
     P   and print that
|improve this answer|||||
\$\endgroup\$
6
\$\begingroup\$

Brachylog, 15 bytes

a₀ᶠ⟨gj₎{l×₅}⟩ᵐc

Try it online!

The final c can be removed if OP replies positively to the question about outputting 2D arrays.

|improve this answer|||||
\$\endgroup\$
6
\$\begingroup\$

Cubix,  44  40 bytes

i.!?@UBqwW_#/>u...;B^...?qo;;q*n5;oN/./)

Try it online!

This still has a lot of no-ops, but it is a little better than before.

As a very brief description, a character is grabbed from input and tested for EOI (-1), halt if it is. The stack is then reversed. Get the number of items on the stack and multiple by -5. Drop that to the bottom of the stack and clean up. Loop through the stack, printing, until a negative number. Print newline, increment the number, if 0 drop the zero, reverse stack and start from input again, otherwise loop through the stack, printing, until a negative number ... ad nauseum

Cubified it looks like

      i . !
      ? @ U
      B q w
W _ # / > u . . . ; B ^
. . . ? q o ; ; q * n 5
; o N / . / ) . . . . .
      . . .
      . . .
      . . .

Watch it online

|improve this answer|||||
\$\endgroup\$
5
\$\begingroup\$

Jelly, 8 bytes

¹Ƥx'J×5Ɗ

Try it online!

J×5x'@¹Ƥ

Try it online!

¹Ƥx'Jx'5

Try it online!

This is likely golfable.

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ A slightly different 8 is +\ẋ"Jx5Ẏ \$\endgroup\$ – Jonathan Allan Aug 20 '18 at 0:05
5
\$\begingroup\$

JavaScript, 48 46 bytes

(thanks @redundancy)

Edit: The author clarified and this answer is now not valid, but I will leave it here unchanged.

Returns an array of multi-line strings.

s=>[...s].map(c=>(q+=c).repeat(5*++i),i=q=`
`)

Try it

f = s=>[...s].map(c=>(q+=c).repeat(5*++i),i=q=`
`);

console.log( f("hello").join`` );

Potential strategy:

It didn't help me much, but maybe someone can use this:

The number of characters at (0-indexed) line i is floor(sqrt(2/5*i+1/4)+1/2), which is golfed in JavaScript as (.4*i+.25)**.5+.5|0.

For a string of length n, there are n*(n+1)*5/2 lines.

Perhaps: s=>{for(i=0;(n=(.4*i+++.25)**.5+.5|0)<=s.length;)console.log(s.slice(0,n))}

|improve this answer|||||
\$\endgroup\$
  • 1
    \$\begingroup\$ Assuming your output format is valid according to the challenge, you can save 2 bytes as demonstrated here: Try it online! \$\endgroup\$ – redundancy Aug 20 '18 at 0:39
4
\$\begingroup\$

Python 3, 43 41 bytes

Thanks to ovs for saving 2 bytes!

Code

f=lambda x:[*x]and f(x[:-1])+[x]*5*len(x)

Try it online!

|improve this answer|||||
\$\endgroup\$
3
\$\begingroup\$

C (gcc), 67 bytes

i,j;f(char*s){for(i=0;s[i++];)for(j=5*i;j--;)printf("%.*s\n",i,s);}

Try it online!

|improve this answer|||||
\$\endgroup\$
3
\$\begingroup\$

Husk, 8 bytes

ΣzoR*5Nḣ

Try it online!

Explanation

Σz(R*5)Nḣ  -- example input: "ab"
        ḣ  -- non-empty prefixes: ["a","ab"]
 z(   )N   -- zip with [1..]
    *5     -- | multiply by 5
   R       -- | replicate
           -- : [["a","a","a","a","a"],["ab","ab","ab","ab","ab","ab","ab","ab","ab","ab"]]
Σ          -- concat: ["a","a","a","a","a","ab","ab","ab","ab","ab","ab","ab","ab","ab","ab"]
|improve this answer|||||
\$\endgroup\$
3
\$\begingroup\$

Haskell, 46 43 42 bytes

f s=do n<-[1..length s];take n s<$[1..n*5]

Try it online!

Sadly inits requires import Data.List, so

import Data.List
((<$)<*>(>>[1..5])=<<).inits

with its 45 bytes is longer.

Edit: -1 byte thanks to @BWO.

|improve this answer|||||
\$\endgroup\$
3
\$\begingroup\$

Charcoal, 11 bytes

F⊕LθE×⁵ι…θι

Try it online! Link is to verbose version of code. Output includes 0 repetitions of the zero-length substring. Explanation:

   θ          Input string
  L           Length
 ⊕            Incremented
F             Loop over implicit range
      ⁵       Literal 5
       ι      Current index
     ×        Multiply
    E         Map over implicit range
         θ    Input string
          ι   Current index
        …     Chop to length
              Implicitly print each string on its own line
|improve this answer|||||
\$\endgroup\$
3
\$\begingroup\$

Ruby, 46 42 bytes

->s{(1..s.size).map{|i|puts [s[0,i]]*i*5}}

Try it online!

|improve this answer|||||
\$\endgroup\$
3
\$\begingroup\$

PowerShell, 40 20 25 bytes

Score cut in half thanks to mazzy
+5 bytes thanks to AdmBorkBork pointing out the specs

$args|%{,($s+=$_)*5*++$i}

Try it online!

Takes input via splatting. Works by building the string by adding the next character to itself, converts it to a one element array, and then repeats it 5*i times.

|improve this answer|||||
\$\endgroup\$
  • 1
    \$\begingroup\$ param is very expensive. Try to avoid it \$\endgroup\$ – mazzy Dec 16 '19 at 15:35
  • \$\begingroup\$ @mazzy Dang, trying to save the index instead of the char itself led me astray. Thanks. \$\endgroup\$ – Veskah Dec 16 '19 at 15:55
  • \$\begingroup\$ @AdmBorkBork Ha ha, whoops. Should be fixed now \$\endgroup\$ – Veskah Dec 18 '19 at 17:41
2
\$\begingroup\$

MATL, 12 bytes

f"G@:)@5*1X"

Try it online!

f               % Get the indices of input i.e. range 1 to length(input)
 "              % For loop over that
   G            % Push input string
    @           % Push current loop index
     :          % Range 1 to that
      )         % Index at those positions (substring 1 to i)
       @5*      % Multiply loop index by 5
          1X"   % Repeat the substring that many times rowwise
                % Results collect on the stack and are 
                %  implicitly output at the end
|improve this answer|||||
\$\endgroup\$
2
\$\begingroup\$

V, 17 bytes

òïç$îî/6Ä
Hl$xòxú

Expects inputs without newlines, and outputs with superfluous leading newlines.

I can remove this entry if input/output violates the challenge spec.

Try it online!

21 bytes

òïç$îî/6Ä
Hl$xòxíîî/ò

Expects inputs without newlines, but outputs with only one leading and trailing newline.

Explanation

Differing substrings are separated with two consecutive newlines so that linewise duplication only applies to lines matching the regex $\n\n.

When the duplication command (Ä) is supplied a count, e.g. , (I think) it deletes the current line before pasting n times, thus only appearing to append n - 1 copies.

ò         | recursively...
 ï        | . append newline
  ç       | . globally search lines matching...
   $îî    | . . compressed version of $\n\n regex
      /6Ä | . . duplicate to create 6 copies
H         | . go to first line
 l        | . move cursor right 1 char
          | . . if current line is 1 char long, errors out of recursion
  $x      | . delete 1 char from end of current line
    ò     | ...end
     x    | delete extra 1-char substring
      ú   | sort so that newlines rise to top
|improve this answer|||||
\$\endgroup\$
2
\$\begingroup\$

APL (Dyalog Unicode), 14 bytesSBCS

{↑(5×⍳≢⍵)/,\⍵}

Try it online!

My first apl post so please let me know if you have any suggestions

How it works:

{↑(5×⍳≢⍵)/,\⍵}
          ,\⍵  - Prefixes of the input
         /      - Repeated
     ⍳≢⍵        - By a list of indices the same length as the input
   5×           - Times 5
 ↑              - Separate into rows         
|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ isn't really Separate into rows but rather Combine [list of lists] into rows [of a matrix], or more technically Increase rank at the expense of depth. \$\endgroup\$ – Adám Dec 19 '19 at 8:44
1
\$\begingroup\$

Perl 5, 29 bytes

map{say}($x.=$_)x($y+=5)for@F

Try it online!

|improve this answer|||||
\$\endgroup\$
1
\$\begingroup\$

Perl 6, 25 bytes

{(1..*X*5)RZxx[\~] .comb}

Try it online!

Anonymous code block that returns a list of list of strings.

If you want it as a 1D array, you can append flat in front like so:

{flat (1..*X*5)RZxx[\~] .comb}

Try it online!

Explanation:

{                       }  # Anonymous code block
                   .comb   # Split the string into a list of characters
              [\~]         # Triangular reduce the list of characters with the concatenate operator
          RZxx             # Multiply each list by:
 (1..*X*5)                 # A sequence of 5,10,15 etc.

Alternatively,

{($+=5)xx*RZxx[\~] .comb}

Try it online!

Also works for the same amount of bytes.

|improve this answer|||||
\$\endgroup\$
1
\$\begingroup\$

Japt, 10 bytes

Awaiting confirmation as to whether the output format is acceptable (+2 bytes if not).

å+ £T±5 ÇX

Try it

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ Output looks reasonable to me, nicely done. \$\endgroup\$ – Nit Aug 20 '18 at 10:10
1
\$\begingroup\$

Japt, 15 12 bytes

-3 bytes from @Shaggy

£¯°Y +R pY*5

Try it online!

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ 12 bytes (including a small fix) \$\endgroup\$ – Shaggy Aug 20 '18 at 8:18
1
\$\begingroup\$

JavaScript, 76 bytes

s=>{for(i=1;i<=s.length;i++)for(j=0;j<5*i;j++)console.log(s.substring(0,i))}

f=s=>{for(i=1;i<=s.length;i++)for(j=0;j<5*i;j++)console.log(s.substring(0,i))}

f("cat")

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ Hello and welcome to PPCG. \$\endgroup\$ – Jonathan Frech Aug 20 '18 at 15:05
  • \$\begingroup\$ i=1;i<=s.length;i++ can be i=0;++i<=s.length;. \$\endgroup\$ – Jonathan Frech Aug 20 '18 at 15:11
1
\$\begingroup\$

Forth (gforth), 48 bytes

: f 1+ 1 do i 5 * 0 do dup j type cr loop loop ;

Try it online!

Explanation

  1. Loop from 1 to string-length
  2. for each iteration:
    1. Loop (5 * loop index) times
    2. Print string from beginning to outer loop index

Code Explanation

: f                \ start a new word definiton
  1+ 1             \ set up to the loop paramers from 1 to str-length
  do               \ start a counted loop
    i 5 * 0 do     \ start a second counted loop from 0 to 5*index - 1
      dup j        \ duplicate the string address and set the length to the outer index
      type         \ print character from start of string to loop index
      cr           \ output a newline
    loop           \ end inner counted loop
  loop             \ end outer counted loop
;                  \ end word definition
|improve this answer|||||
\$\endgroup\$
1
\$\begingroup\$

Java 10, 120 92 90 89 bytes

s->{for(int j=1,i=1;i<=s.length();i+=++j>i*5?j=1:0)System.out.println(s.substring(0,i));}

-28 bytes thanks to @OlivierGrégoire.
-1 byte thanks to @ceilingcat.

Try it online.

Explanation:

s->{                      // Method with String parameter and no return-type
  for(int j=1,            //  Repeat-integer, starting at 1
      i=1;i<=s.length()   //  Loop `i` in the range [1,length_input]
      ;                   //    After every iteration:
       i+=++j>i*5?        //     Increase `j` by 1 first with `++j`
                          //     If `j` is now larger than `i` multiplied by 5:
           j=1            //      Increase `i` by 1, and reset `j` to 1
          :               //     Else:
           0)             //      Leave `i` the same by increasing it with 0
    System.out.println(   //   Print with trailing newline:
      s.substring(0,i));} //    The prefix of size `i`
|improve this answer|||||
\$\endgroup\$
  • 1
    \$\begingroup\$ 92 bytes: s->{for(int i=1,j=1;i<=s.length();i+=j++<i*5?0:+(j=1))System.out.println(s.substring(0,i));} \$\endgroup\$ – Olivier Grégoire Aug 20 '18 at 9:30
  • \$\begingroup\$ @OlivierGrégoire Thanks! And I've been able to golf 2 more bytes by changing using >= and ?j=1:0 instead of < and ?0:+(j=1). \$\endgroup\$ – Kevin Cruijssen Aug 20 '18 at 9:37
  • \$\begingroup\$ Good! I was trying to get rid of it, but I kept having compilation issues. Didn't think about reverting the condition. Well done! ;) \$\endgroup\$ – Olivier Grégoire Aug 20 '18 at 9:38
  • \$\begingroup\$ @ceilingcat Thanks \$\endgroup\$ – Kevin Cruijssen Dec 14 '19 at 9:52
1
\$\begingroup\$

brainfuck, 40 bytes

++++++++++>,[>>+++++[<<[<]>[.>]>>+<-]<,]

Try it online!

[Tape: 10 (newline), [characters], 0, rowcounter]

++++++++++> 10 (newline)
,[          for each input character
  >>+++++     add 5 to number of rows
  [           for each row
    <<[<]       go to start
    >[.>]       print newline and all previous characters
    >>+         add 1 to next rowcounter cell
    <-          decrement current rowcounter cell
  ]
  <,          input next character
]
|improve this answer|||||
\$\endgroup\$
1
\$\begingroup\$

Ruby, 37 bytes

f=->s{s[0]?f[s.chop]+[s]*s.size*5:[]}

Try it online!

|improve this answer|||||
\$\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.