# Injection from two strings to one string

## Challenge

Write a program that applies an injective function which takes an ordered pair of strings as input and one string as output. In other words, each input must map to a unique output.

## Specifics

• The input may be any two strings of arbitrary length, but will consist only of printable ASCII characters (codes $$\[32, 126]\$$).
• Similarly, the output string has no length restriction, but it must consist only of printable ASCII characters.
• If your language can't handle arbitrary-length strings, the program may merely work theoretically for strings of any size.
• The mapping from inputs to outputs should be consistent between executions of the program. Otherwise, the mapping you use is completely up to you, as long as it is an injection.
• The input is ordered. If the two input strings are different, they should produce a different output than if they were swapped. $$\s \neq t \implies f(s, t) \neq f(t, s)\$$
• Not every string needs to be a possible output.
• The shortest answer in each language wins!

## Test cases

The following inputs should all result in different outputs. To avoid confusion, strings are surrounded by guillemets («») and separated by single spaces.

«hello» «world»
«lelho» «drowl»
«diffe» «_rent»
«notth» «esame»

«Code» «Golf»
«Co» «deGolf»
«CodeGolf» «»

«» «»
«» « »
« » «»
« » «  »
«  » « »

«abc", » «def»
«abc» «, "def»
«abc' » «'def»
«abc'» « 'def»

«\» «"»
«\\» «\"»

• Hmm I just noticed "Write a program that..." - note that by meta consensus (currently 46 up, 1 down) "Restricting to just programs requires explicitly specifying "full program" rather than just "program"". I hope you did not mean to try to restrict this, but if you did you'll need to update the post accordingly and address those of us which have already submitted functions as answers. – Jonathan Allan Sep 27 '19 at 17:26
• @JonathanAllan The intent was to allow functions. – negative seven Sep 27 '19 at 22:25
• Can either of the strings be empty? – Shaggy Sep 27 '19 at 22:49
• @Shaggy Yes, either or both can. – negative seven Sep 28 '19 at 7:17
• Can we have some test cases? Thanks! – ouflak Sep 28 '19 at 13:18

## brainfuck, 302927 23 bytes

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


Try it online!

Inputs are separated by a 0x01 byte.

This turns ["foo", "bar"] into fUToUToUTUTbUTaUTrUT. To recover the original two strings, take groups of 3 characters, find the one where the second letter isn't U, and split there.

• @Grimy Sorry, misunderstood "Inputs are separated by a 0x01 byte." – wastl Sep 28 '19 at 11:02

# JavaScript (ES6), 14 bytes

Takes input as an array of 2 strings. Inspired by Luis' answer.

JSON.stringify


Try it online!

# JavaScript (ES6),  21  20 bytes

Takes input as (a)(b).

a=>b=>[a.length,a]+b


Try it online!

Returns the length of $$\a\$$, followed by a comma, followed by the concatenation of $$\a\$$ and $$\b\$$.

• Goddamnit! Was just about to use the stringify trick myself! – Shaggy Sep 27 '19 at 17:25
• Breaks with a="hello","" b="world" and a="hello" b="","world" – Soleil Sep 28 '19 at 18:17
• @Soleil That would give ["\"hello\",\"\"","\"world\""] and ["\"hello\"","\"\",\"world\""]. – Arnauld Sep 28 '19 at 18:51
• @Arnauld the backslash protection won't be visible. The function won't inject. – Soleil Sep 28 '19 at 18:54
• @Soleil I don't understand what you mean. JSON.stringify() definitely escapes ". See a hex dump. – Arnauld Sep 28 '19 at 19:06

## jq -c, 0 bytes

Try it online!

This definitely feels like cheating...? But it seems to comply with the rules of the challenge.

By default, jq will output its input in a human-readable JSON format. The -c (compact) flag tells jq to output in "compact" style, which removes the newlines (since the challenge forbids non-printable ASCII).

• OP allows empty input, so wouldn't a="", b="x" give same output as a="x" b="" – Gnudiff Sep 28 '19 at 13:37
• @Gnudiff How so? They would give ["","x"] and ["x",""] respectively – Doorknob Sep 28 '19 at 15:54
• Sorry, you're right. – Gnudiff Sep 28 '19 at 17:34

# Python 3, 3 bytes

str


A (built-in) function which, given a list of the two strings, gives a string representation of the list

Try it online!

• More pithy than Pyth... – trichoplax Sep 30 '19 at 20:23
• Well, the 1 byte Pyth solution  does the same thing. – Anders Kaseorg Oct 1 '19 at 6:47

# Japt-S, 3 bytes

I still feel I must be missing something here ...

®mc


Try it

