37
\$\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\$
11
  • 6
    \$\begingroup\$ very similar question \$\endgroup\$
    – Giuseppe
    Aug 18, 2019 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\$ 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
    Aug 18, 2019 at 14:29
  • 23
    \$\begingroup\$ Downvotes are inserted between every upvote, just like what the challege describes. \$\endgroup\$
    – user85052
    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
    Aug 19, 2019 at 3:52

80 Answers 80

1 2
3
1
\$\begingroup\$

SNOBOL4 (CSNOBOL4), 94 bytes

I	I =INPUT	:F(END)
S	I LEN(1) . X REM . I	:F(O)
	O ='!' O X	:(S)
O	OUTPUT =O '!'
	O =	:(I)
END

Try it online!

Prints with an additional trailing newline (as SNOBOL always prints a line break). The way input is consumed, there is no way of distinguishing between a final line ending in \n or not.

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

Stax, 6 5 bytes

_z'!R

Run and debug it

Replace "" with "!" using regex replacement. I don't exactly understand why this works.

Edit: Found it in the Ecma spec: 15.5.4.10

If there is a match with an emptystring (in other words, if the value of regexp.lastIndex is left unchanged), increment regexp.lastIndex by 1.

Old Stuff:

I would have been able to get to 5 bytes in either of two scenarios.

  1. If input was provided in an escaped literal e.g. "line1\nline2". But I opted against that for the sake of "usability".
  2. If there was not a bug feature in stax's zip implementation when encountering empty strings/arrays.
\$\endgroup\$
1
\$\begingroup\$

C (gcc), 48 45 bytes

f(char*s){putchar(33);*s&&f(s+write(1,s,1));}

Try it online!

-3 thanks to ceilingcat

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

Wolfram Language (Mathematica), 49 48 42 bytes

#~StringSplit~""~StringRiffle~{a="!",a,a}&

Try it online!

-6 bytes thanks to @attinat

\$\endgroup\$
2
  • \$\begingroup\$ The code in the Header, Code, and Footer sections is run. You can use infix notation and drop the { } in StringSplit for 42 bytes \$\endgroup\$
    – att
    Aug 19, 2019 at 23:24
  • \$\begingroup\$ @attinat Thanks that helps a lot! \$\endgroup\$
    – Kai
    Aug 20, 2019 at 19:50
1
\$\begingroup\$

Brachylog, 10 bytes

"!"w.∋w?w⊥

Try it online!

Takes input through the output variable, and unifies the input variable with "!", printing the output.

"!"w          Print "!", which is the input variable.
    .∋w       Print an element of the output variable.
       ?w     Print the input variable again.
         ⊥    Fail.
\$\endgroup\$
1
\$\begingroup\$

Elvish, 32 bytes

use re;re:replace "" "!" (slurp)

Copy and paste into the Live Environment to try it out!

\$\endgroup\$
2
  • 2
    \$\begingroup\$ Welcome to the site! Would it be possible to edit in a link to an online interpreter, such as Try it online! so that other users can verify your program? \$\endgroup\$ Aug 22, 2019 at 12:04
  • \$\begingroup\$ @cairdcoinheringaahing I added a link to the provided live environment. \$\endgroup\$
    – kon
    Aug 22, 2019 at 13:36
1
\$\begingroup\$

Ahead, 12 bytes

~irj!'~
@Wu<

Try it online!

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

Lua, 31 bytes

print((io.read():gsub("","!")))
\$\endgroup\$
1
\$\begingroup\$

C (gcc), 32 bytes

f(s){printf("!%s",s)-1&&f(s+4);}

Try it online!

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

Red, 47 bytes

func[s][parse s[insert"!"any[skip insert"!"]]s]

Try it online!

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

C#, 142 bytes

public class P{public static void Main(string[]a){System.Console.Write("!");foreach(char c in a[0]){System.Console.Write(c.ToString()+"!");}}}
\$\endgroup\$
0
1
\$\begingroup\$

Branch, 11 bytes

33.,[.33.,]

Try it on the online Branch interpreter!

Put 33 on the tree and output (!). Then, grab input. Finally, while that value is not EOF, output it, output 33 (!), and grab input again.

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

Vim, 13 bytes

:%s/\_./!&/g

Works by putting a ! before every character, including newline characters. The TIO version doesn't work properly for the 4 newline testcase, but it does work in Vim.

Try it online!

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

BRASCA, 14 13 bytes

-1 byte thanks to RezNesX

,![$'!oo{]'!o

Try it online!

Explanation

,               - Reverse stack
 ![$    {]      - Loop stack length:
    '!oo        -     Print "!" followed by the next letter
          '!o   - Print the last "!"
\$\endgroup\$
1
  • 1
    \$\begingroup\$ 13 bytes: ,![$'!oo{]'!o \$\endgroup\$
    – RezNesX
    May 12, 2021 at 11:20
0
\$\begingroup\$

Husk, 10 8 bytes

-2 bytes thanks to Razetime!

`:'!ṁe'!

Try it online!

\$\endgroup\$
0
0
\$\begingroup\$

Pip, 5 bytes

aJW'!

Try it online!

The JW builtin was made for challenges like this.

\$\endgroup\$
0
\$\begingroup\$

x86-16 machine code, 8 bytes

00000000: b021 aaa4 aae2 fcc3                      .!......

Listing:

B0 21       MOV  AL, '!'        ; character to insert
AA          STOSB               ; write first ! to output string  
        CLOOP: 
A4          MOVSB               ; copy input char to output string 
AA          STOSB               ; write ! to output string 
E2 FC       LOOP CLOOP          ; loop until end of input 
C3          RET                 ; return to caller

Callable function, input at SI, length in CX. Output to string at DI.

Test runs:

enter image description here

enter image description here

enter image description here

\$\endgroup\$
0
\$\begingroup\$

Zsh, 12 bytes

<<<${1///!}!

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Julia, 21 20 19 bytes

x->'!'join(x,!)*'!'

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Subleq (8-bit), 1211 bytes

-1 byte for removing last 0

2 -1 33
-1 8 -1
8 -1 0
8 8 

Subleq emulator

Explanation

0:  2 -1 33 ' Output 2: (char(33)); 3rd argument is not used for output so use it for memory
3: -1  8 -1 ' Read input to 8:, exit if null
6:  8 -1  0 ' Output 8:
9:  8  8    ' 8: = 0 goto 0
\$\endgroup\$
1 2
3

Your Answer

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