46
\$\begingroup\$

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 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:

!!!!!
!!!!!
!!!!!
!!!!!
!!!!!
\$\endgroup\$
12
  • 7
    \$\begingroup\$ very similar question \$\endgroup\$
    – Giuseppe
    Commented Aug 18, 2019 at 11:06
  • 9
    \$\begingroup\$ I really don't understand the downvote - this is a clear and well written challenge. Re: being a duplicate - it's not (preceding '!' makes for a big difference), and I don't believe anyone has suggested so (no close votes). \$\endgroup\$ Commented Aug 18, 2019 at 13:45
  • 1
    \$\begingroup\$ if a language can't tell the difference between a\n and a, can we require that there are no trailing newlines? \$\endgroup\$
    – Giuseppe
    Commented Aug 18, 2019 at 14:29
  • 31
    \$\begingroup\$ Downvotes are inserted between every upvote, just like what the challege describes. \$\endgroup\$
    – user85052
    Commented Aug 18, 2019 at 14:34
  • 1
    \$\begingroup\$ Is the case of a single space input " ", is the output supposed to be "!" or "! !" \$\endgroup\$
    – Kai
    Commented Aug 19, 2019 at 3:52

97 Answers 97

40
\$\begingroup\$

QuadR, 1 byte

Thanks to A__ for halving the byte count!

!

Try it online!

Replaces nothing with !

\$\endgroup\$
16
\$\begingroup\$

Python 3, 26 bytes

lambda s:s.replace('','!')

Try it online!

\$\endgroup\$
1
  • 3
    \$\begingroup\$ Welcome to the site! \$\endgroup\$ Commented Aug 19, 2019 at 12:37
13
\$\begingroup\$

Retina 0.8.2, 2 bytes


!

Try it online! At last, a challenge where Retina has a built-in!

\$\endgroup\$
4
  • 2
    \$\begingroup\$ Actually, I created this challenge based on this Retina built-in. \$\endgroup\$
    – user85052
    Commented Aug 18, 2019 at 11:04
  • \$\begingroup\$ Works in QuadR too. \$\endgroup\$
    – Adám
    Commented Aug 18, 2019 at 15:01
  • \$\begingroup\$ -1 bytes in QuadR. \$\endgroup\$
    – user85052
    Commented 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
    Commented Aug 18, 2019 at 19:02
13
\$\begingroup\$

Python 3, 27 bytes

lambda s:f"!{'!'.join(s)}!"

Try it online!

Honestly, I hope someone can show me a cool way to do this with a smaller byte count.

\$\endgroup\$
5
  • \$\begingroup\$ This doesnt handle the empty line case correctly \$\endgroup\$
    – flakes
    Commented 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\$ Commented 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
    Commented 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
    Commented 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\$ Commented Aug 20, 2019 at 7:14
12
\$\begingroup\$

Python 2, 27 bytes

lambda s:'!%s!'%'!'.join(s)

Try it online!

\$\endgroup\$
12
\$\begingroup\$

Haskell, 18 bytes

('!':).(>>=(:"!"))

-1 byte thanks to @nimi

Try it online!

\$\endgroup\$
1
  • 2
    \$\begingroup\$ ('!':). saves a byte. \$\endgroup\$
    – nimi
    Commented Aug 18, 2019 at 15:09
11
\$\begingroup\$

brainfuck, 24 22 bytes

-2 bytes thanks to JoKing.

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

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ damn, that's badass \$\endgroup\$
    – Cruncher
    Commented Aug 19, 2019 at 20:20
9
\$\begingroup\$

Labyrinth,  19 11 10  9 bytes

33
..
",@

Try it online!

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.

\$\endgroup\$
9
\$\begingroup\$

JavaScript (ES6), 19 bytes

Takes input as an array of characters.

s=>`!${s.join`!`}!`

Try it online!


JavaScript (ES6),  23  20 bytes

Saved 3 bytes thanks to @ShieruAsakoto

Takes input as a string.

s=>[,...s,,].join`!`

Try it online!


JavaScript (ES6), 22 bytes

Suggested by @tjjfvi

Takes input as a string.

s=>s.replace(/|/g,"!")

Try it online!

