From: Robert Dober Date: 2006-12-12T01:42:28+09:00 Subject: Re: [QUIZ] Tournament Matchups (#105) ------=_Part_59785_30970447.1165855345394 Content-Type: multipart/alternative; boundary="----=_Part_59786_23962453.1165855345394" ------=_Part_59786_23962453.1165855345394 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline Please find my solution hereafter, I somehow do not like it because it is unreadable, I wanted to show what ruby can do and that it can do it in different ways, i liked the Quiz, save the output part, which was a chellange but not really ruby specificly, well that is why my solution is so unreadable, a readable solution might not differ very much from Smalltalk or Java, structurally I mean ;) Thx for the Quiz. 665/167 > cat screen.rb class Screen def initialize size, &blk @lines = Array.new( size ){ [] } instance_eval &blk if blk end private def at c, pos_line, pos_col @lines[pos_line-1][pos_col-1] = c end def out stream=$stdout stream.puts self end def str_at s, pos_line, pos_col s.each_byte do |c| at c, pos_line, pos_col pos_col += 1 end end def str_align s, align, pos_line, pos_col str_at "%#{align}s" % s.to_s, pos_line, pos_col end def to_s @lines.map{ |line| line.map{ |c| c.nil? ? " " : c.chr}.join}.join("\n") end end 666/168 > cat sol.rb require 'screen' class Tournament def initialize n @n = n.to_i abort "illegal number #@n" unless @n > 0 @outputs = [] [*0..(@n - 1).to_s(2).length].inject([0]){ |pairs,n| @outputs.unshift pairs pairs.map{|p| [p, ( 1 << n ) * 2 - 1 - p]}.flatten } @outputs.map!{ |col| col.map!{|i| i < @n ? i + 1 : :bye} } end def print data = @outputs Screen.new data.first.length * 2 do data.each_with_index do |output, col| output.each_with_index do |player, row| main_row = 2**col + row*2**(col+1) str_align player, 4, main_row, 6*col+5 str_at "-"*5 << "+", main_row, 6*col+9 unless output.length == 1 if col > 1 then (2**(col-1)-1).times do |t| at ?|, main_row -1-t, 6*col+8 at ?|, main_row +1+t, 6*col+8 end end end end out end end end Tournament.new( ARGV.first || "14" ).print Robert -- "The real romance is out ahead and yet to come. The computer revolution hasn't started yet. Don't be misled by the enormous flow of money into bad defacto standards for unsophisticated buyers using poor adaptations of incomplete ideas." - Alan Kay ------=_Part_59786_23962453.1165855345394 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline
Please find my solution hereafter, I somehow do not like it because it = is unreadable, I wanted to show what ruby can do and that it can do it in d= ifferent ways, i liked the Quiz, save the output part, which was a chellang= e but not really ruby specificly, well that is why my solution is so unread= able, a readable solution might not differ very much from Smalltalk or Java= , structurally I mean ;)
Thx for the Quiz.

665/167 > = cat screen.rb

class Screen
      &n= bsp; def initialize size, &blk
      &= nbsp;         @lines =3D Array.new(= size ){ [] }
         &nbs= p;      instance_eval &blk if blk
        end
   &nb= sp;    private
       = def at c, pos_line, pos_col
       &= nbsp;        @lines[pos_line-1][pos_col-= 1] =3D c
        end
  &= nbsp;     def out stream=3D$stdout
   = ;             s= tream.puts self
        end
        def str_at s, pos_line, pos_= col
           &n= bsp;    s.each_byte do
     &nbs= p;            &= nbsp;     |c|
      &n= bsp;            = ;     at c, pos_line, pos_col
   &nbs= p;            &= nbsp;       pos_col +=3D 1
  &nb= sp;            = end
        end
   &nb= sp;    def str_align s, align, pos_line, pos_col
 &n= bsp;            = ;  str_at "%#{align}s" % s.to_s, pos_line, pos_col
 =        end
     &= nbsp;  def to_s
        &nb= sp;       @lines.map{ |line| line.map{ |c| c.nil? ? " " : c.chr }.join}.join("\n")
 &= nbsp;      end
end

666/168 > cat sol.= rb
require 'screen'
class Tournament
     = ;   def initialize n
       = ;         @n =3D n.to_i
 &n= bsp;            = ;  abort "illegal number #@n" unless @n > 0
            = ;    @outputs =3D []
      =           [*0..(@n - 1).to_s(2= ).length].inject([0]){
        &= nbsp;           &nbs= p;   |pairs,n|
        = ;            &n= bsp;   @outputs.unshift pairs
     &n= bsp;            = ;      pairs.map{|p| [p, ( 1 << n ) * 2 - 1 - p]}.flatten
    &nbs= p;           }
 &= nbsp;           &nbs= p;  @outputs.map!{ |col| col.map!{|i| i < @n ? i + 1 : :bye} }
