26
\$\begingroup\$

With challenges like Output with the same length as the code and Create output twice the length of the code, I thought of a separate, but similar challenge.

The task is to produce an output. It can either be a string, a list of characters, or whatever is the default output format of your program. However, your output must always be the same length, regardless of the input. And more importantly, the output should be different for different inputs.

Input

A single integer \$n\$, the ranges of which is determined by the choice of language. If your language has variable length integers, the range is \$-2^{31} \leq n < 2^{31}\$.

Output

A string or a list of characters, or a print to STDOUT or STDERR. You may only use one of these methods. The output should be of the same length regardless of the input, but it is up to you to define which length that is. The output may not contain the digit characters 0-9, or the minus sign -. The output should be deterministic.

You should be able to prove that for every output there is only one possible input, either by a formal proof, an argument, or a brute-force search.

This is a code golf question, so shave off any extraneous bytes. All languages are welcome, the more the better!

\$\endgroup\$
  • 4
    \$\begingroup\$ Should the program in theory work for any input? Or does it suffice to use a method that only works for the specified range (bounded by \$\pm 2^{31}\$), not necessarily due to language limitations but method limitations? \$\endgroup\$ – Mr. Xcoder Aug 16 at 13:28
  • 4
    \$\begingroup\$ @Mr.Xcoder how could you pick a length if you had to work for arbitrary length integers? \$\endgroup\$ – Stephen Aug 16 at 13:34
  • 2
    \$\begingroup\$ @Mr.Xcoder You must only prove that it works for that range. This rule is to help languages such as Python, where numbers can be thousands of digits. Without that rule, it'd be much harder for both Python, and Python derivatives (which includes a lot of golfing languages). \$\endgroup\$ – maxb Aug 16 at 13:37
  • 11
    \$\begingroup\$ If a language doesn't have variable length integers but can support values larger than \$2^{31}\$, are we allowed to use the restricted range \$-2^{31}\le n<2^{31}\$? \$\endgroup\$ – Arnauld Aug 16 at 15:20
  • 2
    \$\begingroup\$ @Arnauld the maximum range is 32 bit signed integers, regardless of the limitations of the language. The language can only decrease that range. \$\endgroup\$ – maxb Aug 16 at 22:30

30 Answers 30

15
\$\begingroup\$

JavaScript (ES8), 33 bytes

Expects the input in the range of safe JS integers: \$-2^{53}\le n < 2^{53}\$.

Returns a string of 76 characters.

n=>btoa(n.toString(2)).padEnd(76)

Try it online!

How?

Step 1

The input is first converted to binary. This preserves the leading minus sign for negative numbers.

Examples:

  • 123"1111011"
  • -77"-1001101"

Step 2

The resulting string is encoded in base-64.

It means that each block of 1 to 3 characters will be turned into a new block of 4 characters. This conversion is safe because none of the resulting blocks contain the forbidden symbols (digits or minus sign).

3-character blocks

"-10" -> "LTEw" | "011" -> "MDEx"
"-11" -> "LTEx" | "100" -> "MTAw"
"000" -> "MDAw" | "101" -> "MTAx"
"001" -> "MDAx" | "110" -> "MTEw"
"010" -> "MDEw" | "111" -> "MTEx"

A single final block of 1 or 2 characters has to be encoded if the length of the binary string is not a multiple of 3:

1-character blocks

"0"   -> "MA==" | "1"   -> "MQ=="

2-character blocks

"-1"  -> "LTE=" | "10"  -> "MTA="
"00"  -> "MDA=" | "11"  -> "MTE="
"01"  -> "MDE=" | 

Step 3

The final output is padded with trailing spaces.

\$\endgroup\$
  • 1
    \$\begingroup\$ Did you already know that these particular combinations of character contain no numbers when converted to base64, or did you find out experimentally? \$\endgroup\$ – Tomáš Zato Aug 19 at 10:33
  • \$\begingroup\$ @TomášZato That was a confident guess, based on the fact that it is a very small charset in the lower part of the ASCII table. \$\endgroup\$ – Arnauld Aug 19 at 12:35
