18
\$\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\$
  • \$\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

34 Answers 34

0
\$\begingroup\$

Python 2, 42 bytes

lambda s:s and f(s[:-1])+(s+'\n')*len(s)*5

Try it online!

\$\endgroup\$
  • \$\begingroup\$ You can return as a list of strings for 41 bytes. Or if do input and output as a list of characters, 37 bytes \$\endgroup\$ – Jo King Aug 20 '18 at 8:10
0
\$\begingroup\$

awk, 74 bytes (or 144 for the extra complex output shown in question)

version 1, 74 bytes: that the question seems to allow (each lines looking like: c ca cat )

{for(c=1;c<=length($1);c++)for(n=1;n<=c*5;n++){print substr($1,1,c)};exit}

version 2, 144 bytes: extra complex output ( each lines looking like: ['c'] ['c', 'a'] ['c', 'a', 't'] )

{s=length($1);for(c=1;c<=s;c++)for(n=1;n<=c*5;n++){printf"[";for(i=1;i<=c;i++)printf("%s'%s'",(i>1)?", ":"",substr($1,i,1));printf("]\n")};exit}

Thanks to the exit, this processes only the first line entered on stdin. Getting rid of it also processes multiple lines, one by one, stacking the towers together (and savig 5 bytes).

The default is the above, with only 1 tower (and only the first input line processed)

\$\endgroup\$
  • \$\begingroup\$ @whalalalalalala-chen : I try to respect the output given in the question... as it is unclear what output would be acceptable otherwise (if it could be : c ca cat, without the '[' ']' and "'" , then the answer can be much shorter!) \$\endgroup\$ – Olivier Dulac Aug 20 '18 at 13:28
0
\$\begingroup\$

bash, 89 bytes (or 158 bytes for extra complex output shown as exemple)

version 1: 89 bytes (output cat as lines with: c ca cat )

read a;for i in $(seq 1 ${#a});do for c in $(seq 1 $((i*5)));do echo "${a:0:i}";done;done

version 2 : 158 bytes ( output cat as lines with : ['c'] ['c', 'a'] ['c', 'a', 't'] )

read a;for i in $(seq 1 ${#a});do for c in $(seq 1 $((i*5)));do printf "[$(for j in $(seq 1 $i);do printf ", '${a:j-1:1}'";done)]\n";done;done|sed -e 's/, //'
\$\endgroup\$
  • \$\begingroup\$ @whalalalalalala-chen : I try to respect the output given in the question... as it is unclear what output would be acceptable otherwise (if it could be : c ca cat, without the '[' ']' and "'" , then the answer can be much shorter!) \$\endgroup\$ – Olivier Dulac Aug 20 '18 at 13:27
0
\$\begingroup\$

Pascal (FPC), 96 bytes

var s:string;i,j:word;begin read(s);for i:=1to length(s)do for j:=1to i*5do writeln(s[1..i])end.

Try it online!

Longer than Java :(

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