&= nbsp;       end
    &n= bsp;   def print
       &nb= sp;        data =3D @outputs
 &n= bsp;            = ; =20 Screen.new data.first.length * 2 do
      =             &nb= sp;     data.each_with_index do
   &n= bsp;            = ;            &n= bsp;   |output, col|
       = ;            &n= bsp;            outp= ut.each_with_index do
        &n= bsp;            = ;            &n= bsp;      |player, row|
            = ;            &n= bsp;            = ;   main_row =3D 2**col + row*2**(col+1)
   &nb= sp;            =             &nb= sp;           str_align p= layer, 4, main_row, 6*col+5
       &n= bsp;            = ;            &n= bsp;       str_at "-"*5 << &q= uot;+", main_row, 6*col+9 unless
            = ;            &n= bsp;            = ;           output.length= =3D=3D 1
          &n= bsp;            = ;            &n= bsp;    if col > 1 then
     =             &nb= sp;            =             &nb= sp;     (2**(col-1)-1).times do
   &n= bsp;            = ;            &n= bsp;            = ;            &n= bsp;  |t|
            = ;            &n= bsp;            = ;            &n= bsp;      at ?|, main_row -1-t, 6*col+8
 &= nbsp;           &nbs= p;            &= nbsp;           &nbs= p;            &= nbsp;    at ?|, main_row +1+t, 6*col+8
   =             &nb= sp;            =             &nb= sp;       end
            = ;            &n= bsp;            = ;   end

        &n= bsp;            = ;           end

&n= bsp;            = ;           end
 =             &nb= sp;          out
 &nbs= p;            &= nbsp; end
        end
end

T= ournament.new ( ARGV.first || "14" ).print

Robert

--=
"The real romance is out ahead and yet to come. The computer revo= lution hasn't started yet. Don't be misled by the enormous flow of money in= to bad defacto standards for unsophisticated buyers using poor adaptations = of incomplete ideas."

- Alan Kay ------=_Part_59786_23962453.1165855345394-- ------=_Part_59785_30970447.1165855345394 Content-Type: application/x-ruby; name=sol.rb Content-Transfer-Encoding: base64 X-Attachment-Id: f_evl4zmqq Content-Disposition: attachment; filename="sol.rb" cmVxdWlyZSAnc2NyZWVuJwpjbGFzcyBUb3VybmFtZW50CglkZWYgaW5pdGlhbGl6ZSBuCgkJQG4g PSBuLnRvX2kKCQlhYm9ydCAiaWxsZWdhbCBudW1iZXIgI0BuIiB1bmxlc3MgQG4gPiAwCgkJQG91 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Type: application/x-ruby; name=screen.rb Content-Transfer-Encoding: base64 X-Attachment-Id: f_evl51bd8 Content-Disposition: attachment; filename="screen.rb" CmNsYXNzIFNjcmVlbgoJZGVmIGluaXRpYWxpemUgc2l6ZSwgJmJsawoJCUBsaW5lcyA9IEFycmF5 Lm5ldyggc2l6ZSApeyBbXSB9CgkJaW5zdGFuY2VfZXZhbCAmYmxrIGlmIGJsawoJZW5kCglwcml2 YXRlCglkZWYgYXQgYywgcG9zX2xpbmUsIHBvc19jb2wKCQlAbGluZXNbcG9zX2xpbmUtMV1bcG9z X2NvbC0xXSA9IGMKCWVuZAoJZGVmIG91dCBzdHJlYW09JHN0ZG91dAoJCXN0cmVhbS5wdXRzIHNl bGYKCWVuZAoJZGVmIHN0cl9hdCBzLCBwb3NfbGluZSwgcG9zX2NvbAoJCXMuZWFjaF9ieXRlIGRv CgkJCXxjfAoJCQlhdCBjLCBwb3NfbGluZSwgcG9zX2NvbAoJCQlwb3NfY29sICs9IDEKCQllbmQK CWVuZAoJZGVmIHN0cl9hbGlnbiBzLCBhbGlnbiwgcG9zX2xpbmUsIHBvc19jb2wKCQlzdHJfYXQg IiUje2FsaWdufXMiICUgcy50b19zLCBwb3NfbGluZSwgcG9zX2NvbAoJZW5kCglkZWYgdG9fcwoJ CUBsaW5lcy5tYXB7IHxsaW5lfCBsaW5lLm1hcHsgfGN8IGMubmlsPyA/ICIgIiA6IGMuY2hyIH0u am9pbn0uam9pbigiXG4iKQoJZW5kCmVuZAoK ------=_Part_59785_30970447.1165855345394--