--Apple-Mail-3--1057530766
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
	charset-ASCII;
	format竟wed

Begin forwarded message:

> From: Hirotsugu Asari <asari / mac.com>
> Date: May 13, 2007 6:58:20 PM CDT
> To: submission / rubyquiz.com
> Subject: Please Forward: Ruby Quiz Submission
>

--Apple-Mail-3--1057530766
Content-Transfer-Encoding: 7bit
Content-Type: text/x-ruby-script;
	x-unix-mode44;
	x-mac-creatorT784D74;
	nameffman.rb
Content-Disposition: attachment;
	filenameffman.rb

#! /usr/bin/env ruby

# Ruby Quiz #123
# Copyright 2007 Hirotsugu Asari

class String
  def huffman_encode
    a  elf.split(//)
    h  } # hash of character occurences
    a.each { |c|
      h[c]  [c].nil? ? 1 : h[c] + 1
    }
    # puts h.inspect
    a  .sort { |a,b|
      # sort the hash in descending order of the values
      b[1] <a[1]
    }
    a  .transpose[0]
    
    encoding
    # for encoding, we will always start with
    # 0, 10, 110, 1110, 11110, ...
    a.each_with_index { |o,i|
      encoding[o]  "1"*i).to_i(2) << 1
    }
    # .... and the last one is 111...111, which is obtained by
    # shifting 1111...1110 to the right by 1 bit.
    encoding[a[-1]]  ncoding[a[-1]] << -1
    
    @key  ncoding
    # puts @key.inspect
    
    # finally, map the binary representation of these numbers
    self.split(//).map { |c| encoding[c].to_s(2) }.
      # ...and concatenate
      inject("") { |s, i| s + i }
  end
  
  def huffman_key
    self.huffman_encode
    @key
  end

  def to_blocks_of_8( delim" )
    self.scan(%r{.{1,8}}).join(delim)
  end
  
end

--Apple-Mail-3--1057530766
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
	charset-ASCII;
	delspサ
	竟

セ
セ ネァ    」アイウョ
セ
セ ノ ァ     ョ  ヤ 「 「  
セ   「ア「     ャ     
セ   アァ        ョ  ィチチチチチツツツ   
セ チチチチチツツ     ョゥ
セ
セ ィチ    ャ ノ         
セ ョ  ヤョゥ
セ
セ ネョ


ュュチュヘュウュュアーオキオウーキカカュュ