104
\$\begingroup\$

Hold up..... this isn't trolling.


Background

These days on YouTube, comment sections are littered with such patterns:

S
St
Str
Stri
Strin
String
Strin
Stri
Str
St
S

where String is a mere placeholder and refers to any combination of characters. These patterns are usually accompanied by a It took me a lot of time to make this, pls like or something, and often the OP succeeds in amassing a number of likes.


The Task

Although you've got a great talent of accumulating upvotes on PPCG with your charming golfing skills, you're definitely not the top choice for making witty remarks or referencing memes in YouTube comment sections. Thus, your constructive comments made with deliberate thought amass a few to no 'likes' on YouTube. You want this to change. So, you resort to making the abovementioned clichéd patterns to achieve your ultimate ambition, but without wasting any time trying to manually write them.

Simply put, your task is to take a string, say s, and output 2*s.length - 1 substrings of s, delimited by a newline, so as to comply with the following pattern:

(for s = "Hello")

H
He
Hel
Hell
Hello
Hell
Hel
He
H

Input

A single string s. Input defaults of the community apply. You can assume that the input string will only contain printable ASCII characters.


Output

Several lines separated by a newline, constituting an appropriate pattern as explained above. Output defaults of the community apply. Leading and trailing blank (containing no characters or characters that cannot be seen, like a space) lines in the output are permitted.


Test Case

A multi-word test case:

Input => "Oh yeah yeah"

Output =>

O
Oh
Oh 
Oh y
Oh ye
Oh yea
Oh yeah
Oh yeah 
Oh yeah y
Oh yeah ye
Oh yeah yea
Oh yeah yeah
Oh yeah yea
Oh yeah ye
Oh yeah y
Oh yeah 
Oh yeah
Oh yea
Oh ye
Oh y
Oh 
Oh
O

Note that there are apparent distortions in the above test case's output's shape (for instance, line two and line three of the output appear the same). Those are because we can't see the trailing whitespaces. Your program need NOT to try to fix these distortions.


Winning Criterion

This is , so the shortest code in bytes in each language wins!

\$\endgroup\$
12
  • 24
    \$\begingroup\$ I am planning to make some more YouTube comments related challenges in the future; hence the YouTube Comments #1 in the title. \$\endgroup\$
    – Arjun
    Feb 27, 2019 at 10:31
  • 1
    \$\begingroup\$ Is returning a array of lines allowed? \$\endgroup\$ Feb 27, 2019 at 11:00
  • 2
    \$\begingroup\$ Can we take input as an array of characters and return an array of arrays of characters? \$\endgroup\$
    – Shaggy
    Feb 27, 2019 at 11:43
  • 3
    \$\begingroup\$ Closely related \$\endgroup\$
    – Giuseppe
    Feb 27, 2019 at 15:49
  • 3
    \$\begingroup\$ Can the input be ""? What about a single character like "H"? If so, what should be the output for both of those cases? \$\endgroup\$ Feb 27, 2019 at 20:41

90 Answers 90

3
\$\begingroup\$

PHP, 79 bytes

