0
\$\begingroup\$

Challenge

Given a string, make it into a triangle as shown below:

Input

Hello, world!

Output

H
He
Hel
Hell
Hello
Hello,
Hello, 
Hello, w
Hello, wo
Hello, wor
Hello, worl
Hello, world
Hello, world!

The first line contains the first character of the string. The following lines contain one more character on each line, until the full length of the original string is reached.

If you receive a one character string, just output it.

You will never receive an empty string.

Rules

  • Standard loopholes apply, as usual
  • This is , shortest solution wins.
\$\endgroup\$
6
  • \$\begingroup\$ Have we really not had this challenge before? \$\endgroup\$
    – xnor
    Jul 8 '17 at 1:55
  • \$\begingroup\$ @xnor Not from my searching \$\endgroup\$
    – anna328p
    Jul 8 '17 at 1:56
  • \$\begingroup\$ Second half of this \$\endgroup\$
    – Stephen
    Jul 8 '17 at 2:01
  • \$\begingroup\$ @WheatWizard Oooops \$\endgroup\$
    – anna328p
    Jul 8 '17 at 2:02
  • \$\begingroup\$ Trailing newline OK? (Is there a default?) \$\endgroup\$
    – xnor
    Jul 8 '17 at 2:05

11 Answers 11

4
\$\begingroup\$

Jelly, 3 bytes

;\Y

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ and of course Dennis crushes the competition within 3 minutes. GG \$\endgroup\$
    – anna328p
    Jul 8 '17 at 1:55
  • \$\begingroup\$ @Mendeleev By the way, even APL gets three bytes, `↑,\` \$\endgroup\$
    – user41805
    Jul 8 '17 at 5:04
2
\$\begingroup\$

brainfuck, 23 bytes

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

Try it online!

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

Python, 33 bytes

f=lambda s:s and f(s[:-1])+s+'\n'

Try it online!

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

J, 2 bytes

[\

Tailor made for J. This is just a scan \ using the identity function [.

Try it online! -- Note the extra space in the output on line 1 is just a quirk of TIO in this case. It does not appear when run in jconsole.

\$\endgroup\$
3
  • \$\begingroup\$ Better formatting: tio.run/##y/r/P03B1kohOuZ/… [\ would count as a tacit verb here. \$\endgroup\$
    – Dennis
    Jul 8 '17 at 5:12
  • \$\begingroup\$ Thanks @Dennis. What did you mean by "count it as a tacit verb"? Is 2 bytes not a fair count according to community rules here? I'm still fairly new to this.... \$\endgroup\$
    – Jonah
    Jul 8 '17 at 5:40
  • \$\begingroup\$ Yes, 2 bytes is fine. We allow anonymous functions, and that's just what [\ is. \$\endgroup\$
    – Dennis
    Jul 8 '17 at 6:09
2
\$\begingroup\$

Haskell, 28 bytes

g[]=[]
g a=(g.init)a++'\n':a

Try it online!

Pretty straight forward. The base case is the empty string and each other case is a recursive call to init to the string with a newline and the input added to the end.

Here are my three attempts at a non-recursive solution all of which are exactly the same length.

g a=concat['\n':take x a|(x,_)<-zip[1..]a]
g a=concat['\n':take(fst x)a|x<-zip[1..]a]
g a=concat['\n':take x a|x<-[1..length a]]

And here's one that I found that is shorter than all of them

g a=do(x,_)<-zip[1..]a;'\n':take x a

I also came up with this very strange solution I quite like (its longer than the others though):

g a=zip[1..](a>>[a])>>=('\n':).uncurry take

Try it online!

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

V, 5 bytes

òÄ$xh

Try it online!

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

Python 2, 49 bytes

lambda s:'\n'.join(s[:i]for i in range(len(s)+1))

Try it online!

Meanwhile, I'm like, how is this is impossible right now in my language?!

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

Python 2, 34 bytes

s=''
for c in input():s+=c;print s

Try it online!

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

C (gcc), 57 bytes

i;f(char*s){for(i=0;i<strlen(s);puts(""))write(1,s,++i);}

Try it online!

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

Ruby, 31 + 1 = 32 bytes

Uses -n flag.

a=''
$_.each_char{|i|puts a<<i}

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ Why use a newline instead of a semicolon? Newlines are bytes too... \$\endgroup\$
    – anna328p
    Jul 8 '17 at 2:17
  • \$\begingroup\$ Newlines are more readable. \$\endgroup\$
    – Pavel
    Jul 8 '17 at 2:17
0
\$\begingroup\$

Ruby, 35 bytes

->s{a='';s.each_char{|c|puts a<<c}}
\$\endgroup\$
2
  • \$\begingroup\$ Full program seems to be shorter. \$\endgroup\$
    – Pavel
    Jul 8 '17 at 2:16
  • \$\begingroup\$ @Phoenix I noticed. \$\endgroup\$
    – anna328p
    Jul 8 '17 at 2:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.