--Apple-Mail-4--1026478836 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset -ASCII; formatðïwed Begin forwarded message: > From: "Nicolas Modrzyk" <hellonico / gmail.com> > Date: May 14, 2007 2:57:48 AM CDT > To: submission / rubyquiz.com > Subject: QUIZ 123 # Huffman Encoder > > --Apple-Mail-4--1026478836 Content-Transfer-Encoding: 7bit Content-Type: application/x-ruby; x-unix-mode66; name ffman_encode.rb Content-Disposition: attachment; filename ffman_encode.rb #! /usr/bin/ruby # Ruby Quiz #123 # Copyright 2007 Nicolas Modrzyk class Huffman def encode string, tree l tree reate_encoding_tree string if tree il @tree ree encoded " string.each_byte {|x| encoded << tree[x.chr].to_s} encoded end def decode string, tree l tree tree if tree nil decoding_tree ree.invert decoded " last_index current_index len tring.length while (current_index < en) s tring.slice(last_index,current_index-last_index) #puts ":"+s+":"+last_index.to_s+":"+current_index.to_s if decoding_tree.has_key?(s) decoded << decoding_tree[s] last_index urrent_index #p "GOAL:" + s+":"+decoding_tree[s] end current_index + end return decoded end def create_encoding_tree string tree ount_frequency string s 1"; count ; tree.keys.sort {|a,b| tree[a]<