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

1 2
0
\$\begingroup\$

Python 3, 55 bytes

lambda a:[a[:i]for i in range(len(a)+1)for j in' '*5*i]

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Clean, 47 bytes

import StdEnv
$s=[repeatn(5*i)c\\c<-s&i<-[1..]]

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Jelly, 9 bytes

WẋL×5ƊƊƤẎ

Try it online!

-2 bytes thanks to Mr. Xcoder

\$\endgroup\$
0
\$\begingroup\$

Tidy, 30 bytes

{k:c(k)*5*count(k)}on prefixes

Try it online!

Returns a list of list of characters. If that is not acceptable, the following also works:

Tidy, 37 bytes

{k:out on c(k)*5*count(k)}on prefixes

Try it online!

Explanation

{k:c(k)*5*count(k)}on prefixes
{k:               }on prefixes     over each prefix k:
   c(k)                              the singleton list k
       *5                            ...repeated 5 times
         *count(k)                   ...repeated by the number of elements in the prefix
\$\endgroup\$
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\$
0
\$\begingroup\$

Pushy, 10 bytes

@L:OvL5*:"

Try it online!

                   \ Implicit: Input on stack, as character codes
@                  \ Reverse stack
 L:                \ length(stack) times do:
   Ov              \   Move TOS to auxiliary stack
     L5*:          \   5 * length(auxiliary stack) times do:
         "         \     Print the auxiliary stack as a string
\$\endgroup\$
0
\$\begingroup\$

brainf**k 93 81 76 bytes

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

Try it online!

Saved 12 bytes by removing a never-used empty cell. Saved another 5 bytes due to the help of Dorian (thanks!). Did not update the "How it works" section.

How it works

[-][ https://codegolf.stackexchange.com/questions/170872/tower-of-strings ]
[-][ -------------------------------------------------------------------- ]
[-][ Variables                                                            ]
[-][ p0: temp0                                                            ]
[-][ p1: temp1                                                            ]
[-][ p2: {LF}                                                             ]
[-][ p3: n input characters                                               ]
[-][ p4: zero                                                             ]
[-][ p5... inputs                                                         ]


p0    [-]
p1  > [-]
p2  > ++++++++++
p3  > + 
p4  > [-]
p5  > ,   # read input
      [   # open loop 
p4      [<] # go back to p4
p3       <
## loop current number of inputs
         [ 
p2         <
p1         < + # copy p3 to p1
p0         < +++++ # use p0 to count to 5
          [
p1         >
p2         > 
p3         >
p4         >
p5         >
           [ ## output all until empty
             . >
           ] <
p4         [<] #back to p4
p3         <
p2         < . #output {LF}
p1         < 
p0         <-
          ]
p1         >
p2         > 
p3         > -
         ]
## copy p1 back to p3
p2       <
p1       <
         [ -
p2         >
p3         > +
p2         <
p1         <
         ]
p2        >
p3        > + # increase p3
p4        >
next     >[>] ,  # read next character
      ]

Shorter than Java ;-)

\$\endgroup\$
  • \$\begingroup\$ Hi and welcome to codegolf. You can remove the starting >>. Tio can handle negative indexes. And you can remove the [-], since that cell is already 0. \$\endgroup\$ – Dorian Dec 16 '19 at 9:27
0
\$\begingroup\$

C# (.NET Core), 89 bytes

Without LINQ.

x=>{int i=0;while(i++<x.Length)for(var j=0;j++<i*5;)Console.WriteLine(x.Substring(0,i));}

Try it online!

\$\endgroup\$
0
\$\begingroup\$

PHP, 70 bytes

If it's okay to print the strings like h he hel hell hello then this is a valid answer. Otherwise, I might need to add more code to make the square brackets, quotes and commas.

for(;$x<strlen($argn);)echo str_repeat(substr($argn,0,++$x)."
",5*$x);

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Wenyan, 350 326 308 bytes

吾有一術名之曰「A」欲行是術必先得一言曰「B」是術曰有數零名之曰「C」吾有一言名之曰「D」凡「B」中之「E」加五於「C」昔之「C」者今其是矣加「E」於「D」昔之「D」者今其是矣為是「C」遍夫「D」書之云云云云是謂「A」之術也

IDE

Just seen this programming language on GitHub, so I'll have a try. There is no equivalent for "process.argv", so I have to write this as a function, but then the boilerplate 吾有一術名之曰「X」欲行是術必先得一言曰「X」乃行是術曰...是謂「X」之術也 itself (114 bytes) is just too long.

Each variable has to be at least 7 bytes (the brackets themselves take 6 bytes), and a string takes 12 bytes + length of string to achieve. Of course I can trivially put something like 施「eval」於「「a=>[...a].map(_=>(a.slice(0,++i)+'\\n').repeat(i*5),i=0).join``」」 (91 bytes) but that's boring (why not use JavaScript then?)

Explanation

function A(B) {
 var C = 0, D = "";
 for (var E of B) {
  C = C + 5;
  D = D + E;
  for (var i = 0; i < C; i++)
   console.log(D);
 }
}

326->308 乃行 before 是術曰 is optional so it's removed, and 吾有一言曰「D」 is replaced by 夫「D」 to recall the variable.

\$\endgroup\$
1 2

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.