\$\endgroup\$
3
  • 3
    \$\begingroup\$ Alternative with .replace, 22 bytes \$\endgroup\$
    – tjjfvi
    Commented Aug 18, 2019 at 19:12
  • \$\begingroup\$ @tjjfvi Nifty one! \$\endgroup\$
    – Arnauld
    Commented Aug 18, 2019 at 19:18
  • 5
    \$\begingroup\$ I've got a 20 for your 23: s=>[,...s,,].join`!` \$\endgroup\$ Commented Aug 19, 2019 at 13:42
8
\$\begingroup\$

R, 25 bytes

function(x)gsub("","!",x)

Try it online!

A function accepting and returning a character vector.

\$\endgroup\$
3
  • \$\begingroup\$ Can shave 3 bytes by switching the function form to scan(,''), like so tio.run/##K/r/P724NElDSUlHSVFJpzg5MU9DR11dU/O/… \$\endgroup\$
    – Sumner18
    Commented Aug 19, 2019 at 13:37
  • \$\begingroup\$ @Sumner18 thanks. I started with that but it splits input at spaces. \$\endgroup\$ Commented 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 with cat.) \$\endgroup\$ Commented Aug 19, 2019 at 21:14
7
\$\begingroup\$

Pepe, 47 bytes

REREEeRErEErREeeEeeeeEREEeeREEeereeREEEEeeEReee

Try it online!

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)
\$\endgroup\$
2
  • \$\begingroup\$ How do you write this code? Is there some code converter that you use? This seems crazy to try and write \$\endgroup\$
    – Cruncher
    Commented 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
    Commented Aug 20, 2019 at 12:59
7
\$\begingroup\$

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 21H in INT 21H happens 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 AAA instead of a standard TEST to compare AL to 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
\$\endgroup\$
5
\$\begingroup\$

Jelly, 5 bytes

Ż”!ṁż

A full program accepting a string, which prints the result.

Try it online!

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!
\$\endgroup\$
5
\$\begingroup\$

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
$000a  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.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Welcome to the site! :) \$\endgroup\$ Commented Aug 21, 2019 at 9:05
  • \$\begingroup\$ Thank you, @Rahul! \$\endgroup\$
    – lee
    Commented Aug 21, 2019 at 10:55
5
\$\begingroup\$

sed, 12 bytes

s/\b\|\B/!/g

Try it online!

-3 bytes thanks to Cows Quack

\$\endgroup\$
4
  • \$\begingroup\$ Actually the Sed code is just 15 characters there: Try it online!. \$\endgroup\$
    – manatwork
    Commented Aug 19, 2019 at 15:11
  • \$\begingroup\$ Great, thank you. I was unclear how that worked... \$\endgroup\$
    – Jonah
    Commented Aug 19, 2019 at 15:13
  • 1
    \$\begingroup\$ s/\b\|\B/!/g also works for 12 bytes \$\endgroup\$
    – user41805
    Commented Aug 20, 2019 at 7:15
  • \$\begingroup\$ @Cowsquack thank you. updated. \$\endgroup\$
    – Jonah
    Commented Aug 20, 2019 at 12:47
5
\$\begingroup\$

AWK, 11 bytes

gsub(a,"!")

Try it online!

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).

\$\endgroup\$
4
\$\begingroup\$

Befunge-98 (PyFunge), 7 bytes

'!,#@~,

Try it online!

\$\endgroup\$
4
\$\begingroup\$

Perl 5 -p0, 17 6 bytes

s,,!,g

Try it online!

My original answer was -p and $_='!'.s,.,$&!,gr. Thanks to @Nahuel Fouilleul for cutting 11 bytes and to @Grimy for the -p0 tip.

\$\endgroup\$
2
  • 2
    \$\begingroup\$ 6 bytes \$\endgroup\$ Commented Aug 19, 2019 at 8:32
  • 1
    \$\begingroup\$ @NahuelFouilleul -lp gives incorrect output for the \n\n\n\n test case (returns 4 newline-separated ! instead of the specified 5). -p0 works correctly. \$\endgroup\$
    – Grimmy
    Commented Aug 19, 2019 at 11:20
4
\$\begingroup\$

MarioLANG, 95 94 90 89 69 bytes

++++++
======< >)
>+++++++",+[
=======<.==<
>+++++++!(.-
========#===

Try it online!

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):

