| Bytes | Lang | Time | Link |
|---|---|---|---|
| 005 | Vyxal 3 | 250215T004046Z | moon-bot |
| 009 | Uiua | 240423T205606Z | noodle p |
| 058 | AWK | 241202T212952Z | xrs |
| 038 | Wolfram Language Mathematica | 190902T092720Z | att |
| 126 | Gray Snail | 240426T230248Z | DLosc |
| 108 | Rust | 240423T192033Z | Dornteuf |
| 038 | Factor + grouping.extras | 220625T013158Z | chunes |
| 002 | Vyxal j | 220625T012128Z | naffetS |
| 063 | Python 3 | 220111T170340Z | python-b |
| 064 | JavaScript ES6 | 220112T171437Z | Merlin04 |
| 096 | Python 3.8 prerelease | 220110T171424Z | Larry Ba |
| 041 | Knight | 210616T062444Z | Sampersa |
| 033 | x8616 machine code | 210616T020146Z | EasyasPi |
| 043 | Ly | 210615T080806Z | cnamejj |
| 003 | Vyxal j | 210615T064857Z | emanresu |
| 032 | Branch | 210419T044428Z | hyperneu |
| 003 | Canvas | 210419T041452Z | Razetime |
| 041 | Scala | 201103T094937Z | Tomer Sh |
| 063 | Python 2 | 190228T071221Z | ElPedro |
| 106 | Java JDK | 201103T104620Z | Conffusi |
| nan | x8616 machine code | 190301T213339Z | 640KB |
| 028 | .+? | 200220T190442Z | Edgex42 |
| 037 | Keg | 200120T222015Z | lyxal |
| 079 | Fortran GFortran | 200120T173257Z | DeathInc |
| 027 | GolfScript | 200120T130118Z | user8505 |
| 003 | 05AB1E legacy | 190227T105420Z | Kevin Cr |
| 072 | Python 3 | 191031T012859Z | Cello Co |
| 052 | C clang | 190902T100649Z | att |
| 254 | BrainFlak | 190904T090420Z | Dorian |
| 029 | CJam | 190902T085633Z | Edgex42 |
| 052 | Python 2 | 190901T210736Z | Chas Bro |
| 060 | Zsh | 190901T210719Z | GammaFun |
| 078 | C | 190324T174419Z | Yoris |
| 065 | PHP | 190831T101653Z | Night2 |
| 003 | Gaia | 190404T220448Z | Giuseppe |
| 119 | TSQL query | 190404T203005Z | t-clause |
| 010 | Pyth | 190404T044915Z | GammaGam |
| 031 | Perl 6 | 190227T110217Z | Jo King |
| 006 | Husk | 190320T060124Z | Unrelate |
| 091 | Java 8 | 190308T223807Z | Benjamin |
| 143 | SAP ABAP Unicode | 190315T223510Z | Maz |
| nan | Haskell | 190227T205121Z | Joseph S |
| 058 | C gcc | 190228T134457Z | gastropn |
| 059 | C gcc | 190227T104246Z | buttercr |
| 032 | [Assembly nasm | 190302T220106Z | moonhear |
| 037 | Röda | 190302T091920Z | fergusq |
| 072 | BASH + GNU coreutils | 190306T153921Z | Rene |
| 053 | x86 machine code Linux | 190305T203504Z | user3604 |
| 090 | Python 2 | 190228T170034Z | akozi |
| nan | 190228T164228Z | Chronoci | |
| 091 | JavaScript Node.js | 190302T105449Z | Kamil Na |
| 004 | Japt R | 190227T144058Z | Quintec |
| 079 | PHP | 190301T184330Z | 640KB |
| 076 | Nim | 190301T153030Z | aloisdg |
| 100 | Wolfram Language Mathematica | 190301T133107Z | Rainer G |
| 077 | Factor | 190301T120815Z | Galen Iv |
| 036 | JavaScript ES6 | 190227T104628Z | Arnauld |
| 012 | J | 190227T113122Z | Galen Iv |
| 048 | Python 3.8 prerelease | 190227T153325Z | xnor |
| 063 | Wolfram Language Mathematica | 190301T041010Z | Kai |
| 011 | J | 190227T114340Z | Adá |
| 048 | APLNARS | 190228T183650Z | user5898 |
| 062 | Kotlin | 190228T200803Z | snail_ |
| 035 | sed | 190228T005505Z | Sophia L |
| 040 | Julia | 190227T133408Z | Doorknob |
| 004 | Jelly | 190227T153414Z | Conor O& |
| 086 | Java JDK | 190228T123239Z | Olivier |
| 041 | ><> | 190228T114422Z | Emigna |
| 007 | MathGolf | 190228T113512Z | maxb |
| 058 | R | 190227T154949Z | Criminal |
| 032 | brainfuck | 190227T191808Z | Nitrodon |
| 046 | PowerShell | 190228T043040Z | mazzy |
| 036 | Haskell | 190228T050703Z | xnor |
| 061 | F# .NET Core | 190227T222008Z | aloisdg |
| 081 | C# Visual C# Interactive Compiler | 190227T145915Z | aloisdg |
| 007 | Japt R | 190227T103254Z | ASCII-on |
| 076 | C# Visual C# Interactive Compiler | 190227T212036Z | dana |
| 031 | APL+WIN | 190227T193154Z | Graham |
| 006 | Brachylog v2 | 190227T173554Z | ais523 |
| 118 | SNOBOL4 CSNOBOL4 | 190227T161131Z | Giuseppe |
| 015 | Attache | 190227T153556Z | Conor O& |
| 006 | Pyke | 190227T150252Z | Blue |
| 103 | MBASIC | 190227T142445Z | wooshiny |
| 090 | JavaScript Node.js | 190227T142346Z | T. Dirks |
| 086 | R | 190227T141326Z | Sumner18 |
| 008 | Brachylog | 190227T140544Z | Fatalize |
| 078 | Red | 190227T115529Z | Galen Iv |
| 058 | Octave | 190227T140154Z | Luis Men |
| 040 | Ruby | 190227T104440Z | Kirill L |
| 074 | C# Visual C# Interactive Compiler | 190227T111231Z | the defa |
| 008 | MATL | 190227T121953Z | Sanchise |
| 026 | Perl 5 p | 190227T120024Z | Nahuel F |
| 005 | Charcoal | 190227T115233Z | Neil |
| 009 | APL Dyalog Unicode | 190227T114612Z | Adá |
| 052 | Python 2 | 190227T104034Z | TFeld |
Vyxal 3, 5 bytes
-1 byte by lyxal
PæᐕJ”
Somehow longer than the vyxal2 answer because no palindromize :/
PæᐕJ”
P # prefixes
æ # bifurcate : copy and reverse
ᐕ # behead
J” # concat the 2 copies, join on newlines
💎
Created with the help of Luminespire.
I am a bot created by Themoonisacheese, please feed me butter.
Uiua, 13 9 bytes
˜⊂↘1⊸⇌⧅□
Outputs a list of boxed lines.
Explanation: Take the prefixes of the string, and append a reversed copy with the first removed.
AWK, 58 bytes
{for(l=length;i++<l*2;)print substr($0,1,i<=l?i:i-(x+=2))}
{for(l=length;i++<l*2;) # twice as many lines as chars
print substr($0,1, # print from beginning of string
i<=l?i:i-(x+=2))} # first or second half
Wolfram Language (Mathematica), 39 38 bytes
Table[#~Drop~-Abs@i,{i,l=Tr@-1^#,-l}]&
Input a list of characters. Return a list of character lists.
Gray Snail, 126 bytes
INPUT s
POP n a "
"
POP z
L
POP c s [s]
POP _ _[][c]
GOTO [s]
POP _ a _[a][][n]
POP _ z _[n][][z]
GOTO L
OUTPUT [a][][z]
Try it here! (Paste the code, then click Compile Code, then click Execute Program, then enter your input and click Set Input.)
Ungolfed, with comments
INPUT instring
"Set nl to newline character and front to empty string"
POP nl front "
"
"Set both partial and back to empty string"
POP partial back ""
#loop
"Pop the first character from instring and store it in char"
POP char instring [instring]
"Set partial to partial + char"
POP _ partial _[partial][char]
"If instring is now empty, exit the loop"
GOTO #endloop "" [instring]
"Set front to front + partial + nl"
POP _ front _[front][partial][nl]
"Set back to nl + partial + back"
POP _ back _[nl][partial][back]
GOTO #loop
#endloop
"Concatenate and output front + partial + back
(Note that partial now contains the full original string)"
OUTPUT [front][partial][back]
Rust, 108 bytes
|a:&str|for i in(1..=a.len()).chain((1..a.len()).rev()){print!("{}
",a.chars().take(i).collect::<String>())}
Factor + grouping.extras, 38 bytes
[ head-clump dup reverse rest append ]
! "Hello"
head-clump ! { "H" "He" "Hel" "Hell" "Hello" }
dup ! { "H" "He" "Hel" "Hell" "Hello" } { "H" "He" "Hel" "Hell" "Hello" }
reverse ! { "H" "He" "Hel" "Hell" "Hello" } { "Hello" "Hell" "Hel" "He" "H" }
rest ! { "H" "He" "Hel" "Hell" "Hello" } { "Hell" "Hel" "He" "H" }
append ! { "H" "He" "Hel" "Hell" "Hello" "Hell" "Hel" "He" "H" }
Python 3, 65 63 bytes
Returns a list of lines.
lambda s:[s[:[i,2*len(s)-i][i>len(s)]]for i in range(len(s)*2)]
JavaScript (ES6), 64 bytes
Here's another JS solution:
s=>(l=s.length,f=i=>i<l*2?s.slice(0,i>l?l-i:i)+`
`+f(i+1):"")(1)
Python 3.8 (pre-release), 96 bytes
def f(s):
for i in (a:=range(len(s))):print(s[:i+1])
for i in a[::-1][1:]:print(s[:i+1])
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
x86-16 machine code, IBM PC-DOS, 33 bytes
Hexdump:
00000000: bf 82 00 91 89 fa e8 0a 00 74 06 41 eb f8 e8 02 .........t.A....
00000010: 00 e2 fb b4 40 cd 21 b0 0d ae cd 29 b0 0a cd 29 ....@.!....)...)
00000020: c3 .
Commented assembly:
[org 0x100]
start:
; DI <- first command line argument
mov di, 0x0082
; DOS startup state abuse 1: set CX to zero by swapping AX (0x0000)
xchg cx, ax
; DX <- DI
mov dx, di
.loop1:
; A multitasking loop.
; This both prints the string up to the full length and counts
; the chars by scanning for '\r'.
; Print DS:DX(CX)
; We test for the end of string when printing
; the newline by using SCASB.
call putsn
; If the SCASB returned true, break to the end of .loop2
; We don't go to the top because it would print the full
; length string twice.
jz .next
; Increment the length and character counter
inc cx
; Loop to loop1
jmp .loop1
.loop2: ; Shortening loop
; Print
call putsn
.next:
; while (--CX)
loop .loop2
; Fallthrough: prints an empty line (as CX is 0) and exits
; Prints CX chars from DX, followed by a newline.
putsn:
; DOS startup state abuse 2: BX is 0x0000.
; DOS lets you write to STDIN and it still prints to the screen.
; Why? I have no idea. It's very convenient though. Note that
; this behavior is consistent on DOS 6.22 and FreeDOS 1.3rc4.
;
; write(0, argv, CX)
mov ah, 0x40
int 0x21
; Print \r\n with INT 29h
mov al, 13
; Since we have \r in AL, we can use SCASB to check
; for the end of the string when we return.
; Interrupts save the flags.
;
; Yes, this reads out of bounds on the second loop,
; but it is DOS, who cares?
scasb
int 0x29
mov al, 10
int 0x29
ret
To be even with 640KB, I also used the command line arguments, but I made some changes:
- I use
write(21:40) instead ofputs(21:09). While it does require manually printing a newline, it lets me manually control the length just by changing CX. - I count the length of the command line arguments on the fly.
- I did some other tricks.
This abuses the newline rule: it actually prints empty strings at the start and end.
Ly, 43 bytes
0>&ir[sp<l&:rp[o]r(10)o>]<rpr[p&s&o(10)o&l]
The logic here is to go through each character (codepoint in Ly) of the input string, add it to a second stack, print the accumulated characters, then go back to the input. Once the input is exhausted, start with the "whole string" copy and do the reverse (remove a character, print, loop) until it's empty.
0>&ir[sp<l&:rp[o]r(10)o>]<rpr[p&s&o(10)o&l] #
0 # Push 0 on the stack
> # Switch to stack on the right
&i # Read input onto the stack as codepoints
r # Reverse the stack
[ ] # Loop until stack is empty
s # Stash the current char
p # Delete it from the stack
< # Switch to stack on the left
l # Load the stashed character
&: # Duplicate stack on itself
rp # Reverse stack, pop the top (0)
[o] # Print stack until we hit a 0
r(10)o # Reverse the stack, print a LF
> # Switch to stack on the right
< # Time to shrink... Switch left
rpr # Remove 0 at front of stack
[ ] # While stack not empty
p # Delete one character
&s # Stash the whole stack
&o # Print the stack in order
(10)o # Print a LF
&l # Restore the stack
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
,[[<]>[.>][-]++++++++++.,]<[[-]<[<]>[.>][-]++++++++++.<]
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.
Scala, 41 bytes
s=>s.inits.toSeq.reverse++s.inits.drop(1)
Scala, 61 57 bytes
s=>1.to(2*s.size-1).map(x=>s.take(s.size-(s.size-x).abs))
- -4 thanks to user!
Python 2, 68 63 bytes
i=input()
j=[i[:x]for x in range(1,len(i))]
print j+[i]+j[::-1]
-5 thanks to @pppery for spotting an unneeded variable declaration.
Longer than the other Python answers but just another way of doing it.
Java (JDK), 107 106 bytes
(s)->{int l=s.length(),i=0;var r=new String[2*l-1];for(;i<l;r[i]=r[2*l-i-2]=s.substring(0,++i));return r;}
Loops over the characters of the input string and fills the resulting array from both sides.
107 -> 106: i=0 at initialisation so removed from for loop
x86-16 machine code, IBM PC DOS, 44 43 39 bytes
00000000: d1ee ad8b d648 93b7 248a cbd0 e13a d975 .....H..$....:.|
00000010: 01fd ac86 3cb4 09cd 2186 3cb8 0d0e cd10 ....<...!.<.....
00000020: b00a cd10 e2e7 c3 .......
Build and test YT.COM using xxd -r from above.
Unassembled:
D1 EE SHR SI, 1 ; point SI to DOS PSP at 80H (SI intialized at 100H)
AD LODSW ; load input length into AL, SI = 82H
8B D6 MOV DX, SI ; save start of string pointer
48 DEC AX ; remove leading space from string length
93 XCHG AX, BX ; save string length in BL
B7 24 MOV BH, '$' ; put end-of-string marker in BH
8A CB MOV CL, BL ; set up loop counter in CL
D0 E1 SHL CL, 1 ; number of lines = 2 * string length - 1
LINE_LOOP:
3A D9 CMP BL, CL ; does CL = string length?
75 01 JNZ LINE_OUT ; if not, go to output line
FD STD ; otherwise flip DF to descend
LINE_OUT:
AC LODSB ; increment or decrement SI
86 3C XCHG BH, [SI] ; swap current string byte with end of string delimiter
B4 09 MOV AH, 9 ; DOS API display string function
CD 21 INT 21H ; write substring to console
86 3C XCHG BH, [SI] ; restore string byte
B8 0E0D MOV AX, 0E0DH ; AH = 0EH (BIOS tty function), AL = CR char
CD 10 INT 10H ; write CR to console
B0 0A MOV AL, 0AH ; AL = LF char
CD 10 INT 10H ; write LF to console
E2 E6 LOOP LINE_LOOP ; move to next line
C3 RET ; return to DOS
Explanation
Loop 2 * input length - 1 for each row. The DOS API's string display function (INT 21H,9) writes a $-terminated string to the screen, so each time through the loop the character after the last to be displayed is swapped with the end-of-string terminator.
The loop counter is compared with the string length, and if it's greater (meaning the ascending part of the output) the string/swap position is incremented, otherwise it's decremented.
Standalone PC DOS executable program, takes input string from command line.
Output
- -1 byte use
SHR SI, 1instead ofMOV- thanks to gastropner! - -1 byte flipping loop comparison
- -1 byte write newline directly instead of as string
- -1 byte use
XCHGinstead ofMOV - -1 byte use
STD/LODSBto ascend/descend SI pointer
.+?, 28 bytes
((.+).(\n.+)*)
\2\n\1\n\2
$
Thanks to JoKing for giving the original regex, which I then golfed a bit.
Explanation
((.+).(\n.+)*) The full pattern
( ) Makes group 1 the entire match, used in the replacement
(.+). Makes group 2 every character on the first line, except for the last character
(\n.+)* Makes it match the rest, so it all will get replaced
\2\n\1\n\2 Replacement
\2 Group 2 (first line except last character)
\n Newline
\1 Group 1 (entire match)
\n Newline
\2 Group 2 (first line except last character)
$ This match always fails. Makes the first pair repeat until it doesn't match, which happens when the first line has less than 2 characters.
Try it online! (The second command line argument is input)
Keg, 37 bytes
?1&((⑻|⑩")(⑻|')⑹
,)⑺⑺((⑻|⑩")(⑻|')⑺
,)
I decided to try an interesting string shifting approach.
Fortran (GFortran), 79 bytes
character(99)a
read*,a
k=len_trim(a)
print('(a)'),(a(1:k-abs(i-k)),i=0,2*k)
end
Fortran. Always good for string manipulation
05AB1E (legacy), 4 3 bytes
Crossed out 4 is no longer 4 :)
η.∊
Try it online or verify all test cases.
Explanation:
η # Get the prefixes of the (implicit) input-string
.∊ # Vertically mirror everything with the last line overlapping
# (which implicitly joins by newlines in the legacy version of 05AB1E)
# (and output the result implicitly)
In the new version of 05AB1E, and explicit » is required after the η, which is why I use the legacy version of 05AB1E here to save a byte.
3 bytes alternative provided by @Grimy:
ηû»
This version works in both the legacy and new version of 05AB1E.
Try it online (legacy), try it online (new version) or verify all test cases (new version).
Explanation:
η # Get all prefixed of the (implicit) input-string
û # Palindromize each string in this list
» # And then join the list of strings by newlines
# (after which the result is output implicitly)
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))
C (clang), 56 52 bytes
i;f(*s,l){for(i=0;write(1,s,++i<l?i:l--);puts(""));}
-4 thanks to @ceilingcat
Takes a string and its length as arguments.
Brain-Flak, 254 bytes
<>(<()>)<>{({}(<()>))<>{({}<>)<>}{}((()()()()()){})(<()>)<>{({}(<()>))(({}<>)<{({}<>)<>}>{})(<()>)<>{({}<>)<>}{}}{}}<>{({}<>)(<()>)<>{({}<>)<>}{}((()()()()()){})(<()>)<>{({}(<()>))(({}<>)<{({}<>)<>}>{})(<()>)<>{({}<>)<>}{}}{}<>}<>{{}}<>{}{}{({}<>)<>}<>{}
Harder than expected (or maybe i'm just not very talented in Brain-Flak ^^)
Explanation:
<>(<()>)<> push 0 on second stack
### copy words with increasing letter count to second stack
{ while letters on stack
({}(<()>)) push 0 after first letter
<>{ move letters up to next 0 from second to first stack
({}<>)<>
}
{} pop 0
((()()()()()){}) append newline
(<()>) push new 0
<>
### append word from first to second stack
{ While letters on stack
({}(<()>)) push 0 after first letter
( move letter on "third" stack for later use
({}<>) move letter to other stack
<{ move letters up to next 0 from second to first stack
({}<>)<>
}>
{} pop 0
) save letter from "third" stack
(<()>) push new 0
<>{ move letters up to next 0 from second to first stack
({}<>)<>
}
{} pop 0
}
{} pop 0
}
<>
### copy words with decreasing letter count to second stack
{ while letters on second stack
({}<>)(<()>)<> move letter and 0 on first stack
{ move letters up to next 0 from second to first stack
({}<>)<>
}
{} pop 0
((()()()()()){}) append newline
(<()>) push new 0
<>
### append word from first to second stack
{ While letters on stack
({}(<()>)) push 0 after first letter
( move letter on "third" stack for later use
({}<>) move letter to other stack
<{ move letters up to next 0 from second to first stack
({}<>)<>
}>
{} pop 0
) save letter from "third" stack
(<()>) push new 0
<>{ move letters up to next 0 from second to first stack
({}<>)<>
}
{} pop 0
}
{} pop 0
<>
}
### Tidy up
<>
{{}} delete input
<>
{}{} pop 0 and newline
{({}<>)<>} move everything from second to first stack
<>
{} pop newline
CJam, 28 29 bytes
l_,{_A)<oNo}fA_,{_A)~<oNo}fA;
My first CJam answer, so this can probably be golfed quite a lot
Edit: actually made it work correctly, at the cost of a byte
Zsh, 60 bytes
for c (${(s::Oa)1})(($#a))&&a=($c $c$^a $c)||a=$c
<<<${(F)a}
for c (${(s::Oa)1}) # (s::) splits the first parameter, (Oa) reverses order
(( $#a )) && # if $a is not empty, then
a=($c $c$^a $c) || # ...set a to the current character, then the array with
# the current character prepended to each element, else
a=$c # ...set a to the current character
<<< ${(F)a} # print a; (F) joins on newlines
C, 78 bytes
#define f;printf("%.*s\n",i,d));
i;y(char*d){for(;i++<strlen(d)f for(i--;i--f}
PHP, 65 bytes
for(;($n=$argn)!=$s.=$n[$i++];$b="$s
$b")$a.="$s
";echo"$a$n
$b";
Tests: Try it online!
In a loop creates a word with one letter added to it towards input on each iteration until the word has one letter less than the input.
For example if the input is "Hello" the word in first iteration is "H" and on third iteration is "Hel" and on last iteration is "Hell".
This word is appended to $a and prepended to $b with a newline. At the end $a, input, a newline and $b are printed.
T-SQL query, 119 bytes
SELECT left(@,n)FROM(SELECT*,number/2+1n,len(@)k,number%2-.5m
FROM spt_values)x
WHERE type='P'and n+m<k
ORDER BY(n-k)*m
Pyth, 10 bytes
j+._zt_._z
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!
Perl 6, 31 bytes
{[\~](@_)[0...@_-1...0]}o*.comb
Anonymous code block that takes a string and returns a list of lines.
Explanation:
{ }o*.comb # Pass the list of characters into the codeblock
[\~](@_) # Triangular reduce by concatenation
# e.g. The list [1,2,3,4] turns into [1,12,123,1234]
[0...@_-1 # Return the elements from 0 to length of string minus 1
...0] # And back down to 0
Husk, 6 bytes
S+o↔hḣ
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.
Java 8, 92 91 bytes
s->{for(int i=0,l=s.length();i++<l*2-1;System.out.println(s.substring(0,i<l?i:l-(i-l))));};
Try it online!
-1 byte by using a for loop instead of a while
SAP ABAP (Unicode), 143 bytes
FORM i USING s.DATA(w) = 2 * strlen( s ).WHILE w > 0.w = w - 1.IF w < strlen( s ).WRITE:/ s(w).ELSE.WRITE:/ s(sy-index).ENDIF.ENDWHILE.ENDFORM.
Pretty straightforward subroutine. I'm using ABAP's substring access var+offset(length) and the system variable sy-index, which represents the index of the current loop. Other than that it's probably pretty self-explanatory, even though it's ABAP.
SAP ABAP (Non-Unicode), 141 bytes
FORM i USING s.DATA(w) = 2 * strlen( s ).WHILE w > 0.w = w - 1.IF w < strlen( s ).WRITE:/ s(w).ELSE.WRITE:/ s(sy(10)).ENDIF.ENDWHILE.ENDFORM.
Saving two bytes here by accessing the index component of sy like a substring, despite it being an integer value. In Unicode systems, characters are obviously multi-byte values, therefore using offsets is not allowed in mixed-type structures like sy. However, in a non-Unicode (ASCII-based) system, both digits of a number and characters are represented as one byte each, and offsets are allowed for structures like sy.
Conveniently index is the first 10-byte component of structure sy, so we don't need to use an offset. Otherwise we'd not be able to save anything here, as sy+x(10) is just as long as sy-index - or even longer if the offset was >= 10.
Output of both programs is obviously the same, screenshot below.
Haskell, 52 50 44 bytes
f x=unlines$init<>reverse$scanr(\_->init)x x
- -2 bytes by a variant of Ørjan Johansen's trick
- -6 bytes by taking advantage of
instance Semigroup b => Semigroup (a -> b)
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(""));}
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)];}
[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
Röda, 37 bytes
f s{{seq 1,#s-1;seq#s,1}|[s[:_],"
"]}
Explanation:
f s{ /* Function f(s) */
{
seq 1,#s-1; /* Push numbers 1..#s-1 to the stream (#s = length of s) */
seq #s,1 /* --..-- #s..1 --..-- */
}|
[ /* Print the following things for each _ in the stream: */
s[:_], /* Substring of s from 0 to _ */
"
" /* Newline */
]
}
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
x86 machine code (Linux), 53 bytes
00000000: 89cf 31d2 4252 e814 0000 005a 803c 1100 ..1.BR.....Z.<..
00000010: 75f2 4a52 e806 0000 005a 09d2 75f4 c36a u.JR.....Z..u..j
00000020: 015b 6a04 58cd 806a 0ab2 0189 e1b0 04cd .[j.X..j........
00000030: 805e 89f9 c3 .^...
Assembly:
section .text
global func
func: ;Main function with args: char* ecx
;Clear registers
mov edi, ecx ;push pointer to original string
xor edx, edx ;message length, start at 0
loop_a: ;Increasing length loop:
inc edx ;increment message length
push edx ;save message length
call print_text ;print string with newline
pop edx ;get message length
cmp byte [ecx + edx], 0 ;If not at end of string:
jne loop_a ;continue looping
loop_b: ;Decreasing length loop:
dec edx ;decrement message length
push edx ;save message length
call print_text ;print string with newline
pop edx ;get message length
or edx, edx ;If edx != 0:
jne loop_b ;Continue loop
ret
print_text: ;Function to print string (pointer in ecx) with newline:
push 1
pop ebx ;fd of stdout
push 4
pop eax ;syscall #4 (write)
int 0x80 ;run syscall (msg is in ecx)
push 0xA ;push newline
mov dl, 1 ;set message length to 1
mov ecx, esp ;set pointer for syscall to top of stack
mov al, 4 ;syscall #4 (write)
int 0x80 ;run syscall
pop esi ;reset stack pointer
mov ecx, edi ;get pointer to original string
ret
Python 2, 122 90 bytes
A worse solution of @xnor's
u=lambda n,s,b:s[:b-abs(n-b)]+'\n'+u(n+1,s,b)if n<2*b else""
def c(w):print(u(0,w,len(w)))
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
JavaScript (Node.js), 91 bytes
a=>{r=[];for(i=0;++i<a.length;)r.push(a.slice(0,i));return[...r,a,...r.reverse()].join`\n`}
PHP, 79 bytes
function y($s,$l=1){echo$t=substr($s,0,$l)."
",$l<strlen($s)?y($s,$l+1).$t:'';}
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)),"
";
Nim, 83 76 bytes
let s=readline(stdin)
let l=len(s)
for a in 1..l*2-1:echo(s[0..<l-abs(a-l)])
My first time golfing in Nim. All inputs are welcome!
This is a port of my F# and C# answer.
- -7 bytes by using the
[a..<b]instead ofsubstr()(source)
Wolfram Language (Mathematica), 100 bytes
StringJoin[#~Join~Reverse[#][[2;;]]&[Append[Take[#,i],"\n"]~Table~{i,1,Length[#]}&[Characters[#]]]]&
Factor, 77 bytes
: f ( s -- ) dup length [1,b] dup reverse 1 tail append [ head ] with map . ;
JavaScript (ES6), 36 bytes
f=([c,...r],s=`
`)=>c?s+f(r,s+c)+s:s
Commented
f = ( // f is a recursive function taking:
// the input string split into:
[c, // c = next character (may be undefined if we've reached the end)
...r], // r[] = array of remaining characters
s = `\n` // the output string s, initialized to a linefeed
) => //
c ? // if c is defined:
s + // append s (top of the ASCII art)
f(r, s + c) + // append the result of a recursive call to f, using r[] and s + c
s // append s again (bottom of the ASCII art)
: // else:
s // append s just once (this is the final middle row) and stop recursion
J, 12 bytes
]\,[:}.@|.]\
Still 1 byte longer than Adám's
K (oK), 12 11 bytes
-1 byte thanks to ngn
{x,1_|x}@,\
Python 3.8 (pre-release), 48 bytes
lambda s,r='':(l:=[r:=r+c for c in s])+l[-2::-1]
Uses assignment expressions with := to accumulate a list of prefixes and then again to save the result to concatenate its reverse (without the first char).
Python 2, 51 bytes
f=lambda s,l=[]:s and f(s[:-1],[s]+l)or l+l[-2::-1]
We almost have the following nice 45-byte solution, but it has the original string twice and I don't see a short way to fix this.
f=lambda s,l=[]:s and f(s[:-1],[s]+l+[s])or l
Wolfram Language (Mathematica), 63 bytes
(L=2StringLength@#;Do[Print@StringTake[#,Min[n,L-n]],{n,L-1}])&
J, 11 bytes
Anonymous tacit prefix function. Returns a space-padded character matrix.
[:(}:,|.)]\
]\ the list of prefixes
[:(…) apply the following function to that list
|. the reverse list
, prepended with
}: the curtailed (without last item) list
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[⍵]}
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.
sed, 31 35 bytes
:x
h
s/.\n.*\|.$//
/^$/{x;q}
H
G
bx
Explanation
At the beginning of each iteration of the loop, pattern space is some "central chunk" of the desired output, and each loop adds a shortened copy to the top and bottom.
:x
h Copy the current chunk to hold space
s/.\n.*\|.$// Remove the last letter of the first line, and all other lines (if there are any)
/^$/{x;q} If pattern space is empty we're done; output hold space
H Add the shortened line to the end of hold space
G and add the new hold space to pattern space.
bx
Julia, 40 bytes
s->(a=cumprod([s...]);[a;a[end-1:-1:1]])
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.
Jelly, 5 4 bytes
-1 byte thanks to @JonathanAllan!
¹ƤŒḄ
Try it online! I think this is my second Jelly answer? I don't know if this is optimal. I am more convinced of it being optimal. Returns an array of lines.
Explanation
¹ƤŒḄ input: "Hi!"
¹Ƥ prefixes of the input: [["H"], ["H", "i"], ["H", "i", "!"]]
ŒḄ bounce, using each array: [["H"], ["H", "i"], ["H", "i", "!"], ["H", "i"], ["H"]]
Another approach, proposed by @JonathanAllan, is ;\ŒḄ, which cumulatively reduces (\) concatenation (;), which is another way to generate prefixes.
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));}
MathGolf, 7 bytes
£rñ{l<n
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
R, 79 65 62 58 bytes
write(substring(s<-scan(,""),1,c(1:(r=nchar(s)),r-1:r)),1)
-14 by Giuseppe's superior function knowledge
-3 with cleaner indexing
-4 thanks to Nick Kennedy and Giuseppe's move to scan and write
Avoiding loops (and substr) is nice.
brainfuck, 32 bytes
,[[<]>[.>]++++++++++.,[>>]<[-]<]
The same loop is used for both halves of the pattern.
Explanation:
, Take first input character as initial line
[ Until line to output is empty:
[<]> Move to beginning of line
[.>] Output all characters in line
++++++++++. Output newline
, Input next character
[>>] Move two cells right if input character nonzero
<[-] Otherwise remove last character in line
< Move to new last character in line
]
PowerShell, 46 bytes
($l=$args|% t*y|%{($s+=$_);++$i})+$l[$i..0]|gu
PowerShell, 42 bytes (YouTube special, dirty)
It is known that the maximum length of a comment on youtube is 10,000 characters. Ok, use this as the upper limit.
($l=$args|% t*y|%{($s+=$_)})+$l[1e4..0]|gu
Haskell, 36 bytes
foldr(\h l->(h:)<$>[]:l++min[[]]l)[]
Outputs a list of lines.
Haskell, 37 bytes
f[c]=[[c]]
f(h:t)=(h:)<$>"":f t++[""]
F# (.NET Core), 67 61 bytes
let l=s.Length
[1..l*2-1]|>Seq.map(fun i->s.[..l-abs(i-l)-1])
Input is a string and output is a seq<string>
Another solution could be let f(s:string)=for i=1 to s.Length*2-1 do printfn"%s"s.[..s.Length-abs(i-s.Length)-1] for 80ish bytes... I am not sure that it is worth looking into.
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)));}
String as input and output to std
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));}
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 :)
APL+WIN, 31 bytes
Prompts for input of string:
⊃((⍳n),1↓⌽⍳n)↑¨(¯1+2×n←⍴s)⍴⊂s←⎕
Explanation:
(¯1+2×n←⍴s)⍴⊂s create a nested vector of the string of length =1+2x length of string
((⍳n),1↓⌽⍳n)↑¨ progressively select elements from each element of the nested vector
following the pattern 1 2 ...to n to n-1 ... 1
⊃ convert nested vector into a 2d array.
Brachylog (v2), 6 bytes
a₀ᶠ⊆.↔
Function submission, returning an array of lines. Loosely based on @Fatalize's answer.
Explanation
a₀ᶠ⊆.↔
.↔ Find a palindrome
⊆ that contains, in order,
ᶠ all
a₀ prefixes of {the input}
Tiebreak order here is set by the ⊆, which, when used with this flow pattern, prefers the shortest possible output, tiebroken by placing the given elements as early as possible. The shortest possible output is what we want here (due to it not being possible to have any duplicate prefixes), and placing the given elements (i.e. the prefixes) as early as possible will place them in the first half (rounded up) of the output. Given that we're also requiring them to be placed in the same order, we happen to get exactly the pattern we need even though the description we gave Brachylog is very general; the tiebreaks happen to work out exactly right, causing Brachylog to pick the output we want rather than some other output that obeys the description.
SNOBOL4 (CSNOBOL4), 118 bytes
N =INPUT
L =1
1 X =LT(X,SIZE(N)) X + 1 :F(D)
O N ARB . OUTPUT POS(X) :($L)
D X =GT(X) X - 1 :F(END)
L ='D' :(O)
END
There appears to be a bug in this implementation of SNOBOL; attempting to replace the label D with the label 2 causes an error, although the manual for Vanilla SNOBOL indicates that (emphasis added)
If a label is present, it must begin with the first character of the line. Labels provide a name for the statement, and serve as the target for transfer of control from the GOTO field of any statement. Labels must begin with a letter or digit, optionally followed by an arbitrary string of characters. The label field is terminated by the character blank, tab, or semicolon. If the first character of a line is blank or tab, the label field is absent.
My supposition is that the CSNOBOL interpreter only supports a single label that begins with an integer.
Attache, 15 bytes
Bounce@Prefixes
Pretty simple. Bounces (appends reverse without center) the Prefixes of the input.
Alternatively, 21 bytes: Bounce@{_[0..0:~-#_]}, re-implementing prefix.
Pyke, 6 bytes
BE 27 4F 5F 2B 58
- input() (implicit)
.> - prefixes(^) (1 byte)
'O_ - ^[:-1], reversed(^)
+ - [^]
X - "\n".join(reversed(^))
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
JavaScript (Node.js), 90 bytes
This can probably be golfed alot more, Arnauld already has a way shorter one but I had fun atleast!
s=>{a=[];for(c=s.length-1;c--;)a[c]=s.slice(0,c+1);return[...a,s,...a.reverse()].join`\n`}
R, 86 bytes
x=utf8ToInt(scan(,''))
for(i in c(y<-1:sum(x|1),rev(y)[-1]))cat(intToUtf8(x[1:i]),"
")
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.
Brachylog, 8 bytes
a₀ᶠ;Lc.↔
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)
Red, 78 bytes
func[s][repeat n l: length? s[print copy/part s n]loop l[take/last s print s]]
C# (Visual C# Interactive Compiler), 123 109 94 84 74 bytes
Assumes we can return a char array array (I believe we can, as a char array is a valid representation for a string and a string array is a valid representation for multiple lines)
a=>new int[a.Length*2-1].Select((b,i)=>a.SkipLast(Math.Abs(a.Length-i-1)))
MATL, 8 bytes
nZv"G@:)
Please like this post for the smiley :) in the code it took me a lot of time to make.
n % Length of the input string
Zv % Symmetric range ([1 2 ... n ... 1])
" % For each k in above range
G % Push input
@: % Push [1 2 ... k]
) % Index
Charcoal, 5 bytes
G^Lθθ
Try it online! Link is to verbose version of code. Explanation: G draws a filled polygon, ^ specifies that the sides are down right and down left (the polygon then automatically closes itself), Lθ specifies the length of those sides as being the length of the original input and the final θ specifies the fill string.
APL (Dyalog Unicode), 9 bytesSBCS
Anonymous tacit prefix function. Returns list of strings.
(⊢,1↓⌽),\
,\ the list of prefixes (lit, the cumulative concatenation)
(…) apply the following function to that list:
⌽ the reversed list
1↓ drop the first item
, prepend
⊢ the unmodified list
Python 2, 60 52 bytes
f=lambda s,n=1:s[n:]and[s[:n]]+f(s,n+1)+[s[:n]]or[s]
Python 3.8 (pre-release), 50 bytes
f=lambda s,n=1:s>(x:=s[:n])and[x,*f(s,n+1),x]or[s]


