From: James Gray Date: 2007-12-10T23:35:06+09:00 Subject: Fwd: Please Forward: Ruby Quiz Submission --Apple-Mail-1--53308556 Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes Content-Transfer-Encoding: 7bit Begin forwarded message: > From: "Robert Horvick" > Date: December 9, 2007 11:15:32 PM CST > To: submission@rubyquiz.com > Cc: "Robert Horvick" > Subject: Please Forward: Ruby Quiz Submission > > I'm only a few months into learning Ruby but what the heck :) > > It attempts to print all possible loops of the same form (start from > left, loop down and to the left, finish upwards). For example > "mississippi" finds three loops. > > $ ruby 149.rb mississippi > i > p > p > i > miss > si > > i > p > p > mis > ss > si > > missis > ps > pi > > --Apple-Mail-1--53308556 Content-Type: multipart/mixed; boundary=Apple-Mail-2--53308556 --Apple-Mail-2--53308556 Content-Type: text/html; charset=US-ASCII Content-Transfer-Encoding: quoted-printable
Begin forwarded = message:

From: "Robert Horvick" <robert.horvick@gmail.com><= /font>
Date: December 9, 2007 11:15:32 PM = CST
Cc: "Robert Horvick" <robert.horvick@gmail.com><= /font>
Subject: Please Forward: Ruby Quiz = Submission
I'm only a few months into learning Ruby but what the = heck :)

It attempts to print all possible loops of the same form = (start from left, loop down and to the left, finish upwards).  For = example "mississippi" finds three loops.

$ = ruby 149.rb mississippi
  i
  p
=   p
  i
miss
  si

 i
 p
 p
mis
 ss
 si

missis
    ps
    pi


=
= --Apple-Mail-2--53308556 Content-Disposition: attachment; filename=149.rb Content-Type: text/x-ruby-script; x-unix-mode=0666; name="149.rb" Content-Transfer-Encoding: quoted-printable # Ruby Quiz 149=0D # Robert Horvick (robert dot horvick at gmail dot com)=0D # http://www.ghostonthird.com=0D =0D class Array=0D # [1, 3, 9] =3D> [[1,1] [1,3] [1,9] [3,3] [3,9] [9,9]]=0D def group2=0D ret =3D []=0D each_index { |i| slice(i, length).each { |x| ret << [at(i), x] } = }=0D ret=0D end=0D end=0D =0D =0D class WordLoop =0D def initialize(word)=0D @word =3D word.downcase.scan(/./m)=0D end=0D =0D def print =0D # no loops for strings less than 5 chars long=0D if @word.length > 4=0D h =3D {}=0D =0D # build the char index groups=0D @word.each_index { |i| c =3D @word[i]=0D if h[c].nil?: h[c] =3D Array.new end=0D h[c] << i=0D }=0D =0D loops =3D []=0D # find each matching pair that is 4 or more long and even = length=0D h.each { |k,v| loops +=3D v.group2.delete_if {|a| =0D a[1] - a[0] < 4 || (a[1] - a[0] & 1 =3D=3D 1) } =0D }=0D =0D # print the graphs =0D if !loops.empty? then = =0D loops.each { |a|=0D # print topper=0D (@word.length-1).downto(a[1]+1) { |i| =0D puts " " * (a[0]) + @word[i] =0D }=0D =0D #print leader, the loop and the next=0D puts @word[0, a[0]+2].join=0D =0D # print the legs=0D leg_start =3D a[0]+2=0D leg_stop =3D a[1]-1=0D =0D while leg_start < leg_stop=0D puts " " * a[0] + @word[leg_stop] + = @word[leg_start]=0D leg_stop -=3D 1=0D leg_start +=3D 1=0D end=0D =0D puts ""=0D }=0D else=0D puts "No loop."=0D end=0D else=0D puts "No loop."=0D end=0D end =0D end=0D =0D ARGV.each { |arg| WordLoop.new(arg).print }=0D =0D --Apple-Mail-2--53308556 Content-Type: text/html; charset=US-ASCII Content-Transfer-Encoding: 7bit

--Apple-Mail-2--53308556-- --Apple-Mail-1--53308556--