10
\$\begingroup\$

Python 3, 49 39 bytes

lambda i:[chr(ord(x)*2)for x in"%9x"%i]

Try it online!

-10 bytes thanks to negative seven

Converts the integer to hexadecimal, and prepends spaces up to 9 characters total. Then, doubles the ASCII code of each character in the string (some extend outside of ASCII into Unicode, but Python handles it fine), outputting a list of characters.

This works because every digit, including -, is mapped to a different ASCII character. No integers between -2147483648 and 2147483648 are equal, so converting them to hexadecimal and prepending spaces would not make them equal. Then, mapping them to different codepoints does not lead to collisions, so there are still no two values in the range which lead to equal outputs.

Python 3, 59 56 47 bytes

lambda i:[*map(lambda x:chr(ord(x)*2),"%9x"%i)]

Try it online!

-3 bytes thanks to Jitse

-9 bytes thanks to negative seven

Same algorithm, but using map instead of a for loop.

\$\endgroup\$
  • 2
    \$\begingroup\$ You can shave off 3 bytes in the map approach by replacing list( ... ) with [* ... ] \$\endgroup\$ – Jitse Aug 16 at 13:47
  • 3
    \$\begingroup\$ You could create the padded string with "%9x"%i \$\endgroup\$ – negative seven Aug 16 at 14:03
  • \$\begingroup\$ @negativeseven thank you, that is really genius \$\endgroup\$ – Stephen Aug 16 at 14:22
  • 1
    \$\begingroup\$ Could stay within ASCII and at 39 bytes in Python 2 using `4e9+n` \$\endgroup\$ – Jonathan Allan Aug 17 at 0:09
8
\$\begingroup\$

05AB1E, 11 5 bytes

тjÇ·ç

-6 bytes porting @Stephen's approach, so make sure to upvote him!