function y($s,$l=1){echo$t=substr($s,0,$l)."
",$l<strlen($s)?y($s,$l+1).$t:'';}

Try it online!

Recursive in PHP as a function. Ungolfed version:

function y( $s, $l=1 ) {
    echo $t = substr( $s, 0, $l ) . "\n";
    if ( $l < strlen( $s ) ) {
        y( $s, $l+1 );
        echo $t;
    }
}

Call as y('String') outputs:

S
St
Str
Stri
Strin
String
Strin
Stri
Str
St
S

Or 69 bytes iterative with php -nF input as STDIN (basically a port of several other answers).

while(++$x<2*$l=strlen($s=$argn))echo substr($s,0,$l-abs($x-$l)),"
";

Try it online!

\$\endgroup\$
3
\$\begingroup\$

BASH (+ GNU coreutils) 72 bytes

Takes input string from STDIN (one line)

read s;for i in `seq ${#s};seq $((${#s}-1)) -1 1`;do echo ${s:0:$i};done

example:

echo "Oh yeah yeah" | ./script.sh

output:

O
Oh
Oh
Oh y
Oh ye
Oh yea
Oh yeah
Oh yeah
Oh yeah y
Oh yeah ye
Oh yeah yea
Oh yeah yeah
Oh yeah yea
Oh yeah ye
Oh yeah y
Oh yeah
Oh yeah
Oh yea
Oh ye
Oh y
Oh
Oh
O

Explanation:

# read string from STDIN into variable $s
read s

# `seq ${#s}` : sequence of numbers from 1 to length of string followed by
# `seq $((${#s}-1)) -1 1` : sequence of numbers from length-1 downto 1

# loop through sequence
for i in `seq ${#s};seq $((${#s}-1)) -1 1`;do

    # print substring of $s from position 0 to i
    echo ${s:0:$i}

# end of loop
done
\$\endgroup\$
2
  • \$\begingroup\$ You can squish it down to 67 bytes... read s;for i in `seq ${#s};seq $[${#s}-1] -1 1`;{ echo ${s:0:$i};} using some bash golf tricks. Example: ideone.com/Pw94LT \$\endgroup\$
    – roblogic
    Mar 23, 2019 at 5:13
  • \$\begingroup\$ I golfed it down to 65 in bash using weird iterators! Here's my solution... tio.run \$\endgroup\$
    – roblogic
    Mar 23, 2019 at 14:05
3
\$\begingroup\$

[Assembly (nasm, x64, Linux)], 35 32 bytes

This is a function that takes a string (Pointer in RSI) and it's length (number in ebp), and outputs the required string to STDOUT.

EDI and EDX MUST be 0


ytc:
	;Actual setup
	inc ebp
	inc edi ;FD for STDOUT
	push rdi ;Value to add/subtract
	mov bl, 0Ah
.lp:
	add edx, [rsp] ;Str Length +- 1
	jz .end
	cmp edx, ebp
	jne .clp
	push -1
.clp:
	xchg [rsi+rdx-1], bl
	mov al, 1
	syscall
	xchg [rsi+rdx-1], bl
	jmp .lp
.end:
	ret

Try it online!

\$\endgroup\$
3
\$\begingroup\$

C (gcc), 68 67 64 59 bytes

thanks @ceilingcat for -6 thanks @gastropner for -5

i,j;f(char*s){for(j=1;i+=j;puts(""))j-=2*!s[write(1,s,i)];}

Try it online!

\$\endgroup\$
7
  • \$\begingroup\$ Does not appear to be reusable. \$\endgroup\$
    – gastropner
    Feb 28, 2019 at 12:50
  • \$\begingroup\$ @gastropner what do you mean, not reusable? \$\endgroup\$
    – Baldrickk
    Feb 28, 2019 at 17:12
  • \$\begingroup\$ @Baldrickk If you call the function again with a new string, it doesn't work. \$\endgroup\$
    – gastropner
    Feb 28, 2019 at 17:13
  • 3
    \$\begingroup\$ @Baldrickk https://codegolf.meta.stackexchange.com/a/4940/758869 \$\endgroup\$
    – gastropner
    Feb 28, 2019 at 18:32
  • 1
    \$\begingroup\$ 59 bytes \$\endgroup\$
    – gastropner
    Mar 8, 2019 at 14:05
3
\$\begingroup\$

C (gcc), 62 60 58 bytes

-2 bytes by using the write() approach of jaeyong-sung's answer.

i,d;f(char*s){for(d=1;write(1,s,i+=d-=!s[i]*2);puts(""));}

Try it online!

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

Octave, 58 bytes

for k=1:(n=nnz(s=input(''))*2)-1
disp(s(1:min(k,n-k)))
end

Try it online!

\$\endgroup\$
3
  • 1
    \$\begingroup\$ I came up with essentially the same answer in MATLAB before I saw yours, but you get to take shortcuts in Octave with those compound assignments saving several bytes... I did briefly consider if any char maps to zero, such that nnz would miss it? \$\endgroup\$
    – Wolfie
    Mar 1, 2019 at 8:58
  • \$\begingroup\$ @Wolfie Somehow I assumed the input would not contain char(0), but you are right, it might be the case. I've asked for the OP if we can assume the standard ASCII range 32--127 \$\endgroup\$
    – Luis Mendo
    Mar 1, 2019 at 10:28
  • \$\begingroup\$ @Wolfie Confirmed: only printable ASCII \$\endgroup\$
    – Luis Mendo
    Mar 4, 2019 at 9:48
2
\$\begingroup\$

Brachylog, 8 bytes

a₀ᶠ;Lc.↔

Try it online!

Explanation

a₀ᶠ          Find all prefixes of the input
   ;Lc.      The output is that list of prefixes with something unknown appended at the end
      .↔     The output reversed is itself (i.e. it's a palindrome)
\$\endgroup\$
2
\$\begingroup\$

MBASIC, 103 bytes

1 INPUT S$:N=1
2 PRINT LEFT$(S$,N):IF N<LEN(S$) THEN N=N+1:GOTO 2
3 N=N-1:PRINT LEFT$(S$,N):IF N>1 THEN 3
\$\endgroup\$
2
\$\begingroup\$

Pyke, 6 bytes

BE 27 4F 5F 2B 58

Try it here!

        - input() (implicit)
.>      - prefixes(^) (1 byte)
  'O_   - ^[:-1], reversed(^)
     +  - [^]
      X - "\n".join(reversed(^))
\$\endgroup\$
2
\$\begingroup\$

C# (Visual C# Interactive Compiler), 82 81 bytes

x=>{for(int l=x.Length,i=0;i<l*2-1;)WriteLine(x.Substring(0,l-Math.Abs(++i-l)));}

Try it online!

String as input and output to std

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

MathGolf, 7 bytes

£rñ{l<n

Try it online!

Explanation

£         length of array/string with pop
 r        range(0, n)
  ñ       pop(a), push palindromize(a) string/list/number
   {      start block or arbitrary length
    l     read string from input
     <    slice input string at index
      n   newline char
\$\endgroup\$
2
\$\begingroup\$

><>,  44  41 bytes

i:0(?^l&}21.>ao&~
:o}1&:1-&) ?^
l&21.>~{~

Try it online!

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

Julia, 40 bytes

s->(a=cumprod([s...]);[a;a[end-1:-1:1]])

Try it online!

Gets the first half by taking the cumulative product (* is string concatenation in Julia) of the array of characters, then adds this array to itself reversed minus the first element.

Thanks to @Kirill L. for 4 bytes.

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

Kotlin, 62 bytes

{s->s.indices.map{s.take(it+1)}.let{it+it.reversed().drop(1)}}

Could probably be golfed more, but this is what I came up with.

Try it online!

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

VBA, 54 51 Bytes

(-3 bytes now that leading & trailing 0-character lines are confirmed as permitted)

x=Len([A1]):For i=-x To x:?Left([A1],x-abs(i)):Next

Just a simple loop from -Length to Length, omitting that many Absolute characters from the end each time

Input is cell A1 of the ActiveSheet. Output and Code are in the Immediate window

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

Husk, 6 bytes

S+o↔hḣ

Try it online!

Takes input as an argument.

S      Apply the first argument to the third argument and to the second argument applied to the third argument:
 +     concatenation,
 o     the composition of
  ↔    reversal
  h    with removal of the last element,
 ḣ     every prefix of
       the input.

...I may need to not try to explain combinators in plain English.

\$\endgroup\$
1
  • \$\begingroup\$ I feel like there might be a way to shave a byte off this on account of this having used both S and o, but I'm not sure... \$\endgroup\$ Mar 20, 2019 at 6:12
2
\$\begingroup\$

Pyth, 10 bytes

j+._zt_._z

Try it online!

j          # Join the final array with newlines and print
 +         # Join the two resulting arrays: 
  ._z      #   1. All prefixes of the input (z)
     t     #   2. Remove the first element (full word)
      _    #      of the reverse 
       ._z #      of all prefixes of the input (z)

Thanks to ASCII-only for helping get the bytes down!

\$\endgroup\$
5
  • \$\begingroup\$ 16 \$\endgroup\$
    – ASCII-only
    Apr 4, 2019 at 5:14
  • \$\begingroup\$ yours can also be 16 \$\endgroup\$
    – ASCII-only
    Apr 4, 2019 at 5:16
  • 1
    \$\begingroup\$ 10 \$\endgroup\$
    – ASCII-only
    Apr 4, 2019 at 5:24
  • \$\begingroup\$ @ASCII-only The 16 ones have a trailing empty line, I didn't realize that was allowed so I had had added the Ik to fix that. That 10-byte entry is seriously impressive, why not submit it as a seperate answer? \$\endgroup\$
    – GammaGames
    Apr 4, 2019 at 14:09
  • \$\begingroup\$ 1. i don't mind if you post it, 2. don't have time today, 3. it's 90% done by a builtin lol, i don't even know Pyth well \$\endgroup\$
    – ASCII-only
    Apr 4, 2019 at 14:11
2
\$\begingroup\$

C, 78 bytes

#define f;printf("%.*s\n",i,d));
i;y(char*d){for(;i++<strlen(d)f for(i--;i--f}
\$\endgroup\$
1
  • 1
    \$\begingroup\$ 73 \$\endgroup\$
    – ASCII-only
    Apr 4, 2019 at 5:31
2
\$\begingroup\$

Python 3, 79 72 bytes

Thanks Jo King for helping me save 7 bytes

I'm a bit late to this golf, but I was bored and didn't see a Python 3 one that gave a printable string and not a list. This isn't exactly very short but this is my first code golf post :)

lambda x:'\n'.join(x[:[i,len(x)-i][i>len(x)]]for i in range(1,len(x)*2))

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ Welcome to CGCC! Anonymous lambdas are acceptable submissions, so you can remove the f=. You can also change the if statement to a list selection ([i,len(x)-i][i>len(x)]) for a total of 72 bytes. \$\endgroup\$
    – Jo King
    Oct 31, 2019 at 4:00
2
\$\begingroup\$

Scala, 41 bytes

s=>s.inits.toSeq.reverse++s.inits.drop(1)

Try it online!

Scala, 61 57 bytes

s=>1.to(2*s.size-1).map(x=>s.take(s.size-(s.size-x).abs))

Try it online!

  • -4 thanks to user!
\$\endgroup\$
3
  • 2
    \$\begingroup\$ You can use .abs directly on Int to get 57 bytes. \$\endgroup\$
    – user
    Nov 4, 2020 at 14:44
  • 1
    \$\begingroup\$ The 41 byte version is really cool, by the way. Wish I could upvote again. \$\endgroup\$
    – user
    Nov 5, 2020 at 17:32
  • 1
    \$\begingroup\$ Thanks user :) I found it while searching something else. It was pure luck \$\endgroup\$ Nov 5, 2020 at 17:36
2
\$\begingroup\$

Canvas, 3 bytes

[]─

Try it here!

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

Branch, 32 bytes

,[[^]/[./]10.,]^[0^[^]/[./]10.^]

Try it on the online Branch interpreter!

Explanation

,                 Read a character from STDIN
[            ]    While true (while input is not EOF)
 [^]              Keep going up until we hit 0; this is the parent of the first input node
    /             Go to the input node
     [./]         While true, output the node and descend; outputs all input at this point
         10.      Output 10 (newline)
            ,     Read the next character
^                 Go up one space (this puts us on the last input node)
[              ]  While true (while we haven't deleted all of the input yet)
 0^               Zero the current value and go up (delete the last character)
   [^]/           Go to the top again
       [./]       Output all of the input again (gradually gets deleted)
           10.    Output newline
              ^   Go up again; this is the new last input character

This can actually be ported quite easily.

brainf***, 56 bytes

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

Try it online!

Basically does the same thing as above. However, BF can't set the value of a cell like Branch can, so instead we can zero with [-] (which doesn't actually work well in Branch because long long int can take a long long time to zero if its initial value is -1) and then to get 10, we can just do + 10 times after zeroing.

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

Vyxal j, 3 bytes

¦øm

Try it Online!

-3 thanks to lyxal

¦   # Prefixes
 øm # Palindromised
    # Joined by newlines
\$\endgroup\$
2
\$\begingroup\$

Knight, 41 bytes

;=aP;=nF;W<=n+1nLaO GaFn;OaW=aSa-LaT1""Oa

Nothing too fancy, we just print out the first half, then the middle, then the second.

; = a PROMPT # read stdin
; = n FALSE # will coerce to `0` within the `WHILE` condition, but saves us a space from `=n 0`

# until we're at the length of `a`, print the prefix [0..n] from `a`
; WHILE < (=n + 1 n) (LENGTH a)
    OUTPUT (GET a FALSE n)

# output the entire thing
; OUTPUT a

# continue removing pieces from the end of `a` until it's empty, and printing `a` in the process
WHILE (= a SUBS a (- LENGTH a TRUE) 1 "")
    OUPUT a
\$\endgroup\$
1
  • \$\begingroup\$ You should use GaF-LaT. \$\endgroup\$
    – EasyasPi
    Jun 16, 2021 at 15:12
2
\$\begingroup\$

Vyxal j, 2 bytes

¦∞

Try it Online!

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

R, 86 bytes

x=utf8ToInt(scan(,''))
for(i in c(y<-1:sum(x|1),rev(y)[-1]))cat(intToUtf8(x[1:i]),"
")

Try it online!

I'm learning more and more about better ways to manipulate strings in R, so I'm somewhat proud of this answer. The only part I don't like is the for loop portion, which I feel could definitely be golfed.

\$\endgroup\$
5
  • 1
    \$\begingroup\$ what about y<-seq(x)? \$\endgroup\$
    – Droplet
    Feb 27, 2019 at 17:10
  • 2
    \$\begingroup\$ @Droplet you have to be quite careful about seq(x)! seq(x) behaves like seq_along(x) so long as x is not an integer or double of length 1. In that case, if x=c(65), for example, then seq(x) instead returns 1:x, which would not be the right behavior here. It's rather frustrating! \$\endgroup\$
    – Giuseppe
    Feb 27, 2019 at 18:09
  • \$\begingroup\$ You're right, I didn't think of the case when s was just one letter \$\endgroup\$
    – Droplet
    Feb 28, 2019 at 9:57
  • \$\begingroup\$ is the purpose of x=utf8ToInt(scan(,''))? \$\endgroup\$
    – dylanjm
    Mar 20, 2019 at 22:02
  • 1
    \$\begingroup\$ neat y<-seq(a=x) trick that somebody introduced (I forget who) try it online for -2 bytes! \$\endgroup\$
    – Giuseppe
    Apr 4, 2019 at 22:14
1
\$\begingroup\$

C# (Visual C# Interactive Compiler), 76 bytes

x=>{for(int i=0,s=1;s+i>0;s=i<x.Length?s:-s)WriteLine(x.Substring(0,i+=s));}

Try it online!

Using an iterative approach, as opposed to LINQ.

I did not realize until after I posted, but my answer is pretty similar to aloisdg's answer. Although they are different enough, I might just leave mine too :)

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Gald you posted it. I like it! \$\endgroup\$
    – aloisdg
    Feb 27, 2019 at 22:05
1
\$\begingroup\$

Java (JDK), 86 bytes

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

Try it online!

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

APL(NARS), 24 char, 48 bytes

{⊃{w[⍵]}¨k,1↓⌽k←⍳¨⍳≢w←⍵}

test and how to use it:

  h←{⊃{w[⍵]}¨k,1↓⌽k←⍳¨⍳≢w←⍵}
        h ,'1'
1
  h '12'
1 
12
1 
  h '123'
1  
12 
123
12 
1  

comment: it would build one array of ranges [(1) (1 2) (1 2 3) ecc] and the code pass each of them to the function {w[⍵]}

\$\endgroup\$
1
  • \$\begingroup\$ Use and a train, and return a vector of vectors to save: {(⊂¨(+,1↓⌽)⍳¨⍳≢⍵)⌷¨⊂⍵} Try it online! \$\endgroup\$
    – Adám
    Mar 5, 2019 at 23:57
1
\$\begingroup\$

Wolfram Language (Mathematica), 63 bytes

(L=2StringLength@#;Do[Print@StringTake[#,Min[n,L-n]],{n,L-1}])&

Try it online!

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