Possibly 2 bytes:

mq


Try it

Or stretching it with this 1-byter:

U


Try it

The first version maps each string in the array to its codepoints and outputs them joined with a space.

The second version splits each string to a character array and outputs them joined with a space.

And the third version, which feels like cheating, just outputs the input with the -Q flag doing the heavy lifting of srringifying it.

# Pyth, 4 bytes

jNmC


Try it online!

This converts each string to base 256 and then joins them in order with a ". Since the results are each numbers the " unambiguously separates them and the original strings can be recovered with mCsdczN.

• @AndersKaseorg suggests a 1-byter. – Kevin Cruijssen Oct 1 '19 at 7:11
• @KevinCruijssen I've been aware of that since a few minutes after I posted this, I just prefer the spirit of this solution. Thanks for bringing it up anyway, and definitely feel free to post it yourself :) – FryAmTheEggman Oct 1 '19 at 14:41
• I indeed like your answer more as well. :) And nah, I don't even know Pyth. If you want you can post it yourself as a separated answer (or a combined answer by editing this one), otherwise Anders Kaseorg can post it, since he's the one who mentioned it in the comment. – Kevin Cruijssen Oct 1 '19 at 15:13

# T-SQL, 38 bytes

SELECT QUOTENAME(a)+QUOTENAME(b)FROM i


Input is taken from a pre-existing table $$\i\$$ with varchar fields $$\a\$$ and $$\b\$$, per our IO rules.

Uses QUOTENAME, which surrounds the strings with [] and also escapes any internal brackets. Should map to a unique output.

• Same length on MySQL: SELECT CONCAT(QUOTE(a),QUOTE(b))FROM t Try it online – Night2 Sep 27 '19 at 18:00

# Zsh, 7 bytes

<<<$@:q  Try it online! Implicitly joins the arguments on spaces. The q modifier tells zsh to quote the arguments, which crucially escapes spaces, ensuring an unescaped space unambiguously separates the two arguments. (Without q, "a " "b" and "a" " b" would both yield "a b".) # MATL, 1 byte j  The code takes an array of two strings as input, and outputs a string representation of that array. Try it online! ### Explanation The code simply reads the input as a string, unevaluated. • Does this really take two strings as input? It seems like this just prints the input whatever it is.Try it online! – James Sep 27 '19 at 17:28 • @DJ It just reads unevaluated input, so it reads anything. Is it a loophole? A more standard approach would be to take the evaluated input as an array and then convert to string representation. But the result would be the same as my code, so I would argue that the distinction is unobservable – Luis Mendo Sep 27 '19 at 17:45 # Jelly, 2 bytes ŒṘ  A monadic Link accepting a list of two lists of characters as its argument which yields a single list of characters. Try it online! ### How? It's a built-in to get Python's string representation, simples. ## Haskell, 4 bytes show  The Haskell built-in to turn things into strings. The input is taken as a pair of strings. Try it online! # 05AB1E, 2 bytes ₁ö  Try it online! Interprets each string as a base-256 integer, then prints the two in the form [1, 2]. ## 05AB1E, 1 byte (unknown validity) â  Try it online! Takes the cartesian product of the input with itself. Quotes in the input are not escaped, which could cause confusion. I brute-forced all combinations of up to 12 ", " and "], [" and didn’t find any collision; however, I can’t prove there aren’t any collisions for longer strings. If anybody can come up with a proof or counter-example, I’d highly appreciate it! The trivial 0-byter fails because of quotes not being escaped: inputs (", ", empty string) and (empty string, ", ") both yield the output ["", "", ""]. The 1-byter º (mirror each input string) also fails because of this: inputs (", "" ,", empty string) and (empty string, " ,"", ") both yield the output ["", "" ,"", "" ,"", ""]. # C# with 26 bytes (thanks to Lukas Lang, Kevin Cruijssen and Jo King) a=>b=>$"{a.Length}.{a}{b}"


tio.run lambda