Outputs a list of up to 100 characters, with \$100-(\text{input length})\$ amount of @ (double the space codepoint), and all -0123456789 mapped to Z`bdfhjlnpr (double the ASCII codepoints).

Try it online.

Explanation:

 j     # Prepend spaces in front of the (implicit) input-integer to make it of length:
т      # 100
  Ç    # Convert each character to its unicode value
   ·   # Double each
    ç  # And convert it back to a character
       # (after which the resulting list is output implicitly)

Original 11 bytes answer:

Ķ×AIdè«žIj

Try it online (limited to 1000 instead of 2147483648).

Explanation:

The output length is always 2,147,483,648 characters long. It will output \$2147483648-|n|-1\$ amount of spaces, appended with \$|n|\$ amount of newlines, appended with either an 'a' if \$n\lt0\$ or 'b' if \$n\geq0\$.

Ä            # Get the absolute value of the (implicit) input-integer
 ¶×          # And have a string with that many newline characters
   A         # Push the lowercase alphabet
    Id       # Check if the input is non-negative (>=0) (1 if truthy; 0 if falsey)
      è      # Use that to index into the alphabet (so "a" for <0 and "b" for >=0)
       «     # Append that to the newline-string we created earlier
          j  # And prepend spaces to make the string of a length:
        žI   # 2147483648 (this has been replaced with `₄`/1000 in the TIO)
             # (after which the result is output implicitly)
\$\endgroup\$
7
\$\begingroup\$

brainfuck, 48 29 28 16 13 bytes

This program requires cells, where \$ c_n \in \mathbb{N} \$, but if you want consistent results, make sure that \$ c_n < 256 \$

Output will obviously be unique no matter what number will you enter (\$ -\infty \lt n \lt \infty \$). If the integer is shorter, the program will pad out the output to match exactly \$ \infty \$ bytes, so the length is always the same.

This answer is a bit loopholing the challenge, as it didn't state that output has to be finite.

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

Try it online!


Original, 28 byte answer:

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

This one will pad the output to be exactly \$ 2^8 - 1 \$ bytes. The number conversion mechanism works the same here. This program assumes the same as the program above.

\$\endgroup\$
  • \$\begingroup\$ Infinite output is not allowed \$\endgroup\$ – Jo King Aug 18 at 5:09
  • \$\begingroup\$ @JoKing this was stated after I made this answer. But if you really feel hurt by this, I can simply opt out of challenge and declare first answer as non-competing \$\endgroup\$ – Krzysztof Szewczyk Aug 18 at 8:12
5
\$\begingroup\$

Python 3, 39 bytes

lambda n:f"{n:33b}".translate("abc"*99)

Try it online!

Turns the given number into a binary string representation (padded with spaces), then maps the characters (space)-01 to caab with the str.translate function.

\$\endgroup\$
5
\$\begingroup\$

Jelly, 4 bytes

œ?ØẠ

A monadic Link accepting an integer which yields a list of 52 characters.

The input range may be up to somewhat more than \$-2^{223} \leq n \lt 2^{223}\$ since \$52!>2^{224}\$.

Try it online!

How?

œ?ØẠ - Link: integer, n
  ØẠ - alphabet (Jelly's longest built-in character list containing no digits
     -           or hyphen) = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
œ?   - permutation at index (n) if all 52! permutations were written out
     - in lexicographical order.
     - This indexing is modular so when n = -x we fetch the (52!-x)th entry.

So...

-2147483648 zyxwvutsrqponmlkjihgfedcbaZYXWVUTSRQPONIGCDAMLJKFEHB
-2147483647 zyxwvutsrqponmlkjihgfedcbaZYXWVUTSRQPONIGCDAMLJKFHBE
-2147483646 zyxwvutsrqponmlkjihgfedcbaZYXWVUTSRQPONIGCDAMLJKFHEB
-2147483645 zyxwvutsrqponmlkjihgfedcbaZYXWVUTSRQPONIGCDAMLJKHBEF
    ...
         -4 zyxwvutsrqponmlkjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDACB
         -3 zyxwvutsrqponmlkjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDBAC
         -2 zyxwvutsrqponmlkjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDBCA
         -1 zyxwvutsrqponmlkjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCAB
          0 zyxwvutsrqponmlkjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA
          1 ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
          2 ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxzy
          3 ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwyxz
          4 ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwyzx
    ...
 2147483644 ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmrtxwznoqpsvyu
 2147483645 ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmrtxwznoqpsyuv
 2147483646 ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmrtxwznoqpsyvu
 2147483647 ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmrtxwznoqpusvy
\$\endgroup\$
5
\$\begingroup\$

Ruby, 27 bytes

->n{('%34b'%n).tr'01','ah'}

Try it online!

('%34b'%n) Converts an integer into its binary representation, using ..1 to indicate a negative number (this is meant to represent an infinitely-long prefix of 1s), and left-pads this out to 34 characters using spaces. Then we replace the 0s with 'a' and the 1s with 'h' to create the Maniacal Base 2 Representation: strings like "haaahahahaaha" prepended with spaces and sometimes ... Since every step here is invertible this is 1:1.

Edit: Let the record show that @manatwork posted this identical solution first. Oops. I should've refreshed.

\$\endgroup\$
  • 3
    \$\begingroup\$ Lol. Your output is much funnier than mine. \$\endgroup\$ – manatwork Aug 16 at 15:58
4
\$\begingroup\$

Jelly, 6 bytes

Ø%+ṃØA

Try it online!

Since Jelly has arbitrary length integers, this monadic link takes an integer in the range \$±2^{31}\$ and returns a length 7 alphabetic string. It works by adding \$2^{32}\$ and then base decompressing into the capital letters.

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

C (gcc), 38 bytes

f(a,i){for(i=32;i--;putchar(a>>i&1));}

Try it online!

This expands each bit of the input integer into a byte that is either 0 or 1 (which are both unprintable characters, but there is no rule against that). So the output is always 32 bytes, and guaranteed to be unique.

\$\endgroup\$
  • \$\begingroup\$ Clever approach, similar to the Brainfuck response padding with NUL bytes. \$\endgroup\$ – maxb Aug 17 at 12:41
  • \$\begingroup\$ I'm not sure a program that doesn't terminate is really within specs... but if it is, 28 bytes? f(a){putchar(a&1);f(a/2);} \$\endgroup\$ – G. Sliepen Aug 17 at 12:49
  • \$\begingroup\$ I made an exception for Malbolge, but in general the program should terminate. Having "infinity" as the length is a bit of a cheat. \$\endgroup\$ – maxb Aug 17 at 17:01
4
\$\begingroup\$

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

x=>$"{x,11}".Select(n=>(char)(n+n))

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Crashes for x = int.MinValue as it can't be negated. \$\endgroup\$ – Hand-E-Food Aug 19 at 3:55
  • 1
    \$\begingroup\$ @Hand-E-Food Fixed by changing to an entirely different version \$\endgroup\$ – Embodiment of Ignorance Aug 19 at 4:42
3
\$\begingroup\$

Haskell, 31 bytes

map(\d->[d..]!!10).show.(+2^60)

Try it online!

Adds 2^60 to the input so that the resulting number has the same amount of digits for the whole input range. Turn into a string and shift each character 10 places to the right in the ASCII order (0 -> : ... 9 -> C).

\$\endgroup\$
  • 1
    \$\begingroup\$ Does it handle negative numbers? \$\endgroup\$ – maxb Aug 16 at 14:39
  • \$\begingroup\$ @maxb: no, but now it's fixed. \$\endgroup\$ – nimi Aug 16 at 15:32
3
\$\begingroup\$

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

x=>(new char[32]).Select(y=>(char)(x%2+65+(x/=2)*0))

Try it online!

Different approach to a c# solution, takes advantage of the fact that c# modulus is negative for negative numbers. I suppose you could shave off a byte or two if you allow non-display characters ('\0', and so on) by updating the +65... to not offset the character value to something human readable.

\$\endgroup\$
  • \$\begingroup\$ 44? - I am outputting non-printable characters (which seems ok) but you could convert to printable by adding 65 like your current answer. \$\endgroup\$ – dana Aug 17 at 17:18
  • 1
    \$\begingroup\$ 42 - This time with printable characters :) \$\endgroup\$ – dana Aug 17 at 17:26
3
\$\begingroup\$

Perl 5 -MDigest::MD5=md5_hex -p, 23 bytes

$_=md5_hex$_;y/0-9/k-t/

Try it online!

Previously:

Perl 5 -p, 29 bytes

$_=sprintf'%064b',$_;y/01/ab/

Try it online!

Converts the number to its 64 bit binary representation, then transliterates 0 and 1 to a and b, respectively.

\$\endgroup\$
  • 5
    \$\begingroup\$ And you have confirmed there are no collisions for all valid inputs? \$\endgroup\$ – Sparr Aug 16 at 22:45
  • \$\begingroup\$ ... and that no md5 hash produces a digit character by chance? \$\endgroup\$ – AlexR Aug 18 at 22:05
  • \$\begingroup\$ Forgot about the requirement to exclude digits. I've updated to accommodate that. \$\endgroup\$ – Xcali Aug 19 at 3:44
  • \$\begingroup\$ 9 bytes, 0 risk of MD5 collision. \$\endgroup\$ – Grimy Aug 19 at 12:51
2
\$\begingroup\$

T-SQL, 73 70 61 bytes

SELECT TRANSLATE(STR(n,11),'-0123456789','ABCDEFGHIJK')FROM t

I'm just directly replacing the digits (and -) with letters, after STR pads the integer to 11 characters. No conversion to hex or binary is necessary.

TRANSLATE was introduced in SQL 2017.

Input is via a pre-existing table \$t\$ with INT column \$n\$, per our IO rules. Range of the INT datatype in SQL is \$-2^{31} \leq n < 2^{31}\$.

EDIT: Saved 3 bytes by replacing manual padding with a conversion to CHAR(11), which is a fixed-width character format that automatically pads with spaces.

EDIT 2: Saved 9 bytes by using STR() function instead of CAST. STR converts a number to a text string padded to the specified length.

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

APL (Dyalog Unicode), 28 bytes

{11↑' Z'[⍵≤0],⎕A[⍎¨'¯'~⍨⍕⍵]}

Try it online!

Simple Dfn, taking an integer argument. Uses ⎕IO←0.

TIO links to a test case from -2^10 to 2^10. The 0~⍨ part removes the duplicate 0 from the arguments.

How:

{11↑' Z'[⍵≤0],⎕A[⍎¨'¯'~⍨⍕⍵]} ⍝ Dfn
              ⎕A[         ]  ⍝ Index the Uppercase Alphabet with
                        ⍕⍵   ⍝ String representation of the argument
                   '¯'~⍨     ⍝ Without the character ¯
                 ⍎¨          ⍝ Executing each digit back into integers
             ,               ⍝ Prepend
    ' Z'[   ]                ⍝ A character from the string ' Z' indexed by
         ⍵≤0                 ⍝ Argument ≤ 0. Returns 1 if true, else 0.
                             ⍝ This will prepend a whitespace to positive numbers, and a Z otherwise.
 11↑                         ⍝ Take the first 11 characters, padding with whitespace.
\$\endgroup\$
2
\$\begingroup\$

Japt, 6 bytes

I think this is right. Inspired by Stephen's Python solution so please +1 him.

¤ùI cÑ

Try it

¤ùI cÑ     :Implicit input of integer
¤          :Convert to binary string
 ù         :Left pad with spaces
  I        :  To length 64
    c      :Map codepoints
     Ñ     :  Multiply by 2
\$\endgroup\$
2
\$\begingroup\$

Malbolge, 2708 bytes

This answer is super cheaty, because it always produces the same amount of input, which is equal to \$ \infty \$.

bP&A@?>=<;:9876543210/.-,+*)('&%$T"!~}|;]yxwvutslUSRQ.yx+i)J9edFb4`_^]\yxwRQ)(TSRQ]m!G0KJIyxFvDa%_@?"=<5:98765.-2+*/.-,+*)('&%$#"!~}|utyrqvutsrqjonmPkjihgfedc\DDYAA\>>Y;;V886L5322G//D,,G))>&&A##!7~5:{y7xvuu,10/.-,+*)('&%$#"yb}|{zyxwvutmVqSohmOOjihafeHcEa`YAA\[ZYRW:U7SLKP3NMLK-I,GFED&%%@?>=6;|9y70/4u210/o-n+k)"!gg$#"!x}`{zyxZvYtsrqSoRmlkjLhKfedcEaD_^]\>Z=XWVU7S6QPON0LKDI,GFEDCBA#?"=};438y6543s1r/o-&%*k('&%e#d!~}|^z]xwvuWsVqponPlOjihgIeHcba`B^A\[ZY;W:UTSR4PI2MLKJ,,AFE(&B;:?"~<}{zz165v3s+*/pn,mk)jh&ge#db~a_{^\xwvoXsrqpRnmfkj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    \$\begingroup\$ With it being Malbolge, I might let that slide... Fantastic job! \$\endgroup\$ – maxb Aug 17 at 12:39
2
\$\begingroup\$

