24
\$\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\$
  • 6
    \$\begingroup\$ very similar question \$\endgroup\$ – Giuseppe Aug 18 at 11:06
  • 7
    \$\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\$ – Jonathan Allan Aug 18 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 Aug 18 at 14:29
  • 13
    \$\begingroup\$ Downvotes are inserted between every upvote, just like what the challege describes. \$\endgroup\$ – A̲̲ Aug 18 at 14:34
  • 2
    \$\begingroup\$ This is dead. \$\endgroup\$ – V. Courtois Aug 19 at 7:30

64 Answers 64

33
\$\begingroup\$

QuadR, 1 byte

Thanks to A__ for halving the byte count!

!

Try it online!

Replaces nothing with !

\$\endgroup\$
12
\$\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\$
  • \$\begingroup\$ This doesnt handle the empty line case correctly \$\endgroup\$ – flakes Aug 19 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 Aug 19 at 9:08
  • 3
    \$\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\$ – Reinstate Monica Aug 19 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 Aug 19 at 13:18
  • 1
    \$\begingroup\$ codegolf.stackexchange.com/questions/190223/insert-nbetween/… as you were saying to show a shorter way \$\endgroup\$ – U10-Forward - Reinstate Monica Aug 20 at 7:14
11
\$\begingroup\$

Python 3, 26 bytes

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

Try it online!

\$\endgroup\$
10
\$\begingroup\$

Retina 0.8.2, 2 bytes


!

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

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

Haskell, 18 bytes

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

-1 byte thanks to @nimi

Try it online!

\$\endgroup\$
  • 2
    \$\begingroup\$ ('!':). saves a byte. \$\endgroup\$ – nimi Aug 18 at 15:09
9
\$\begingroup\$

Python 2, 27 bytes

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

Try it online!

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

R, 25 bytes

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

Try it online!

A function accepting and returning a character vector.

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

brainfuck, 24 22 bytes

-2 bytes thanks to JoKing.

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

Try it online!

\$\endgroup\$
  • \$\begingroup\$ @JoKing thanks, I've updated my answer. \$\endgroup\$ – Krzysztof Szewczyk Aug 19 at 14:44
  • \$\begingroup\$ damn, that's badass \$\endgroup\$ – Cruncher Aug 19 at 20:20
6
\$\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\$
  • \$\begingroup\$ How do you write this code? Is there some code converter that you use? This seems crazy to try and write \$\endgroup\$ – Cruncher Aug 19 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 Aug 20 at 12:59
6
\$\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\$
5
\$\begingroup\$

sed, 12 bytes

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

Try it online!

-3 bytes thanks to Cows Quack

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

Zsh, 32 bytes

for c ('' ${(s::)1})echo -nE $c!

Try it online!

(s::) splits into characters, '' adds an empty element to the start, and then echo -nE $c! echoes each followed by an !.

\$\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
    \$\begingroup\$ 6 bytes \$\endgroup\$ – Nahuel Fouilleul Aug 19 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 Aug 19 at 11:20
4
\$\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\$
  • 1
    \$\begingroup\$ Welcome to the site! :) \$\endgroup\$ – Rahul Bharadwaj Aug 21 at 9:05
  • \$\begingroup\$ Thank you, @Rahul! \$\endgroup\$ – lee Aug 21 at 10:55
4
\$\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\$
  • \$\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 Aug 19 at 22:29
  • \$\begingroup\$ @mbomb007, thanks for the pointer. \$\endgroup\$ – spuck Aug 19 at 22:42
  • 1
    \$\begingroup\$ Unfortunately this does not add an ! at the end of the input. \$\endgroup\$ – Kritixi Lithos Aug 20 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 Aug 20 at 16:17
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\$
  • \$\begingroup\$ 62 bytes by using a multiplication loop instead of just a counter \$\endgroup\$ – Jo King Aug 21 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\$ – Reinstate Monica Aug 21 at 12:41
  • 1
    \$\begingroup\$ Aha! 60 bytes by multiplying 5*6 + 3 instead of 8*4+1 \$\endgroup\$ – Jo King Aug 21 at 12:43
  • \$\begingroup\$ Man, I know it isn't exactly your first rodeo, but this is really impressive. xD \$\endgroup\$ – Reinstate Monica Aug 21 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 Aug 21 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\$
3
\$\begingroup\$

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

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

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ nice use of interpolated strings, wouldn't have thought of that \$\endgroup\$ – Zac Faragher Aug 20 at 4:12
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\$

Befunge-98 (PyFunge), 7 bytes

'!,#@~,

Try it online!

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

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

Ruby, 17 16 bytes

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

Try it online!

Thanks Value Ink for -1 byte

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

Gema, 11 characters

\A=\!
?=?\!

Unfortunately ! starts a comment in Gema, so must be escaped.

Sample run:

bash-5.0$ echo -ne '1 2 3 4 5 6\n129591 129012 129127 129582\n\n0' | gema '\A=\!;?=?\!'
!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!

Try it online!

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

Japt, 4 bytes

rP'!

Try it

r      Replace
 P     all empty strings "" with
  '!   An exclamation mark (If '!' wasn't a special character in Japt I could remove the quote)
\$\endgroup\$
  • \$\begingroup\$ You'd only be able to ditch the ' if the replacement were a number. \$\endgroup\$ – Shaggy Aug 31 at 16:05
2
\$\begingroup\$

Japt, 4 bytes

rP'!

Try it

Japt -P, 7 bytes

Unfortunately ! is a reserved character, necessitating the quotation marks.

ï'! i'!

Try it

Not much to explain: ï is Cartesian product and i prepends.

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

Perl 5 + -p, 11 bytes

s/^|.\K/!/g

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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