• Please consider using tio.run – Ver Nick Sep 28 '19 at 14:49
• This still breaks on ("a<>","b") and ("a","<>b") - note that it is impossible to handle all cases if you're just concatenating the inputs, no matter what you put between/around them - you need to modify the strings somehow – Lukas Lang Sep 28 '19 at 17:54
• Sorry, still not: ("1","11111111111") and ("11111111111","1") - what does work on the other hand (and is even shorter) is this $"{a.Length}.{a}{b}" - you can always extract a and b from this without any ambiguity (just read until the first . to get the length of a, b is then the rest – Lukas Lang Sep 28 '19 at 19:14 • @LukasLang Great. Thanks for the follow ups. – Soleil Sep 28 '19 at 19:27 • Many thanks @JoKing ! – Soleil Oct 2 '19 at 0:11 # Charcoal, 12 bytes ⪫Ｅ²⭆⪪Ｓ"⪫""λ,  Try it online! Link is to verbose version of code. Explanation:  Ｅ² Repeat twice Ｓ Input a string ⪪ " Split it on "s ⭆ Map over each piece and join ⪫""λ Wrap each piece in "s ⪫ , Join the two results with a , Implicitly print  # PowerShell, 22 bytes Many fixes thanks to mazzy "$($args|% le*)"+$args


Try it online!

Take five, oh my.

• "ab" "c" and "a" "bc" both produce the same output. – BradC Sep 27 '19 at 20:50
• @JoKing Things should be fixed. Hopefully. – Veskah Sep 28 '19 at 4:32
• 15 bytes with $OFS? – Andrei Odegov Sep 28 '19 at 15:07 # R, 4 bytes dput  Try it online! A built-in function which returns the string representation of the input (inspired by Jonathan Allan's answer ) # Wolfram Language (Mathematica), 8 bytes Compress  Try it online! • {"Hello, ","World"} and {"Hello",", World"} don't they give the same string? – Nahuel Fouilleul Sep 27 '19 at 20:00 • @NahuelFouilleul ah, nice catch. Compress it is then – att Sep 27 '19 at 21:27 # CSS + HTML, 55 + 20 = 75 bytes Provide the inputs in the HTML after <code> tags. Visually injects letters one by one from each input into the output. When an input is longer than the other one, visual spaces are shown for missing letter(s) of the shorter input. Also one comma is added in HTML to force visual output uniqueness (I hope). *{position:absolute;letter-spacing:9px}code>code{left:9px <code>abcdefg<code>hijklmn</code>, # C# (Visual C# Interactive Compiler), 5 bytes Print  Try it online! • Why the downvote? My solution is perfectly valid – Gymhgy Sep 28 '19 at 16:11 # Perl 6, 6 3 bytes &dd  Try it online! Outputs the object representation to STDERR. • I'm not familiar enough with Perl - how would you input a string containing spaces here? – negative seven Sep 28 '19 at 8:32 • @negativeseven like this – Grimmy Sep 28 '19 at 9:17 • @negativeseven Yeah sorry, I was just using a shortcut syntax for lists of strings. You can do that in a typical fashion like ["Hello, ", "World!"] – Jo King Sep 29 '19 at 2:01 # Lua, 27 bytes print(('%q%q'):format(...))  Try it online! Full program, take input as arguments. Inspired by zsh answer, as it also use %q modifier to use internal safe-string engine. Also, I can think of just ('%q%q'):format  but I'm not sure if this is acceptable answer. • I don't think the second is valid since ('%q%q'):format doesn't result in an object that can, for example, be assigned to a variable. It just causes a syntax error on its own. The colon syntax is just a shortcut for '%q%q'.format('%q%q', ...) and Lua doesn't allow partial application for functions – Jo King Sep 30 '19 at 0:15 • @JoKing Yeah, you are right, I think. – val is still with Monica Sep 30 '19 at 4:06 # sed, 19 bytes N s/ /. /g s/\n/: /  Try it online! N # append the second string into the pattern space s/ /. /g # prefix all spaces with ".". Now ": " will not occur in the stiring s/\n/: / # replace the newline with ": "  # JavaScript (SpiderMonkey), 6 bytes uneval  Try it online! Input array of strings, output a single string. Inspired by Arnauld's JSON.stringify answer. # C (gcc), 59 bytes Thanks to Grimy for the suggestion. Takes an array of input strings (of which "2" is the correct number for this challenge) and prints their character values, including the trailing NUL. Technically, the %p formatter used by printf is for pointers, but it works fine for displaying hex values of arbitrary integers if you're not picky about how they look! f(s,t)char**s,*t;{for(;t=*s++;)for(;printf("%p",*t++)^5;);}  Try it online! • Nice! Here’s a 59. – Grimmy Oct 1 '19 at 9:25 # Brachylog, 0 bytes Input as an array of 2 strings, output in the format: Z = ["{{string 1 stringified}}","{{string 2 stringified}}"] Try it online! # PHP, 9 bytes serialize  Try it online! Input is an array of 2 strings like this: ['abc', 'def']. Uses PHP's serialize to serialize the input into a string. # Bash, 11 bytes echo${@@Q}


Try it online!

\${parameter@Q} quotes the parameter for reuse as input. In the case of an array, it quotes each element.

# Ruby, 12 bytes

->*a{a.to_s}
`

Try it online!