------ art_85875_22495611.1179085089902
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
Well, first time I actually submit my solution, it's a bit hairy, but it
worked in my tests...
I made the decoder too, only problem was serialization, I did not know what
to do with it, and how this in real apps work, so that part is a bit odd
(programming is just hobby for me).
usage:
ruby quiz123.rb encode this!
and
ruby quiz123.rb -d
The app will prompt you for the tree and the part to decode. The tree should
be a hash definition ex: { "a" "0", "b" "10" }. (To test, you can
first encode something (it will show the tree in correct format) en then
decode it again.
What puzzled me was that longer texts appeared to take up more bytes encoded
than decoded.
anyway, here is my code:
#! /usr/local/bin/ruby
class Huffman
attr_accessor :tree, :encoded
def initialize
@encoded, @tree il
@line "
end
def encode( line )
@line ine
chars ine.split("")
uniq hars.uniq
counts ]
uniq.each do |char|
counts << { :char char, :count chars.select{ |x| x char
}.length }
end
counts.sort!{ |x,y| x[:count] <