Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

Introduction

Some ASCII characters are just so expensive these days...

To save money you've decided to write a program that encodes expensive characters using inexpensive ones.

However, character prices change frequently and you don't want to modify your program every time you need to encode or decode a different character! You'll need a more dynamic solution.

Challenge

Your task is to write two programs: an encoder and a decoder.

The encoder should accept a list of five inexpensive characters, and a single expensive character.

It should output a single string made up of the inexpensive characters, that encodes the expensive character.

This string may not be longer than 4 characters, to remain inexpensive. However, it does not have to use all of the inexpensive characters in the encoding and encodings may be of different lengths.


The decoder should accept the string outputted by the encoder, and output the expensive character.

The decoder shall accept no input other than the encoded string. It must work, unmodified, from the encoder's output for any (valid) combination of inputs. In other words, your decoder program doesn't know which characters are expensive or inexpensive.

Scoring

Shortest combined code wins!

Notes

  • All characters will be either uppercase letters [A-Z], lowercase letters [a-z], or numbers [0-9].

  • The list of inexpensive characters won't contain duplicates. No character will be both inexpensive and expensive.

  • The encoder and decoder don't have to be written in the same language, but they can be. You may write a program or a function.

  • Input and output may be in any reasonable format for your language.

  • The two programs may not share any variables or data.

Summary

  • Input of some inexpensive characters and an expensive character is given to encoder.

  • Encoder outputs a string of inexpensive characters, encoding the expensive character.

  • Decoder is given the encoder's output, and outputs the expensive character.

Examples

Input:     a, b, c, d, e     f

Encoder Possibilities:     a     eeee     caec

Decoder:     f


Input:     a, b, c, d, e     h

Encoder Possibilities:     bc     cea     eeaa

Decoder:     h


Input:     q, P, G, 7, C     f

Encoder Possibilities:     777     P7     PPCG

Decoder:     f

share|improve this question
    
This really just could be me, and I apologize for this question if it is, but how exactly are you supposed to encode your message with the inexpensive characters? Addition of the ASCII codes for the 5 inexpensive characters? Actually, this question only has a basis if your decoder must decode for all encoding possibilities generated. –  Cole 17 hours ago
    
To be clear: The Encoder Possibilities are just examples and we can encode each character as we want, yes? –  Dennis 17 hours ago
    
@Dennis Yes, those are just examples. –  UndefinedFunction 17 hours ago
    
@Cole Without giving away an actual algorithm, as that is the main challenge here, I believe that it is possible. There are only 62 possible expensive letters to encode, and with these 4 ascii characters up to 92 can be encoded, according to A239914. (huge thanks to PhiNotPi's sandbox comment for this one - I didn't calculate exactly how many could be encoded) –  UndefinedFunction 17 hours ago
    
@UndefinedFunction I realize now what you've intended: Dennis's question answered what I was confused about. –  Cole 16 hours ago

2 Answers 2

CJam, 55 50 48 bytes

Encoder, 24 22 21 bytes

l$:L4m*{$L|L=},rc'0-=

Try it online.

Decoder, 31 28 27 bytes

l_${|}s|:L4m*{$L|L=},\a#'0+

Try it online.

share|improve this answer

Pyth, 46 bytes

Encoder, 22 bytes

@smfql{Td^<Szd4S4-Cw48

Decoder, 24 bytes

C+48xsmfql{Td^<sS{zd4S4z
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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