The goal is to write a program that encodes an other program (input) with the fewest character possible.


  • The score is equal to the different number of characters needed for the output.
  • Lower score is better.


  • No target languages with a limited set of commands. (No Brainf**k, Whitespace etc)
    Edit: I mean at least 26 significant characters, A doesn't change the way a brainf**k program operates, so you can't count this character. Same applies to whitespace.
  • The target language must exist at the time where this question is written.
  • You have to include a small explanation how you archive your score.
  • The input program is valid.
  • The encoded program must be a valid program in the same language as the input.
  • The encoded program must do the same job as the original program.
  • Your encoder must work for every valid program in that language.
  • Include some sample input and output.


  • The encoder might be written in any language, not only in the language it targets.
  • This is not , readable programs are encouraged.
  • The great goal is to see how many different characters are needed to write anything in that language. I disallowed BF etc because there would be no challenge.
  • This was inspired by Print a string in as few distinct characters as possible, you could take it as for that question.


In Java, you can use \uXXXX instead other characters. A valid entry encodes every character from the input this way. This would have a score of 18. (\ 0-9a-f)

Code in Tcl, encodes a Java program:

set res {}
foreach char [split [read stdin] {}] {
    append res [format \\u%04x [scan $char %c]]
puts $res
  • \$\begingroup\$ sorry for the vote to close... thought this was a duplicate of codegolf.stackexchange.com/questions/6974 but that one's python-specific. \$\endgroup\$ – boothby May 15 '13 at 0:12
  • 2
    \$\begingroup\$ With you Java score and your encoder: I don't speak much Tcl, but doesn't gets read a single line of input only? And you missed the u in your encoder (but on the other hand you don't need the space and thus the score remains the same). \$\endgroup\$ – Howard May 15 '13 at 4:40
  • 7
    \$\begingroup\$ Don't all programming languages have a "limited set of commands"? Where do you draw the line? \$\endgroup\$ – jpjacobs Sep 15 '14 at 10:53
  • 4
    \$\begingroup\$ I'm voting to close this because it has a broken victory condition patched over by a very unclear ban on languages. If Unary wasn't banned, it'd score 1. If brainfuck wasn't banned, it'd score 7 (note that the language itself naturally has 8 commands + 248 comment characters). So where's the line between naturally having too few commands, and having enough to be legitimate. \$\endgroup\$ – user62131 Dec 28 '16 at 11:10
  • 1
    \$\begingroup\$ Even languages like Python have a limited set of commands. You can't "make a new one", you simply create a new function or something, but the reserved words stay the same. The definition of "command" is unclear. The question could possibly be fixed by not limiting languages (just don't upvote trivial answers), and requiring that the set of characters used must be a proper subset of those in the language. \$\endgroup\$ – mbomb007 Dec 28 '16 at 14:28

GolfScript / GolfScript, score 4


The encoder itself is a GolfScript program which takes the original code on STDIN and transforms it into a sequence of characters ',+~. This output itself is valid GolfScript code which performs the same operations as the original version.

The basic method consists of an encoding of the code as a string (using chars ',+, see below) and then evaluate this string using the eval command ~.

If one concatenates any string together with an array of numbers in GolfScript the numbers are converted to code points and the result is a string itself. Thus the string encoding simply builds a list of numbers (from the code points of the input code) and then concatenates all those with an empty string.


The input code


is translated into (note: line breaks and comments added for readability)

# initialize an empty array []

# append number 34 (length of string ',,, ... ,,,')

# ... other numbers

# result is array [34 104 105 34 10 112]

# append empt string, i.e. convert array to string (results in "\"hi\"\np")

# evaluate
| improve this answer | |

Python -> Python, 8 distinct characters

def minimal_python(input_code):
    """Convert Python code to minimal Python code."""

    # Create a list of the ordinal numbers of <input_code>'s characters.
    # '%' signs have to be treated specially and are represented with -1.
    ords = []
    num_escaped_chars = 0
    for char in input_code:
        if char == '%':
            num_escaped_chars += 1

    modulo_sign_escape = '%' * 2**num_escaped_chars
    def formatters():
        num_escaped_chars_so_far = 0
        for o in ords:
            if o == -1:
                yield modulo_sign_escape
                yield '%' * 2**num_escaped_chars_so_far + 'c'
                num_escaped_chars_so_far += 1
    format_str = "'" + ''.join(formatters()) + "'"

    values_str = ''.join('%' + '-~'*o + '0' for o in ords if o != -1)

    return 'exec' + format_str + values_str

This uses modulo formatting to rebuild the input string. For example, print 1 results in this program:


In theory you can encode any program like this, but the resulting program will always have more than 2n characters, where n is the number of characters in the input, not including % symbols.

| improve this answer | |
  • \$\begingroup\$ The lower bound of the resulting program is 2**n+3*n-1 + 6 characters, including all input characters (if you assume every character is a NUL byte). This is taken from OEIS A132074, plus 6 for exec''. If you assume characters must be printable ASCII, then the lower bound is higher. repl.it/EHEN \$\endgroup\$ – mbomb007 Oct 26 '16 at 19:09

CJam -> CJam, score: 3

CJam is newer than the question so it's not eligible to win.


It uses ')~. ') is the character ), and each extra ) increments it by one. ~ can evaluate a character or a string. The complete program is evaluated after concatenating all the characters by evaluating +. And an integer of the character value and a number-to-character operation is evaluated for each character less than ).



