r/dailyprogrammer • u/Coder_d00d 1 3 • Jul 18 '14
[7/18/2014] Challenge #171 [Hard] Intergalatic Bitstream
Description:
Keeping with our "Bit" theme this week. We will look into the future. It is 2114. We have colonized the Galaxy. To communicate we send 140 character max messages using [A-Z0-9 ]. The technology to do this requires faster than light pulses to beam the messages to relay stations.
Your challenge is to implement the compression for these messages. The design is very open and the solutions will vary.
Your goals:
- Compact 140 Bytes down to a stream of bits to send and then decompact the message and verify 100% data contained.
- The goal is bit reduction. 140 bytes or less at 8 bits per byte so thats 1120 bits max. If you take a message of 140 bytes and compress it to 900 bits you have 220 less bits for 20% reduction.
Input:
A text message of 140 or less characters that can be [A-Z0-9 ]
Output:
Read Message of x Bytes.
Compressing x*8 Bits into y Bits. (z% compression)
Sending Message.
Decompressing Message into x Bytes.
Message Matches!- x - size of your message
- x* 8 = bits of your message
- z - the percentage of message compressed by
- y bits of your bit stream for transmission
So compress your tiny message and show some stats on it and then decompress it and verify it matches the original message.
Challenge Inputs:
three messages to send:
REMEMBER TO DRINK YOUR OVALTINE
GIANTS BEAT DODGERS 10 TO 9 AND PLAY TOMORROW AT 1300
SPACE THE FINAL FRONTIER THESE ARE THE VOYAGES OF THE BIT STREAM DAILY PROGRAMMER TO SEEK OUT NEW COMPRESSIONCongrats!
We are a trending subreddit for today 7-18-2014. Welcome to first time viewers of /r/dailyprogrammers checking out our cool subreddit. We have lots of programming challenges for you to take on in the past and many to look forward to in the future.
7
u/quickreply100 Jul 18 '14
My approach is to substitute the 256 most common English words which are over 2 letters in length, and signal this with 0x00, followed by the word's index in a table.
This method does have one worst case scenario which is particularly bad. A message consisting entirely of 0s would have -100% compression.
Obviously this was not intended to be competitive, compression ratio wise!
(written in C#) http://www.hastebin.com/iboyopujew.cs
7
u/MaximaxII Jul 18 '14 edited Jul 19 '14
I'll start out by saying that I have no formal education in any of this. I have never written any compression script before, haven't heard of Huffmann coding, so I sort of invented my own algorithm.
Here are the rules that I set for myself:
00is used to separate characters.0s as it may, but it can't start on a0, and it can't have 2 or more consecutive zeros in the middle.I get an average of 36,3% (the coded message is 63,7% of the original), which is decent, but I think that I can do better - if I move some characters around in my dictionary, and apply a few other tricks to avoid redundancy, I think I can do this. I think I can cross 40%, but that'll be for tomorrow.
Any feedback is more than welcome!!
#Challenge #171 Hard - Python 2.7
#Output
Github (MIT License)
1
u/Phirazo Jul 18 '14
If we have superluminal transmitters, does that mean we have superluminal processors?
3
u/thestoicattack Jul 18 '14
Bash implementation of Huffman coding. Obviously sending the tree with the message is going to totally defeat the purpose, but suppose sender and receiver both have an identical corpus of representative text that they can learn the tree from. Mostly I just wanted to write a Huffman coding thingy in bash.
3
u/Godd2 Jul 18 '14
Pretty straightforward in Ruby. I just created my own encoding since I know what characters will exist:
Of course, as a result, I have the same compression percentage no matter what the message is:
3
u/KillerCodeMonky Jul 21 '14 edited Jul 21 '14
Simple solution: A-Z and space can be encoded in 5 bits. So encode any message without numbers with 5 bits. This provides a 62.9% compression ratio for 140-character messages.
Any message with numbers, encode in 6 bits. This provides 75.7% compression ratio for 140-character messages.
6-bit encoding also provides an extra 27 unused combinations. A more complex solution could take advantage of this.
CODE: https://dotnetfiddle.net/PmTpsZ
1
u/XenophonOfAthens 2 1 Jul 18 '14 edited Jul 18 '14
I didn't do anything fancy here, I just interpreted the input as a number in base 37, converted the number to bytes, and then back again. I achieve 65% compression doing this (i.e. the compressed version is 0.65 times the size of the original, I guess you could call that 35% compression if you want). Here's the code in Python 3:
Edit: for such a short message, I wonder if it's worth doing anything more fancy than just encoding it like I did. Things like Huffman codes are going to use up way too much in headers and such. I suppose run-length coding combined with a Burrows-Wheeler transform might work, but that would heavily depend on the entropy of the input, and maybe 140 characters isn't enough to make it worth it.
And here's the output for the "final frontier" example string:
1
u/Godspiral 3 3 Jul 18 '14 edited Jul 18 '14
In J, using huffman codes (see lab included with J), and massive cheating of using the sample 3 sentences for the frequency table.
input =. dltb each cutLF wd 'clippaste'
hist =: ~. ,.&< #/.~
] 'Let Freq' =: hist ; input NB. frequency distribution of each char (the cheating part instead of taking from english dict)
distribution of code sizes (2 are 3 digits, and 2 7 digits)
hencode=: 4 : '(>{.x) ;@:{~ (>{:x) i. y'
encodings =: ((Freq <@:(huffc ; ]) Let) hencode &> ]) input
decodeTbl =: hst Freq (huffc ; ]) Let NB. hst from huffman encode lab and "involved" (more than 1 line)
hdecode=: 4 : '(>{:x) {~ (3;{.x);:y'
1
u/skeeto -9 8 Jul 18 '14 edited Jul 18 '14
C++11 using Huffman coding. It turned out longer than I expected.
You have to feed it a corpus of sample text, which it analyzes to build the Huffman tree. The sample text doesn't need to be formatted in any special way, other than being UTF-8 or ASCII. It treats whole words as symbols, so it can only encode words that appear in the corpus. This is a problem for the sample inputs because I don't have good samples that have words like OVALTINE, PROGRAMMER, and COMPRESSION all at the same time.
There's a 3-bit header which tells the decompressor how many bits are in the last byte of input. A value of 0 actually means 8 (no padding bits), since 0 wouldn't make sense. Otherwise is just a pure run-length encoding.
If I use the last challenge message (110 bytes) using my system's dictionary (
/usr/share/dict/words) as the corpus, I get 36% compression ratio. So I got that goin' for me, which is nice.If I use Pride and Prejudice as both the corpus and the input, I get a 21% compression ratio. Unlike the corpus, the input must be formatted properly.
For small inputs, this beats real compression programs, but that's really because I only have 3-bit overhead, and the overhead dominates for small inputs.
1
u/Laremere 1 0 Jul 18 '14 edited Jul 19 '14
My solution relies on probability of the contents of space age messages. The example three messages manage to compress all the way down to two bits total. In the worst case it has a 25% compression.
Go playground link: http://play.golang.org/p/ylnpCGiBGh
Solution:
package main
Output:
Edit: Calculated compression incorrectly
1
u/stillalone Jul 19 '14 edited Jul 19 '14
I normally don't do any of the /r/dailyprogrammer problems but this one seemed like it was kind of fun. I kind of cheated and assumed that numbers were rarer than letters or spaces. So I encoded letters and spaces into a 5 bit field. Since 5 bits can have 32 values that means I had 5 unused values. the 5 unused values indicated that you were encoding a number the value (from 1 to 5) indicated the number bits that number was x 2 followed by the number encoded in those bits, so you could encode a 10 bit number in 15 bits.
The python code got complicated, partly due to my use of python 2.7, which didn't have nonlocal. I ended up converting numbers into strings of ones and zeros and back again to save me the hassle of bit string manipulation and buffering bits (again, since I didn't have nonlocal). I also use a regex to break the string up into numbers and letters which I also used to make sure the string matched the [A-Z0-9 ] and group digits together up to groups of three. the groups of three for numbers means that I could encode numbers 0...999 with 7 to 15 bits. I should be able to encode 1000...1023 as one number seemed too complicated so they end up being encoded as two separate numbers.
The compression ratios:
EDIT: CRUD!! I didn't really handle numbers with leading 0s properly. I'd have to separate them out in the regex to preserve the algorithm. Basically each trailing 0 would be encoded separately and cost an additional 7bits.
1
u/Mattgame555 Jul 19 '14
First time submitting here and decided to go for VB.net because we don't see it used on here much at all. Not an overly complex piece of code but it was long because of all the Select...Case Statements so I decided to post it on PasteBin. Feel free to comment with suggestions or criticisms.
PasteBin Link: http://pastebin.com/dN1fvw1S
1
u/Regimardyl Jul 19 '14
My Haskell solution
Note that I didn't include the output describing what is being done, I might do that later (4:30am now …)
Approach:
Encoder:
Decoder:
1
u/turtle_with_dentures Jul 19 '14
So when listing the compression percentage do we show the % the end result is of the total or the amount of compression we achieved.
Ex:
If "HELLO" is normally 40 bits and we compress it to 30 bits should the compression be displayed as
or
1
u/mikrostew Jul 19 '14
I read up on Huffman Coding and redid this challenge, still in Ruby. I used letter frequencies from this page, instead of calculating them from the message itself, so that a known encoding can be used at both ends.
I wasn't sure how to weight the numbers, so I played around with them until they didn't hurt the compression too much in the 2nd message.
Output:
1
u/apentlander 0 1 Jul 19 '14 edited Jul 19 '14
I implemented it in golang. I encoded the letters into 6 bits per character, allowing to 64 total characters. Since there are only 36 character that are valid, I used some of the remaining space to represent the most common English digraphs and trigraphs. No fancy compression algorithms here! I tried to keep it super simple. The minimum compression ratio is 25%. Here is: http://play.golang.org/p/Bjsvkn8xfW Feedback would be great!
1
u/healthycola Jul 24 '14
I used Huffman Encoding in C++ to do this. At first I used a random ebook I found to figure out the frequencies of letters in general in a sample of english literature (I got the ebook in plaintext from Project Gutenberg). That got me the following results (45.5, 40, 48.5).
Then I tried the same but performed the encoding specifically for the test set. With that I got the following results (46%, 45%, 48%).
The code can be checked out here.
Great problem! It was fun to solve.
1
u/scruft Jul 19 '14 edited Jul 19 '14
This is my first attempt at one of these challenges. I went with a rice/golomb-type approach (I think thats what it's called, not sure). Code here. Python 2.7.
I've ordered the characters by frequency (I've cheated a little here and used the 3 given messages to measure the frequency). The most frequent I represent with 0000, the second with 0001 and so on until we get to 16th, where I use 11110000 and continue, 11110001, etc. Some example codes below:
Full output here. It's consistently around 40%. Best case is 50%, for example 'TEST' (8 bits per char to 4 bits per char), Worst case is -50%, for example 'ZZZ'.
I tried different number of bits, but 4 gave the best compression.
0
u/mikrostew Jul 18 '14 edited Jul 18 '14
Ruby implementation. It's a simple conversion from 8-bit character to 6-bit index in an array of possible characters, so the maximum possible compression is 25%. Edit: also need 8 bits for the length, which further decreases the compression.
Output:
-1
u/ENoether Jul 18 '14
Technically I'm compressing to a list of bytes rather than a stream of bits, but it's essentially the same thing. Compresses up to 25%, depending on the length of the message.
Technically technically I'm compressing to a list of integers rather than a list of bytes, but they're integers that could be held in single bytes.
Python 3 (As always, feedback and criticism welcome):
Output:
0
u/[deleted] Jul 18 '14
[deleted] — view deleted comment
0
u/[deleted] Jul 18 '14
[deleted] — view deleted comment