Perl 6, 12 bytes

*~|('@'x 11)

Try it online!

Anonymous Whatever lambda that takes a number and string ORs it with 11 @s. This maps the digits to pqrstuvwxy and the dash to m, then pads the string out to 11 characters with @s

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

Perl 5 (-p), 9 bytes

$_^=A x$=

Try it online!

Bitwxise-xor of the input with the string AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA.

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

Wolfram Language (Mathematica), 44 33 bytes

Echo@Table[Or[i>#+13!,],{i,14!}]&

Try it with a smaller domain

-2 thanks to Greg Martin

Prints >> , followed by the 523069747202-character string representation of a list of \$13!+n\$ Nulls padded by Trues to a length of \$14!\$, and a newline. Works on the domain \$[-13!,14!-13!)\$, which is a superset of \$[-2^{31},2^{31})\$.

Given the size of the output, I've included a test case with a smaller domain of \$[-2^4,2^4)\$ instead

\$\endgroup\$
  • \$\begingroup\$ Nice :) I think you can save two bytes by changing 2^31 and 2^32 to 13! and 14! respectively. At the loss of some "brevity" in the output.... \$\endgroup\$ – Greg Martin Aug 17 at 6:20
1
\$\begingroup\$

Stax, 6 bytes

11){Hm

Run and debug it

Procedure:

  1. Left pad input to 11 characters.
  2. Double each non-space ascii character.
\$\endgroup\$
1
\$\begingroup\$

PHP, 64 54 bytes

-10 bytes by using strtr function instead of manual character replacement.

<?=str_pad(strtr($argn,'-0123456789',ADEFGHIJKLM),20);

Try it online!

Largest int value possible in PHP as of now is 9223372036854775807 which is 19 digits long, considering the minus sign in negative numbers, it will be 20. The code above replaces minus sign (-) with the A character and every digit from 0 to 9 with a character from D to M and then pads the string in the right with space character to always make it 20 characters long. For example, the output for input of -9876543210 is "AMLKJIHGFED ".

The output is unique for each integer input and you can get back to the input by removing all spaces, replacing A with - and replacing D to M with 0 to 9.


PHP, 44 bytes

<?=str_pad(base64_encode(decbin($argn)),88);

Try it online!

This is same idea as Arnauld's answer. Converts the input to binary, and then converts it to base-64. Also pads it to 88 characters (largest length is for -9223372036854775807 which is 88 characters) with space character at right to always get same length in the output.

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

Retina 0.8.2, 21 bytes

T`p`l
$
10$*o
!`.{11}

Try it online! Always outputs 11 characters from the range n..z. Explanation:

T`p`l

Translate the printable ASCII characters to the lowercase letters. This maps - to n and 0..9 to q..z. (It's really fortunate that the digits are the 16th to the 25th printable ASCII characters!)

$
10$*o

Append 10 os. Since the input will have between 1 and 11 characters, there are now between 11 and 21 characters.

!`.{11}

Extract the first 11 characters. Since there are fewer than 22 characters, this will only match once.

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

Charcoal, 9 bytes

◧⍘﹪NXχχαχ

Try it online! Link is to verbose version of code. Always outputs 10 spaces and uppercase letters. Explanation:

   N        Input as a number
  ﹪         Modulo
     χ      10
    X       To power
      χ     10
 ⍘     α    Convert to base 26 using uppercase letters
◧       χ   Left pad to length 10
\$\endgroup\$
1
\$\begingroup\$

R, 37 bytes

set.seed(scan())
cat(sample(LETTERS))

Try it online!

Looks like the output is random, but it isn't! The input is used as the seed of the Pseudo-Random Number Generator, and we then get one of the \$26!=4\cdot 10^{26}\$ permutations of the alphabet. The output is always of length 51 (26 letters + 25 spaces).

There is still the issue of insuring that all the outputs are different. We end up with \$2^{32}\approx 4\cdot 10^9\$ permutations (out of \$4\cdot 10^{26}\$). If we pretend that the permutations are distributed uniformly at random, then the probability that all the permutations are different can be computed following the same calculations as for the Birthday problem. The probability that 2 specific outputs are identical is \$10^{-17}\$, so a first order approximation of the probability that all \$2^{32}\$ outputs are distinct is

\$1-\exp(-2^{64}/26!)\approx0.99999998\$

which is close enough to 1 for me.

\$\endgroup\$
  • \$\begingroup\$ While yes, it's statistically unlikely that there's a collision, that doesn't mean it's impossible as the question asks for \$\endgroup\$ – Jo King Aug 17 at 9:59
  • 2
    \$\begingroup\$ I like the probability calculation. I guess you could run through all 4 billion inputs if you really wanted to check for collisions, but if you don't feel like doing that I might let it slide. As it is now, you've presented a compelling argument, and if anyone finds a counterexample, you'd have to modify your submission. \$\endgroup\$ – maxb Aug 17 at 10:11
1
\$\begingroup\$

brainfuck, 20 19 bytes

-1 byte thanks to Krzysztof Szewczyk

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

Try it online!

Outputs the number with each digit and dash mapped to 255 minus their ordinal value, padded out to 255 characters with NUL bytes.

\$\endgroup\$
  • 1
    \$\begingroup\$ You can save one byte: -[>,[->-<]>.[-]<<-] \$\endgroup\$ – Krzysztof Szewczyk Aug 17 at 11:46
1
\$\begingroup\$

R, 40 37 bytes

cat(gsub("S","",intToBits(scan())>0))

Try it online!

An alternative to Robin Ryder's answer; this is certainly deterministic.

This converts the input to a raw vector of 32 bytes, each byte being a hex number 00 or 01 representing the bits of the integer. We then coerce to a logical by comparing to 0, so 00 is mapped to FALSE and 01 to TRUE. Then we need to remove a single letter from each FALSE to guarantee equal-length output, arbitrarily selected to be S. Result is printed (with space) for a length of 169.

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

Zsh, 43 bytes

for c (${(s::)${(l:30::0:)1}})echo \\x$[#c]

               ${(l:30:0:)1}                 # left-pad with zeroes to 30 characters
        ${(s::)             }                # split characterwise
 for c (                     )               # for each character
                                      $[#c]  # get character code (in decimal)
                              echo \\x$[#c]  # interpret as hex code, print (with newline)

Try it online!

This solution gets around the long long limits of Zsh's integers by working with only characters. I only padded it to 30 characters for readability, but replacing 30 with 99 will allow this method to work on all numbers from -1E99+1 to 1E100-1.

The effect of interpreting the decimal codes as hexadecimal are as follows:

0 => 48 => H    1 => 49 => I    2 => 50 => P    3 => 51 => Q
4 => 52 => R    5 => 53 => S    6 => 54 => T    7 => 55 => U
8 => 56 => V    9 => 57 => W    - => 45 => E

Zsh, 46 bytes

integer -i2 -Z66 x=$1
<<<${${x//[02-]/a}//1/b}

Try it online!

Declares x as a binary number, zero-padded to a width of 66. Then maps 0a and 1b. We also map 2 and - to a, since those characters are printed in [[-]][base]#[num] notation. To see what $x looks like before replacement, and Zsh's limits in parsing integer types, check the Debug output in the TIO link.

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

Java (JDK), 42 bytes

n->"".format("%8x",n).chars().map(i->i|64)

Try it online!

First, this creates the hexadecimal representation of the input, left-padded with spaces which provides the same length constraint (8 characters-long), removes the minus sign, and keeps each intermediate output unique.

This gives a string with 17 different possible characters: 0123456789abcdef and space.

Then each character is streamed and mapped by adding 64 to its codepoint if it's a digit or a space. Effectively, this results in the following mapping: 0123456789abcdef<space> to pqrstuvwxyabcdef` which has 17 different characters, so no two numbers will result in the same output.

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

Bash, 30 bytes

echo $1|md5sum|tr '0-9-' 'A-K'

Try it online!

To prove that output is unique, I just googled for MD5 collisions, and found no results within the integers between \$-2^{31}\$ and \$2^{31}\$. To avoid having forbidden characters in the output, just translate the characters in question to be upper case letters. Output is always the same length by definition, and guaranteed to not contain any forbidden characters.

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