+++++++++++>,
==========@"+
+++++++++++)[
@==========.==<
+++++++++++!(.-
===========#===
\$\endgroup\$
5
  • \$\begingroup\$ 62 bytes by using a multiplication loop instead of just a counter \$\endgroup\$
    – Jo King
    Commented Aug 21, 2019 at 12:35
  • \$\begingroup\$ Okay now THAT is cool. I'll update as soon as I have time to redo the explanation, thanks a bunch! \$\endgroup\$
    – squid
    Commented Aug 21, 2019 at 12:41
  • 1
    \$\begingroup\$ Aha! 60 bytes by multiplying 5*6 + 3 instead of 8*4+1 \$\endgroup\$
    – Jo King
    Commented Aug 21, 2019 at 12:43
  • \$\begingroup\$ Man, I know it isn't exactly your first rodeo, but this is really impressive. xD \$\endgroup\$
    – squid
    Commented 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
    Commented Aug 21, 2019 at 12:46
4
\$\begingroup\$

Perl 6, 16 11 bytes

{S:g/<(/!/}

Try it online!

Replaces all zero width matches with exclamation marks. Null regexes are not allowed, so we use a capture marker to capture nothing instead

\$\endgroup\$
4
\$\begingroup\$

Zsh, 32 23 bytes

<<<!${(j:!:)${(s::)1}}!

Try it online! Try it online!

(s::) splits into characters, (j:!:) joins on !s.

\$\endgroup\$
4
+200
\$\begingroup\$

APL (Dyalog Unicode), 11 bytes

(∊⊢⍪⍪¨)∘'!'

Try it online!

@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

{'!'⍪∊⍉↑⍵('!'⍴⍨≢⍵)}

Try it online!

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
\$\endgroup\$
7
  • \$\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
    Commented 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
    Commented Oct 24, 2022 at 17:51
  • \$\begingroup\$ @Adám what does that exactly do? seems like a bunch of compositions? \$\endgroup\$
    – Seggan
    Commented Oct 24, 2022 at 17:52
  • \$\begingroup\$ Let's continue this discussion in chat… \$\endgroup\$
    – Adám
    Commented 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
    Commented May 30, 2023 at 12:40
3
\$\begingroup\$

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

s=>$"!{String.Join("!",s)}!"

Try it online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ nice use of interpolated strings, wouldn't have thought of that \$\endgroup\$ Commented Aug 20, 2019 at 4:12
3
\$\begingroup\$

Java 8, 20 bytes

A lambda function from String to String.

s->s.replace("","!")

Try It Online

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

05AB1E, 4 bytes

€'!Ć

I/O as a list of characters.

Try it online.

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)
\$\endgroup\$
3
\$\begingroup\$

Triangular, 15 13 bytes

B\3;#*~.,</@<

Try it online!

-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\.*#).(/?~@<
\$\endgroup\$
3
\$\begingroup\$

><>, 11 6 bytes

"!"oio

Try it online!

Saved 5 bytes thanks to Jo King, suggesting exiting with an error. Previous version which does not exit with an error:

"!"oi:0(?;o

Try it online!

\$\endgroup\$
1
  • 2
    \$\begingroup\$ You can remove the :0(?; to terminate in an error \$\endgroup\$
    – Jo King
    Commented Aug 19, 2019 at 10:30
3
\$\begingroup\$

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 variable char (optional, the default variable is _).
    argv.0 is the first parameter passed to the render() method of the compiler.
  • {@echo char}! - outputs the char variable 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.

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

Bash, 36 bytes

while read -n1 c;do printf \!$c;done

Try it online!

This counts on the newline terminating the input for the last ! mark.

\$\endgroup\$
4
  • \$\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
    Commented Aug 19, 2019 at 22:29
  • \$\begingroup\$ @mbomb007, thanks for the pointer. \$\endgroup\$
    – spuck
    Commented Aug 19, 2019 at 22:42
  • 1
    \$\begingroup\$ Unfortunately this does not add an ! at the end of the input. \$\endgroup\$
    – user41805
    Commented 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
    Commented Aug 20, 2019 at 16:17
3
\$\begingroup\$

Ruby, 17 16 bytes

->s{s.gsub'',?!}

Try it online!

Thanks Value Ink for -1 byte

\$\endgroup\$
1
  • \$\begingroup\$ Remove the space after gsub. \$\endgroup\$
    – Value Ink
    Commented Aug 20, 2019 at 19:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.