From: Will Bailey Date: 2007-06-11T02:17:41+09:00 Subject: Fwd: [QUIZ][SOLUTION] Mexican Blanket (#127) ------=_Part_94842_4444572.1181495861798 Content-Type: multipart/alternative; boundary="----=_Part_94843_7766511.1181495861798" ------=_Part_94843_7766511.1181495861798 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline I noticed there are some line wraps if you cut and paste from the email so here is the source file. On 6/10/07, Will Bailey < will.bailey@gmail.com> wrote: > > Here is my solution for the Mexican Blanket Quiz. > It's also available here: > > http://code.rendale.com/svn/RubyQuiz/MexicanBlanket_127/Mexican_Blanket.html > < http://code.rendale.com/svn/RubyQuiz/MexicanBlanket> > > I was having a hard time seeing the pattern in the ascii so I added a > feature to convert the ascii string into an html image composed of colored > divs. > > The colors in the blanket are randomly selected from an array of possible > colors and gradients are alternately separated by Mexican flag bands and > black bands. The technique I use is to create a string with enough of the > > pattern to fill the entire blanket and then shift back by one unit per > row. > > Any feedback on how I can improve this code would be much appreciated. > > > ------------------------------------------------------------------------------------------------------------- > > #!/usr/local/bin/ruby > module RubyQuiz > > #creates the pattern > class Pattern > attr_reader :colors > attr_reader :value > > def initialize(length) > @length = length > @color_count = {:color1=>5, :color2=>1} > @colors = > Hash["0","black","1","red","2","orange","3","yellow","4","green","5","blue","6","indigo","7","violet","8","white","9","grey"] > > > @value="" > @flag_switch = true > generate > end > > def generate > pick_new_colors > (0..(@length.to_f/6).ceil).each do |i| > @value += (@color1.to_s * @color_count[:color1]) + (@color2.to_s* > @color_count[:color2]) > update_colors > end > end > > private > def update_colors > if @color_count[:color1] > 1 > @color_count[:color1]-=1 > @color_count[:color2]+=1 > else > @color_count[:color1],@color_count[:color2]=5,1 > @flag_switch ? add_flag : add_seperator > pick_new_colors > end > end > > def add_flag > @value += "01840" > @flag_switch = false > end > > def add_seperator > @value += "000" > @flag_switch = true > end > > def pick_new_colors > @color1 = (rand*9).ceil > @color2 = @color1 < 2 ? @color1 + 1 : @color1 - 1 > end > end > > # composes the pattern into a blanket > class Blanket > attr_reader :ascii > attr_accessor :scale > def initialize(width=75,height=100, scale=2) > @width, @height, @scale = width.to_i, height.to_i, scale.to_i > @ascii, @html = ["",""] > @pattern = Pattern.new(@width+@height) > weave > end > > private > def weave > 0.upto(@height) do |i| > @ascii += (@pattern.value[0 + i,@width])[0,@width] + "\n" > end > puts @ascii + "\n" > puts "open Mexican_Blanket.html in your interweb browser to see it > in full color" > html > end > > def html > @html = "\n\nMexican Blanket > Solution\n\n" > @html +="\n" > @html > > +="\n" > @html +="
\n" > @ ascii.split("\n").each do |line| > line.gsub (/(.)/) do |char| > @html += "
background:#{@pattern.colors[char]}\">
" > end > end > @html += "\n
\n
" > @html += "\n
\n#{@ascii}\n
\n" > @html += "\n" > to_file > end > > def to_file > output_file = File.open("Mexican_Blanket.html","w")do |f| > f.write(@html) > end > end > end > end > > # Blanket.new accepts height, width, and scale (used for html version) > if __FILE__ == $0 > blanket = RubyQuiz::Blanket.new(*ARGV) > end > > ------------------------------------------------------------------------------------------------------------- > > -- will bailey towers perrin 804.852.6758 -- will bailey towers perrin 804.852.6758 ------=_Part_94843_7766511.1181495861798 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline I noticed there are some line wraps if you cut and paste from the email so here is the source file.

On 6/10/07, Will Bailey < will.bailey@gmail.com> wrote:
Here is my solution for the Mexican Blanket Quiz.
It's also available here:
http://code.rendale.com/svn/RubyQuiz/MexicanBlanket_127/Mexican_Blanket.html < http://code.rendale.com/svn/RubyQuiz/MexicanBlanket>


I was having a hard time seeing the pattern in the ascii so I added a
feature to convert the ascii string into an html image composed of colored
divs.

The colors in the blanket are randomly selected from an array of possible
colors and gradients are alternately separated by Mexican flag bands and
black bands.  The technique I use is to create a string with enough of the
pattern to fill the entire blanket and then shift back by one unit per row.

Any feedback on how I can improve this code would be much appreciated.


-------------------------------------------------------------------------------------------------------------
#!/usr/local/bin/ruby
module RubyQuiz

  #creates the pattern
  class Pattern
    attr_reader :colors
    attr_reader :value

    def initialize(length)
      @length = length
      @color_count = {:color1=>5, :color2=>1}
      @colors =
Hash["0","black","1","red","2","orange","3","yellow","4","green","5","blue","6","indigo","7","violet","8","white","9","grey"]

      @value=""
      @flag_switch = true
      generate
    end

    def generate
      pick_new_colors
        (0..(@length.to_f/6).ceil).each do |i|
          @value += (@color1.to_s * @color_count[:color1]) + (@color2.to_s *
@color_count[:color2])
        update_colors
        end
    end

    private
      def update_colors
        if @color_count[:color1] > 1
          @color_count[:color1]-=1
          @color_count[:color2]+=1
        else
          @color_count[:color1],@color_count[:color2]=5,1
          @flag_switch ? add_flag : add_seperator
          pick_new_colors
        end
      end

      def add_flag
        @value += "01840"
        @flag_switch = false
      end

      def add_seperator
        @value += "000"
        @flag_switch = true
      end

      def pick_new_colors
        @color1 = (rand*9).ceil
        @color2 = @color1 < 2 ? @color1 + 1 : @color1 - 1
      end
  end

  # composes the pattern into a blanket
  class Blanket
    attr_reader :ascii
    attr_accessor :scale
    def initialize(width=75,height=100, scale=2)
      @width, @height, @scale = width.to_i, height.to_i, scale.to_i
      @ascii, @html = ["",""]
      @pattern = Pattern.new(@width+@height)
      weave
    end

    private
      def weave
        0.upto(@height) do |i|
          @ascii += (@pattern.value[0 + i,@width])[0,@width] + "\n"
        end
        puts @ascii + "\n"
        puts "open Mexican_Blanket.html in your interweb browser to see it
in full color"
        html
      end

      def html
        @html = "<html>\n<head>\n<title>Mexican Blanket
Solution</title>\n</head>\n"
        @html +="<body>\n"
        @html
+="<style>\nbody{background:#ccc;font-weight:bold;text-align:center}\ndiv.pixel{float:left;
width:#{@scale}px; height:#{@scale}px;}\n.wrapper{border:2px solid #fff;
width:#{@width*@scale}px; margin:auto;}\n</style>\n"
        @html +="<div class=\"wrapper\">\n"
        @ ascii.split("\n").each do |line|
          line.gsub (/(.)/) do |char|
            @html += "<div class=\"pixel\" style=\"
background:#{@pattern.colors[char]}\"></div>"
          end
        end
        @html += "\n<div style=\"clear:both\"></div>\n</div>"
        @html += "\n<div><pre>\n#{@ascii}\n</pre></div>\n"
        @html += "</body>\n</html>"
        to_file
      end

      def to_file
        output_file = File.open("Mexican_Blanket.html","w")do |f|
           f.write(@html)
        end
      end
  end
end

# Blanket.new accepts height, width, and scale (used for html version)
if __FILE__ == $0
  blanket = RubyQuiz::Blanket.new(*ARGV)
end

-------------------------------------------------------------------------------------------------------------



--
will bailey
towers perrin
804.852.6758



--
will bailey
towers perrin
804.852.6758 ------=_Part_94843_7766511.1181495861798-- ------=_Part_94842_4444572.1181495861798 Content-Type: application/x-ruby; name=MexicanBlanket.rb Content-Transfer-Encoding: base64 X-Attachment-Id: f_f2rqnfzc Content-Disposition: attachment; filename="MexicanBlanket.rb" IyEvdXNyL2xvY2FsL2Jpbi9ydWJ5Cm1vZHVsZSBSdWJ5UXVpegogIAogICNjcmVhdGVzIHRoZSBw YXR0ZXJuCiAgY2xhc3MgUGF0dGVybgogICAgYXR0cl9yZWFkZXIgOmNvbG9ycwogICAgYXR0cl9y ZWFkZXIgOnZhbHVlCiAgICAKICAgIGRlZiBpbml0aWFsaXplKGxlbmd0aCkKICAgICAgQGxlbmd0 aCA9IGxlbmd0aAogICAgICBAY29sb3JfY291bnQgPSB7OmNvbG9yMT0+NSwgOmNvbG9yMj0+MX0K ICAgICAgQGNvbG9ycyA9IEhhc2hbIjAiLCJibGFjayIsIjEiLCJyZWQiLCIyIiwib3JhbmdlIiwi MyIsInllbGxvdyIsIjQiLCJncmVlbiIsIjUiLCJibHVlIiwiNiIsImluZGlnbyIsIjciLCJ2aW9s ZXQiLCI4Iiwid2hpdGUiLCI5IiwiZ3JleSJdCiAgICAgIEB2YWx1ZT0iIgogICAgICBAZmxhZ19z d2l0Y2ggPSB0cnVlCiAgICAgIGdlbmVyYXRlCiAgICBlbmQKCiAgICBkZWYgZ2VuZXJhdGUKICAg ICAgcGlja19uZXdfY29sb3JzCiAgCSAgKDAuLihAbGVuZ3RoLnRvX2YvNikuY2VpbCkuZWFjaCBk byB8aXwKICAJICAgIEB2YWx1ZSArPSAoQGNvbG9yMS50b19zICogQGNvbG9yX2NvdW50Wzpjb2xv cjFdKSArIChAY29sb3IyLnRvX3MgKiBAY29sb3JfY291bnRbOmNvbG9yMl0pCiAgICAgICAgdXBk YXRlX2NvbG9ycyAgICAgIAogIAkgIGVuZCAgICAKICAgIGVuZAoKICAgIHByaXZhdGUKICAgICAg ZGVmIHVwZGF0ZV9jb2xvcnMKICAgICAgICBpZiBAY29sb3JfY291bnRbOmNvbG9yMV0gPiAxCiAg ICAgICAgICBAY29sb3JfY291bnRbOmNvbG9yMV0tPTEKICAgICAgICAgIEBjb2xvcl9jb3VudFs6 Y29sb3IyXSs9MSAgCiAgICAgICAgZWxzZQogICAgICAgICAgQGNvbG9yX2NvdW50Wzpjb2xvcjFd LEBjb2xvcl9jb3VudFs6Y29sb3IyXT01LDEKICAgICAgICAgIEBmbGFnX3N3aXRjaCA/IGFkZF9m bGFnIDogYWRkX3NlcGVyYXRvcgogICAgICAgICAgcGlja19uZXdfY29sb3JzCiAgICAgICAgZW5k ICAgIAogICAgICBlbmQKCiAgICAgIGRlZiBhZGRfZmxhZwogICAgICAgIEB2YWx1ZSArPSAiMDE4 NDAiCiAgICAgICAgQGZsYWdfc3dpdGNoID0gZmFsc2UKICAgICAgZW5kCiAgCiAgICAgIGRlZiBh ZGRfc2VwZXJhdG9yCiAgICAgICAgQHZhbHVlICs9ICIwMDAiCiAgICAgICAgQGZsYWdfc3dpdGNo ID0gdHJ1ZQogICAgICBlbmQKICAKICAgICAgZGVmIHBpY2tfbmV3X2NvbG9ycwogICAgICAgIEBj b2xvcjEgPSAocmFuZCo5KS5jZWlsCiAgICAgICAgQGNvbG9yMiA9IEBjb2xvcjEgPCAyID8gQGNv bG9yMSArIDEgOiBAY29sb3IxIC0gMQogICAgICBlbmQgIAogIGVuZAoKICAjIGNvbXBvc2VzIHRo ZSBwYXR0ZXJuIGludG8gYSBibGFua2V0CiAgY2xhc3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o6QmxhbmtldC5uZXcoKkFSR1YpCmVuZA== ------=_Part_94842_4444572.1181495861798--