is translated into:




is translated into:

| improve this answer | |

APL (score: 10)


Characters used in encoding: ⍎⎕AV[(⍴⍬)]

The APL program to encode must restrict itself to the APL charset and not use any extra Unicode.

Encoding for the program 42 (which outputs the answer to Life, the Universe, and Everything):


How it works: is the empty list, ⍬⍬⍬⍬ is therefore a list of four empty lists, ⍴⍬⍬⍬⍬ is the length of the list of four empty lists, which is four. Dyadic is reshape, so the length-of-list-of-lists instructions must be wrapped in parenthesis, giving the final encoding of character number four as (⍴⍬⍬⍬⍬).

These numbers are then looked up in ⎕AV which is the character set, and executes the resulting string.

(At first sight it looks like the score could be brought to 9 by using ⎕UCS instead of ⎕AV and saving [], but that doesn't work, because (⍴⍬)(⍴⍬) is a list of lists of scalars, i.e [[1], [1]] instead of the [1, 1] that would be required, and working around that would require interspersing the encoded values with commas, bringing the score back up to 10.)

| improve this answer | |

RProgN, 7 Distinct Characters, Noncompeting

RProgN is newer than this question.

' do.Lc

Convert the program to 'oooo...' L c 'ooooo...' L c 'oo...' L c . . . do, where each 'oo...' represents the character code in o's, surrounded by apostrifies to make a string out of them. L c then turns them into a number constant. After all the strings are placed on the stack, a chain of .'s equal to the amount of characters in the encoded string minus one rebuild the encoded string, and do executes it.

Encoder, also written in RProgN.

'oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'ooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooooooooooooooooo' L c 'ooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'ooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'ooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooooo' L c 'ooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'ooooooooooooooooooooooooooooooooooooooo' L c 'ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'ooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooooo' L c 'ooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'ooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'ooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'ooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'ooooooooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'ooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'ooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooooooooooooooooo' L c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . do

Try it online!

| improve this answer | |

Ruby -> Ruby, 8

#Convert a number from 0 to 255 to the form "111+11+11+1+1+1"
def byte_to_ones(byte)
  output = []
  ones = [111,11,1]
  ones.each do |one|
    while byte>=one

bytes = $<.each_byte.map { |byte| byte_to_ones(byte) }

puts "eval''<<#{bytes.join('<<')}"


puts "Hello, world!" => eval''<<111+1<<111+1+1+1+1+1+1<<111+1+1+1+1+1<<111+1+1+1+1<<11+11+1+1+1+1+1+1+1+1+1+1<<11+11+11+1<<11+11+11+11+11+11+1+1+1+1+1+1<<11+11+11+11+11+11+11+11+11+1+1<<11+11+11+11+11+11+11+11+11+1+1+1+1+1+1+1+1+1<<11+11+11+11+11+11+11+11+11+1+1+1+1+1+1+1+1+1<<111<<11+11+11+11<<11+11+1+1+1+1+1+1+1+1+1+1<<111+1+1+1+1+1+1+1+1<<111<<111+1+1+1<<11+11+11+11+11+11+11+11+11+1+1+1+1+1+1+1+1+1<<11+11+11+11+11+11+11+11+11+1<<11+11+11<<11+11+11+1<<1+1+1+1+1+1+1+1+1+1

Implements the Ruby solution given in the linked problem and replaces I/O with eval.

| improve this answer | |
  • \$\begingroup\$ It was already linked in the question body; I wasn't using your comment. It's non-trivial to port answers over from there since you need to reverse-engineer them. \$\endgroup\$ – histocrat May 15 '13 at 15:34
  • 1
    \$\begingroup\$ I missed the reference to that question in the question body. I still don't think it's very interesting to just transform answers from it, but I withdraw my previous comment with apologies. \$\endgroup\$ – Peter Taylor May 15 '13 at 16:04

05AB1E, 8 distinct bytes (non-competing)


Non-competing, because this version of 05AB1E was released mid-2018 (and the very first version of the 05AB1E legacy was released December 2015, although that version lacked the builtin žĆ anyway).

Try it online.


žĆ        # Push the 05AB1E code-page
  IS      # Push the input, and convert it to a list of characters
    k     # Get the index of each character in the codepage-string
     '>× '# Convert each index to a string of that many repeated ">"-characters
€         # For each ">"-string:
 "žĆ0ÿè"  #  Map it to the string "žĆ0ÿè",
          #  where the `ÿ` is automatically filled with the ">"-string
J         # Join these strings together
 …J.V«    # And append "J.V" at the end
      ?   # After which we output the string without trailing newline

This will produce a program with the 8 distinct bytes žĆ0>èJ.V in the following format:

Multiple žĆ0>...>è for each character of the program we input (where the >...> are between potentially none up to a maximum of 256 amount of >):

  • žĆ: Push the 05AB1E code-page
  • 0: Push a 0
  • A certain amount of > (potentially none, up to a maximum of 256), which each increases by 1
  • è: Index this integer into the code-page string

And after that:

  • J: Join these indexed characters on the stack together without delimiter
  • .V: And evaluate this string as 05AB1E code
  • (after which the result is output implicitly)

Try such a program for ( will push the lowercase alphabet & palindromize it).

| improve this answer | |

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.