Posted from here.
This challenge is highly "distilled" from this question. Special thanks to @Akababa!
In this task, you should insert an exclamation mark at the start of the string and after every character.
Rules
- There will always be a non-empty-string input. The input will not contain tabs either. You can assume that the input only contain non-extended ASCII printable characters and newlines.
- The input will not contain trailing newlines as long as your language can't detect a newline.
- This is a code-golf contest; the shortest answer should win.
Examples
- 4 newlines result in 5 newline-delimited exclamation marks. It is very hard to put this as a Markdown text, so this is stated instead.
1 2 3 4 5 6 129591 129012 129127 129582 0
Outputs
!1! !2! !3! !4! !5! !6! !1!2!9!5!9!1! !1!2!9!0!1!2! !1!2!9!1!2!7! !1!2!9!5!8!2! ! !0!
asd afjoK ak:e kPrLd fOJOE; KFO KFkepjgop sgpaoj faj
Outputs
!a!s!d! !a!f!j!o!K! !a!k!:!e! !k!P!r!L!d! ! ! ! ! !f!O!J!O!E!;! ! ! ! ! !K!F!O! !K!F!k!e!p!j!g!o!p! !s!g!p!a!o!j! ! ! !f!a!j!
A base test case with only one character:
a
Outputs
!a!
(Auto-completion! Just kidding, there is no such thing.) Contains exclamation marks:
!! !! !! !! !!
Outputs:
!!!!! !!!!! !!!!! !!!!! !!!!!
97 Answers 97
-
3\$\begingroup\$ Welcome to the site! \$\endgroup\$2019年08月19日 12:37:51 +00:00Commented Aug 19, 2019 at 12:37
-
2\$\begingroup\$ Actually, I created this challenge based on this Retina built-in. \$\endgroup\$user85052– user850522019年08月18日 11:04:05 +00:00Commented Aug 18, 2019 at 11:04
-
\$\begingroup\$ Works in QuadR too. \$\endgroup\$Adám– Adám2019年08月18日 15:01:11 +00:00Commented Aug 18, 2019 at 15:01
-
\$\begingroup\$ -1 bytes in QuadR. \$\endgroup\$user85052– user850522019年08月18日 15:19:08 +00:00Commented Aug 18, 2019 at 15:19
-
2\$\begingroup\$ @A__ Right, I forgot about that feature (If there is only one non-function line...). You may want to re-consider your check-mark. \$\endgroup\$Adám– Adám2019年08月18日 19:02:21 +00:00Commented Aug 18, 2019 at 19:02
Python 3, 27 bytes
lambda s:f"!{'!'.join(s)}!"
Honestly, I hope someone can show me a cool way to do this with a smaller byte count.
-
\$\begingroup\$ This doesnt handle the empty line case correctly \$\endgroup\$flakes– flakes2019年08月19日 04:22:52 +00:00Commented Aug 19, 2019 at 4:22
-
\$\begingroup\$ @flakes What do you mean? If you mean an empty string: we do not need to handle an empty string (and regardless this outputs
!!in that case, which is what makes sense to me). If you mean the string\n: it does, since the correct output is!\n!. \$\endgroup\$Jonathan Allan– Jonathan Allan2019年08月19日 09:08:41 +00:00Commented Aug 19, 2019 at 9:08 -
4\$\begingroup\$ @JAD As far as I can see, it doesn't have an empty string in the examples. Not only that, but the first rule literally states "there will always be a non-empty string input." \$\endgroup\$squid– squid2019年08月19日 11:13:21 +00:00Commented Aug 19, 2019 at 11:13
-
1\$\begingroup\$ Ah I was incorrect. The first example has an empty line in the middle of the input. But this answer will handle placing the exclamation point in the middle of that,
!\n!\n!. nice work. \$\endgroup\$flakes– flakes2019年08月19日 13:18:56 +00:00Commented Aug 19, 2019 at 13:18 -
1\$\begingroup\$ codegolf.stackexchange.com/questions/190223/insert-nbetween/… as you were saying to show a shorter way \$\endgroup\$U13-Forward– U13-Forward2019年08月20日 07:14:42 +00:00Commented Aug 20, 2019 at 7:14
-
\$\begingroup\$ damn, that's badass \$\endgroup\$Cruncher– Cruncher2019年08月19日 20:20:36 +00:00Commented Aug 19, 2019 at 20:20
Labyrinth, (削除) 19 11 10 (削除ここまで) 9 bytes
33
..
",@
How?
We enter the Labyrinth at the top-left facing right with an infinite stack of zeros...
I / O stack
0,0,0,...
3 - pop * 10 + 3 3,0,0,0,...
- 2 neighbours, forward
3 - pop * 10 + 3 33,0,0,0,...
- 2 neighbours, forward
. - pop & print chr ! 0,0,0,...
- T junction from the side
- TOS==0, forward
, - read chr or -1 L 76,0,0,0,... or -1,0,0,0
- T junction from the base
- if TOS > 0 right:
" - no-op 76,0,0,0,...
- 2 neighbours, forward
. - pop & print chr L 0,0,0,...
- T junction from the side
- TOS==0, forward
3 - ...back to the start
- elif TOS == -1 left:
@ - exit we're out!
* right, but on the first occasion (from above) we hit the wall and turn
around, so that's like a left
Luckily we don't need to handle un-printables, otherwise the first zero-byte would turn us around at , and play havoc.
JavaScript (ES6), 19 bytes
Takes input as an array of characters.
s=>`!${s.join`!`}!`
JavaScript (ES6), (削除) 23 (削除ここまで) 20 bytes
Saved 3 bytes thanks to @ShieruAsakoto
Takes input as a string.
s=>[,...s,,].join`!`
JavaScript (ES6), 22 bytes
Suggested by @tjjfvi
Takes input as a string.
s=>s.replace(/|/g,"!")
-
3\$\begingroup\$ Alternative with
.replace, 22 bytes \$\endgroup\$tjjfvi– tjjfvi2019年08月18日 19:12:04 +00:00Commented Aug 18, 2019 at 19:12 -
\$\begingroup\$ @tjjfvi Nifty one! \$\endgroup\$Arnauld– Arnauld2019年08月18日 19:18:24 +00:00Commented Aug 18, 2019 at 19:18
-
5\$\begingroup\$ I've got a 20 for your 23:
s=>[,...s,,].join`!`\$\endgroup\$Shieru Asakoto– Shieru Asakoto2019年08月19日 13:42:38 +00:00Commented Aug 19, 2019 at 13:42
-
\$\begingroup\$ Can shave 3 bytes by switching the function form to
scan(,''), like so tio.run/##K/r/P724NElDSUlHSVFJpzg5MU9DR11dU/O/… \$\endgroup\$Sumner18– Sumner182019年08月19日 13:37:27 +00:00Commented Aug 19, 2019 at 13:37 -
\$\begingroup\$ @Sumner18 thanks. I started with that but it splits input at spaces. \$\endgroup\$Nick Kennedy– Nick Kennedy2019年08月19日 14:54:38 +00:00Commented Aug 19, 2019 at 14:54
-
2\$\begingroup\$ @Sumner18 The challenge asks to handle input with newlines, which can't be done with
scan(but which Nick's solution does handle, at least if you display the output withcat.) \$\endgroup\$Robin Ryder– Robin Ryder2019年08月19日 21:14:53 +00:00Commented Aug 19, 2019 at 21:14
Pepe, 47 bytes
REREEeRErEErREeeEeeeeEREEeeREEeereeREEEEeeEReee
Explanation:
REREEeRE # Push 0,then input (str),then 0 -> (R)
# The zeroes are pushed to correct the inserting
rEE # Begin loop labelled 0 -> (r)
rREeeEeeeeE # Push "!" -> (R)
# r flag inserts it instead of pushing
REEeeREEee # Move pointer pos 2 steps forward -> (R)
ree # Loop while (R) != 0
REEEEeeE # Remove characters of (R) that are in stack of (r)
# Removes the 0 in (R)
Reee # Output (R)
-
\$\begingroup\$ How do you write this code? Is there some code converter that you use? This seems crazy to try and write \$\endgroup\$Cruncher– Cruncher2019年08月19日 20:21:48 +00:00Commented Aug 19, 2019 at 20:21
-
1\$\begingroup\$ @Cruncher 1) I use this as my guide. 2) Nope, I don't use a code converter, I just use the guide to write the code. \$\endgroup\$u-ndefined– u-ndefined2019年08月20日 12:59:52 +00:00Commented Aug 20, 2019 at 12:59
8086 machine code, .COM format (MS-DOS 2+), 32 bytes
(-1 depending on emulator: see below)
For best results redirect standard input from a file, as typing gives odd-looking output due to no buffering; also, newlines look a little weird because they are stored as CR LF, and the CR part messes up the output.
This program behaves fine in an actual MS-DOS emulation (e.g. PCjs) but DOSBox seemed to have issues with Ctrl+Z EOF (see comments in the assembly listing), so DON'T try to enter input using the console in DOSBox unless you add the extra check!
BB 01 00 53 59 BA 0B 01 B4 40 CD 21 4A 4B B4 3F CD 21 85 C0 74 09 B4 40 43 41 CD 21 49 EB EE C3
Some interesting bits:
I saved some data space by reusing memory that had already been executed (the
21HinINT 21Hhappens to be!)I was almost able to use an interesting trick that I found on the page "The Hidden Power of BCD Instructions" which would have allowed me to use
AAAinstead of a standardTESTto compareALto 0, saving one byte. Unfortunately, this is not fully documented so I couldn't rely on it: for example, PCjs doesn't adjust anything but the carry and auxiliary carry flags. :-(
Assembly code (TASM ideal mode):
IDEAL
MODEL TINY
CODESEG
ORG 100H
;; DOSBox (tested with 0.74-2) didn't seem to handle Ctrl-Z as EOF
;; so uncomment the ";;" lines to run it there.
MAIN:
MOV BX,1
PUSH BX
POP CX
MOV DX,OFFSET MAIN_1+1 ; The 21H in INT 21H
MOV AH,40H
MAIN_1:
INT 21H
DEC DX
;;PUSH DX
;;POP SI
IO_LOOP:
DEC BX
MOV AH,3FH
INT 21H
;;; This should work on an non-emulated PC.
;;;AAA ; AL=0?
TEST AX,AX
JZ DONE
;;CMP [BYTE PTR SI],1AH
;;JZ DONE
MOV AH,40H
INC BX
INC CX
INT 21H
DEC CX
JMP IO_LOOP
DONE:
RET
ENDS
END MAIN
Jelly, 5 bytes
Ż"!ṁż
A full program accepting a string, which prints the result.
How?
Ż"!ṁż - Main Link: list of characters, s e.g. "abc"
"! - character '!' '!'
ṁ - mould like:
Ż - s with a zero prepended "!!!!"
ż - zip together with s ["!a","!b","!c",'!']
- implicit (smashing) print !a!b!c!
6502, 12 bytes (13 bytes if Apple II)
6502
The machine code assumes that a pair of zero page locations are connected to character input ($FE) and output (FF) hardware. Many 6502-based systems facilitate I/O in this fashion, albeit I/O address are usually not in zero page.
For simplicity, I used Py65, a 6502 microcomputer system simulator written in Python.
Here is a memory dump from Py65. You can load the following code anywhere in zero page such that it does not overlap $FE and $FF.
PC AC XR YR SP NV-BDIZC
6502: 0000 00 00 00 ff 00110010
.mem 0:b
0000: a9 21 85 ff a5 fe f0 fc 85 ff d0 f4
Running in a Windows command window, you can paste (Ctrl+V) any text you desire, or you can simply type. If typing, press Ctrl+J for a newline (same ASCII char). Press Ctrl+C to interrupt the processor and return to the Py65 command prompt.
Naturally, assembly code is easier to read.
PC AC XR YR SP NV-BDIZC
6502: 0000 00 00 00 ff 00110010
.d 00:0b
0000ドル a9 21 LDA #21ドル
0002ドル 85 ff STA $ff
0004ドル a5 fe LDA $fe
0006ドル f0 fc BEQ 0004ドル
0008ドル 85 ff STA $ff
000ドルa d0 f4 BNE 0000ドル
For clarity, here is the assembly code in CBA65 format.
; ASSEMBLE:
; cba65 bangit
;
; LOAD/RUN
; python3 py65/monitor.py -i 00fe -o 00ff -l bangit.bin
; goto 0000
.FILES BIN=256
; I/O LOCATIONS
GETC .EQU $FE ; (1) MOVING PY65'S GETC TO ZP SHAVES 1 BYTE
PUTC .EQU $FF ; (1) MOVING PY65'S PUTC TO ZP SHAVES 2 BYTES
.ORG 0000ドル
VROOM LDA #'!'
STA PUTC
VROOM2 LDA GETC
BEQ VROOM2
STA PUTC
BNE VROOM
.END
Apple II
The code above assumes a null indicates there is no input, so continues polling until a non-null value is returned.
For comparison, the Apple I and Apple II signals availability of a new character by setting bit 7 of the keyboard I/O address, which then needs to be cleared after fetching the character. On those systems, character I/O usually is performed by calling system monitor routines instead of accessing the hardware directly.
By calling RDKEY ($FD0C) and COUT ($FDED), the Apple II equivalent of the above can be coded in 13 bytes, and is runnable anywhere in RAM. Here is the code I ran in an Apple //e emulator, a2ix on Android 9.
Pressing Return has the same effect as a newline.
*300L
0300- A9 A1 LDA #$A1
0302- 20 ED FD JSR $FDED
0305- 20 0C FD JSR $FD0C
0308- 20 ED FD JSR $FDED
030B- F0 F3 BEQ 0300ドル
Did you notice that instead of the normal ASCII value #21ドル for the exclamation point, #$A1 is used instead? That's because sending standard ASCII values to COUT causes them to be displayed in "inverse mode," black on white. Displaying ASCII in normal white on black requires adding #80ドル to the character value in the accumulator before calling COUT. Because RDKEY returns characters with the hi-bit set, assembly programs generally cleared the bit of the character to obtain its ASCII value before using it.
-
1\$\begingroup\$ Welcome to the site! :) \$\endgroup\$Rahul Bharadwaj– Rahul Bharadwaj2019年08月21日 09:05:42 +00:00Commented Aug 21, 2019 at 9:05
-
\$\begingroup\$ Thank you, @Rahul! \$\endgroup\$lee– lee2019年08月21日 10:55:59 +00:00Commented Aug 21, 2019 at 10:55
-
\$\begingroup\$ Actually the Sed code is just 15 characters there: Try it online!. \$\endgroup\$manatwork– manatwork2019年08月19日 15:11:28 +00:00Commented Aug 19, 2019 at 15:11
-
\$\begingroup\$ Great, thank you. I was unclear how that worked... \$\endgroup\$Jonah– Jonah2019年08月19日 15:13:30 +00:00Commented Aug 19, 2019 at 15:13
-
1\$\begingroup\$
s/\b\|\B/!/galso works for 12 bytes \$\endgroup\$user41805– user418052019年08月20日 07:15:27 +00:00Commented Aug 20, 2019 at 7:15 -
\$\begingroup\$ @Cowsquack thank you. updated. \$\endgroup\$Jonah– Jonah2019年08月20日 12:47:42 +00:00Commented Aug 20, 2019 at 12:47
AWK, 11 bytes
gsub(a,"!")
This is similar to some other entries. It changes a null string to ! globally, using 0ドル as the default target, then prints the result since no other action is defined. Using a works since it's an undefined variable and is shorter than using "" (which would do the same thing).
Perl 5 -p0, (削除) 17 (削除ここまで) 6 bytes
s,,!,g
My original answer was -p and $_='!'.s,.,$&!,gr. Thanks to @Nahuel Fouilleul for cutting 11 bytes and to @Grimy for the -p0 tip.
-
2\$\begingroup\$ 6 bytes \$\endgroup\$Nahuel Fouilleul– Nahuel Fouilleul2019年08月19日 08:32:10 +00:00Commented Aug 19, 2019 at 8:32
-
1\$\begingroup\$ @NahuelFouilleul
-lpgives incorrect output for the\n\n\n\ntest case (returns 4 newline-separated!instead of the specified 5).-p0works correctly. \$\endgroup\$Grimmy– Grimmy2019年08月19日 11:20:56 +00:00Commented Aug 19, 2019 at 11:20
MarioLANG, (削除) 95 (削除ここまで) (削除) 94 (削除ここまで) (削除) 90 (削除ここまで) (削除) 89 (削除ここまで) 69 bytes
++++++
======< >)
>+++++++",+[
=======<.==<
>+++++++!(.-
========#===
First time trying out MarioLANG, that was a lot of fun!
Thanks to Jo King for -20 bytes
Explanation:
So, as the name implies, MarioLANG is made to execute like a game of Super Mario Bros. It operates similarly to BF, with memory arranged in a tape of cells. There are operators to increment, decrement, print (as ascii or numeric) and read into the current memory cell, and operators to move left or right along the tape.
Mario (the instruction pointer) always begins in the top left cell of the program, with his intended direction of motion set to the right. If Mario does not have a floor-like object beneath him (=, ", or #), he will fall until he reaches a floor-like object. If Mario leaves the program space, the program ends due to Game Over :(
This specific program can basically be split into two halves: the setup, and the loop.
Setup Loop
-----------------------------------------------
|
++++++ |
======< | >)
>+++++++ | ",+[
=======< | .==<
>+++++++ | !(.-
======== | #===
In the Setup section, we're simply incrementing the first memory cell until we reach 33 - the ASCII value for "!". Easy enough; if this can be golfed, it's purely a matter of shape. Mario starts from the top left, picks up 10 coins, starts falling when picking up the 11th, switches directions, then repeats. He picks up the last 11 coins without switching directions; he ends the setup section at the bottom-rightmost "+".
In the loop section, Mario starts by reaching an elevator. The "!" operator makes him cease motion, so that he remains on the elevator. On the way up, it prints the corresponding ASCII character to the current memory cell's value (this one is always 33, "!"), then switches to the next cell in memory. Mario reaches the top and sets his direction to the right. He falls, and reads a character from input as its ASCII value (or -1 if no character). We increment because the only measure of control in MarioLANG is to skip an instruction if the current memory cell has a value of 0. If it does, we skip changing Mario's direction, so he will walk right off of the next floor to his doom. If it does not, we set direction to left; walking left off of the floor below decrements the current cell back to its previous value, that value is printed, and we move back to the first memory cell before getting back on the elevator.
Previous version (89 bytes):
+++++++++++>,
==========@"+
+++++++++++)[
@==========.==<
+++++++++++!(.-
===========#===
-
-
\$\begingroup\$ Okay now THAT is cool. I'll update as soon as I have time to redo the explanation, thanks a bunch! \$\endgroup\$squid– squid2019年08月21日 12:41:16 +00:00Commented Aug 21, 2019 at 12:41
-
1
-
\$\begingroup\$ Man, I know it isn't exactly your first rodeo, but this is really impressive. xD \$\endgroup\$squid– squid2019年08月21日 12:45:05 +00:00Commented Aug 21, 2019 at 12:45
-
\$\begingroup\$ Actually, this is my first time golfing MarioLANG. I just have some experience with brainfuck as well as other 2D languages \$\endgroup\$Jo King– Jo King2019年08月21日 12:46:48 +00:00Commented Aug 21, 2019 at 12:46
Perl 6, (削除) 16 (削除ここまで) 11 bytes
{S:g/<(/!/}
Replaces all zero width matches with exclamation marks. Null regexes are not allowed, so we use a capture marker to capture nothing instead
Zsh, (削除) 32 (削除ここまで) 23 bytes
<<<!${(j:!:)${(s::)1}}!
(削除) Try it online!
(削除ここまで)
Try it online!
(s::) splits into characters, (j:!:) joins on !s.
APL (Dyalog Unicode), 11 bytes
(∊⊢⍪⍪ ̈)∘'!'
@Adám's golf to mine (see below).
{'!'⍪∊⍉↑⍵('!'⍴⍨≢⍵)} ⍝ original
{'!'⍪∊⍵⍪ ̈('!'⍴⍨≢⍵)} ⍝ concatenate each instead of transpose mix
{'!'⍪∊⍵⍪ ̈ '!'} ⍝ each already handles many-to-one
{ ⍵ ⍪∊⍺⍪ ̈ ⍵ }∘'!' ⍝ break out "!"
{∊⍵ ⍪ ⍺⍪ ̈ ⍵ }∘'!' ⍝ defer flattening to later
(∊⊢ ⍪ ⍪ ̈ )∘'!' ⍝ go tacit
(∊⊢⍪⍪ ̈)∘'!' ⍝ remove spacing
APL (Dyalog Unicode), 19 bytes
{'!'⍪∊⍉↑⍵('!'⍴⍨≢⍵)}
My first APL submission :P Loving this language. No idea why I need the parentheses after ⍵.
{'!'⍪∊⍉↑⍵('!'⍴⍨≢⍵)}
{ } ⍝ Boilerplate
⍴⍨ ⍝ Repeat
'!' ⍝ Exclamation point
≢⍵ ⍝ len(input) times
⍵( ) ⍝ Wrap ^ into an array with the input
↑ ⍝ Matrixify
⍉ ⍝ Transpose
∊ ⍝ Flatten
⍪ ⍝ Prepend
'!' ⍝ An exclamation point
-
\$\begingroup\$ Congrats on your first APL answer! I'm quite impressed, considering how new you are to APL. With experience, you will be able to golf this to
'!'∘(∊,¨,⊣)\$\endgroup\$Adám– Adám2022年10月24日 17:46:19 +00:00Commented Oct 24, 2022 at 17:46 -
\$\begingroup\$ @Adám thanks! This is just the culmination of a few hours practice, I was sure it could be golfed :D \$\endgroup\$Seggan– Seggan2022年10月24日 17:51:12 +00:00Commented Oct 24, 2022 at 17:51
-
\$\begingroup\$ @Adám what does that exactly do? seems like a bunch of compositions? \$\endgroup\$Seggan– Seggan2022年10月24日 17:52:42 +00:00Commented Oct 24, 2022 at 17:52
-
\$\begingroup\$ Let's continue this discussion in chat... \$\endgroup\$Adám– Adám2022年10月24日 17:54:03 +00:00Commented Oct 24, 2022 at 17:54
-
4\$\begingroup\$ @RAREKpopManifesto based on the Dyalog APL codepage the bytes would be
185 173 59 208 208 128 248 184 13 204 13(thi is for the shorter of the 2 answers). \$\endgroup\$Seggan– Seggan2023年05月30日 12:40:23 +00:00Commented May 30, 2023 at 12:40
-
1\$\begingroup\$ nice use of interpolated strings, wouldn't have thought of that \$\endgroup\$Zac Faragher– Zac Faragher2019年08月20日 04:12:29 +00:00Commented Aug 20, 2019 at 4:12
05AB1E, 4 bytes
€'!Ć
I/O as a list of characters.
Explanation:
€'! '# Prepend a "!"-item before each character in the (implicit) input-list
Ć # Enclose (append the first character of the list at the end of it)
# (after which the result is output implicitly)
Triangular, (削除) 15 (削除ここまで) 13 bytes
B3円;#*~.,</@<
-2 bytes after remembering that Triangular has a conditional halt operator.
I believe this is as short as it gets on this one. Triangular does have conditional direction-change operators, but they unfortunately work differently than the other conditionals. While all others check if ToS <= 0, the direction-changing conditionals check ToS != 0. If this weren't the case, we would have 10 bytes in the form of Bq3~#*/@<<.
Ungolfed:
B
\ 3
; # *
~ . , <
/ @ <
----------------------------------------------------
B3* - Push 11 and 3, then pop both and push their product.
<,< - Change directions 3 times (to save 2 bytes on last line)
@/ - Print Top of Stack value as a character, do not pop
~;\ - Push a character from input to ToS. Halt if ToS <= 0. Change Direction.
# - Print ToS as a character and pop
Previous Version (15 bytes):
B.3\.*#).(/?~@<
><>, (削除) 11 (削除ここまで) 6 bytes
"!"oio
Saved 5 bytes thanks to Jo King, suggesting exiting with an error. Previous version which does not exit with an error:
"!"oi:0(?;o
-
2\$\begingroup\$ You can remove the
:0(?;to terminate in an error \$\endgroup\$Jo King– Jo King2019年08月19日 10:30:28 +00:00Commented Aug 19, 2019 at 10:30
SimpleTemplate, 23 bytes
This is a language I wrote, and it was supposed to be for templates, but well.
!{@eachargv.0}{@echo_}!
Should be almost self-explanatory, once you see the ungolfed code:
!{@each argv.0 as char} {@echo char}!{@/}
And an explanation:
!- Prints the literal!character{@each argv.0 as char}- Loops through every character, with the value set to the variablechar(optional, the default variable is_).
argv.0is the first parameter passed to therender()method of the compiler.{@echo char}!- outputs thecharvariable and a literal!character.
For the golfed version, the default variable_is used instead.{@/}- closes the loop (optional)
Pure SimpleTemplate solution:
{@fn x}!{@eachargv.0}{@echo_}!{@/}{@/}
Creates a function x that outputs the same result.
You can use it like this:
{@call x "this is an example"}
You can try all of this on: http://sandbox.onlinephpfunctions.com/code/f6baff8d411fc8227ece81eccf05b6e7d3586bfa
On the line 908, you can use the variables $golfed, $ungolfed and $fn to test all the versions.
However, if it is allowed to use a character array, the code is simplified (20 bytes):
!{@echoj"!" argv.0}!
And ungolfed:
!{@echo separator "!" argv.0}!
Basically, outputs all items in the array, joined by "!", surrounded by literal !.
Due to limitations in the compiler class, the space is mandatory (in the golfed version).
This code is also extremelly harder to use in pure SimpleTemplate (using the function as example):
{@fn x}!{@echoj"!" argv.0}!{@/}
{@// alternative: @call str_split into a "a char array"}
{@set a "a", " ", "c", "h", "a", "r", " ", "a", "r", "r", "a", "y"}
{@call x a}
The @call can call a function that exists in PHP, which means that it isn't a pure SimpleTemplate solution.
Bash, 36 bytes
while read -n1 c;do printf \!$c;done
This counts on the newline terminating the input for the last ! mark.
-
\$\begingroup\$ Welcome! Please consider adding an explanation or link to an interpreter or something, because code-only answers are automatically flagged as low-quality. \$\endgroup\$mbomb007– mbomb0072019年08月19日 22:29:00 +00:00Commented Aug 19, 2019 at 22:29
-
\$\begingroup\$ @mbomb007, thanks for the pointer. \$\endgroup\$spuck– spuck2019年08月19日 22:42:15 +00:00Commented Aug 19, 2019 at 22:42
-
1\$\begingroup\$ Unfortunately this does not add an
!at the end of the input. \$\endgroup\$user41805– user418052019年08月20日 07:18:11 +00:00Commented Aug 20, 2019 at 7:18 -
\$\begingroup\$ @Cowsquack: on my terminal, the newline that terminates the input gets the ! added. On tio.run, the input needs to be terminated with a carriage return. I've updated the link to the Try it Online to reflect that. \$\endgroup\$spuck– spuck2019年08月20日 16:17:02 +00:00Commented Aug 20, 2019 at 16:17
a\nanda, can we require that there are no trailing newlines? \$\endgroup\$" ", is the output supposed to be "!" or "! !" \$\endgroup\$