Forum: Ruby Mexican Blanket (#127)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2007-06-08 14:48
(Received via mailing list)
The three rules of Ruby Quiz:

1.  Please do not post any solutions or spoiler discussion for this quiz
until
48 hours have passed from the time on this message.

2.  Support Ruby Quiz by submitting ideas as often as you can:

http://www.rubyquiz.com/

3.  Enjoy!

Suggestion:  A [QUIZ] in the subject of emails about the problem helps
everyone
on Ruby Talk follow the discussion.  Please reply to the original quiz
message,
if you can.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

by Scott Prager

I'm looking at this blanket my dad got in Mexico, and I'm noticing that
it has
an interesting gradient. There are two colors (c1 and c2) and the
outside c1 is
5 stitches wide, then a 1 stitch c2, 4 stitches c1, 2 stitches c2... It
looks
something like this:

  RRRRRBRRRRBBRRRBBBRRBBBBRBBB
  RRRRBRRRRBBRRRBBBRRBBBBRBBBB
  RRRBRRRRBBRRRBBBRRBBBBRBBBBB
  RRBRRRRBBRRRBBBRRBBBBRBBBBBY
  RBRRRRBBRRRBBBRRBBBBRBBBBBYB
  BRRRRBBRRRBBBRRBBBBRBBBBBYBB
  RRRRBBRRRBBBRRBBBBRBBBBBYBBB
  RRRBBRRRBBBRRBBBBRBBBBBYBBBB
  RRBBRRRBBBRRBBBBRBBBBBYBBBBY
  RBBRRRBBBRRBBBBRBBBBBYBBBBYY
  BBRRRBBBRRBBBBRBBBBBYBBBBYYB
  BRRRBBBRRBBBBRBBBBBYBBBBYYBB
  RRRBBBRRBBBBRBBBBBYBBBBYYBBB
  RRBBBRRBBBBRBBBBBYBBBBYYBBBY
  RBBBRRBBBBRBBBBBYBBBBYYBBBYY
  BBBRRBBBBRBBBBBYBBBBYYBBBYYY
  BBRRBBBBRBBBBBYBBBBYYBBBYYYB
  BRRBBBBRBBBBBYBBBBYYBBBYYYBB
  RRBBBBRBBBBBYBBBBYYBBBYYYBBY
  RBBBBRBBBBBYBBBBYYBBBYYYBBYY
  BBBBRBBBBBYBBBBYYBBBYYYBBYYY
  BBBRBBBBBYBBBBYYBBBYYYBBYYYY
  BBRBBBBBYBBBBYYBBBYYYBBYYYYB
  BRBBBBBYBBBBYYBBBYYYBBYYYYBY
  RBBBBBYBBBBYYBBBYYYBBYYYYBYY
  BBBBBYBBBBYYBBBYYYBBYYYYBYYY
  BBBBYBBBBYYBBBYYYBBYYYYBYYYY
  BBBYBBBBYYBBBYYYBBYYYYBYYYYY

It goes on and on like that and when one color ends, another begins.
When two
sets of gradients meet, there is a thick separator. Two gradients don't
always
meet at the same color, but their opposite borders are usually the same.
(ex:
black-to-yellow |separator| white-to-black) There is also the occasional
gradient of thick bars of equal length; ex: |green|-|yellow|-|red|. It
looks
like this:

  http://www1.istockphoto.com/file_thumbview_approve...

So the challenge is to make a pattern generator to generate the patterns
as seen
in the ASCI and picture. It should, or rather NEEDS to, contain the
Mexican flag
as it is respectful to the culture where this challenge comes from. For
extra
credit, try to put them on a blanket of 200x100 pexels. And if you
really need
extra credit to graduate, make the patterns symmetrical, however make
the colors
in the patterns NOT symmetrical (ex: WWRW middle OBOO).
912c61d9da47754de7039f4271334a9f?d=identicon&s=25 MenTaLguY (Guest)
on 2007-06-08 18:53
(Received via mailing list)
On Fri, 8 Jun 2007 21:46:56 +0900, Ruby Quiz <james@grayproductions.net>
wrote:
> It should, or rather NEEDS to, contain the Mexican flag as it is respectful to
> the culture where this challenge comes from.

By this, you mean the colors of the Mexican flag?  The Mexican flag
proper also includes a rather complex coat of arms in the center, which
seems a little ambitious...

-mental
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2007-06-08 18:57
(Received via mailing list)
On Jun 8, 2007, at 11:52 AM, MenTaLguY wrote:

> On Fri, 8 Jun 2007 21:46:56 +0900, Ruby Quiz
> <james@grayproductions.net> wrote:
>> It should, or rather NEEDS to, contain the Mexican flag as it is
>> respectful to
>> the culture where this challenge comes from.
>
> By this, you mean the colors of the Mexican flag?  The Mexican flag
> proper also includes a rather complex coat of arms in the center,
> which seems a little ambitious...

Yes.  I'm pretty sure the author was referring to the colors.

James Edward Gray II
E8a419959139f3f505b49bb95f7e7afe?d=identicon&s=25 Joshua Ballanco (jballanc)
on 2007-06-08 19:19
James Gray wrote:
> On Jun 8, 2007, at 11:52 AM, MenTaLguY wrote:
>
>> On Fri, 8 Jun 2007 21:46:56 +0900, Ruby Quiz
>> <james@grayproductions.net> wrote:
>>> It should, or rather NEEDS to, contain the Mexican flag as it is
>>> respectful to
>>> the culture where this challenge comes from.
>>
>> By this, you mean the colors of the Mexican flag?  The Mexican flag
>> proper also includes a rather complex coat of arms in the center,
>> which seems a little ambitious...
>
> Yes.  I'm pretty sure the author was referring to the colors.
>
> James Edward Gray II

Oh, thank God! Now I might actually attempt this one... :-D
2f4d4f9c35ea851bffb9a9cc2e086365?d=identicon&s=25 Harry Kakueki (Guest)
on 2007-06-09 15:13
(Received via mailing list)
On 6/8/07, Ruby Quiz <james@grayproductions.net> wrote:
> So the challenge is to make a pattern generator to generate the patterns as seen
> in the ASCI and picture. It should, or rather NEEDS to, contain the Mexican flag
> as it is respectful to the culture where this challenge comes from. For extra
> credit, try to put them on a blanket of 200x100 pexels. And if you really need
> extra credit to graduate, make the patterns symmetrical, however make the colors
> in the patterns NOT symmetrical (ex: WWRW middle OBOO).
>
>
I didn't quite understand this part.
Is this suitable output for 35x70 ?

GGGGGWGGGGWWGGGWWWGGWWWWGWWWWWRWWWWRRWWWRRRWWRRRRWRRRRRBRRRRBBRRRBBBRR
GGGGWGGGGWWGGGWWWGGWWWWGWWWWWRWWWWRRWWWRRRWWRRRRWRRRRRBRRRRBBRRRBBBRRB
GGGWGGGGWWGGGWWWGGWWWWGWWWWWRWW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RRRRBRRRRBBRRRBBBRRBBBBRBBBBBYBBBBYYBBBYYYBBYYYYBY
WWWRRWWWRRRWWRRRRWRRRRRBRRRRBBRRRBBBRRBBBBRBBBBBYBBBBYYBBBYYYBBYYYYBYY
WWRRWWWRRRWWRRRRWRRRRRBRRRRBBRRRBBBRRBBBBRBBBBBYBBBBYYBBBYYYBBYYYYBYYY
WRRWWWRRRWWRRRRWRRRRRBRRRRBBRRRBBBRRBBBBRBBBBBYBBBBYYBBBYYYBBYYYYBYYYY


I did not want to post 200 lines here but I made a similar (sloppy and
temporary) HTML file of 200x100 in an attempt to make it easier to
understand the color patterns. Is this an acceptable pattern?
http://www.kakueki.com/ruby/q127.html

Or do I need to add some separators and if so, where? I did not get that
part.


Harry
--

A Look into Japanese Ruby List in English
http://www.kakueki.com/
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2007-06-09 18:15
(Received via mailing list)
On Jun 9, 2007, at 8:12 AM, Harry Kakueki wrote:

>> gradient of thick bars of equal length; ex: |green|-|yellow|-|
>> from. For extra
> GGGGGWGGGGWWGGGWWWGGWWWWGWWWWWRWWWWRRWWWRRRWWRRRRWRRRRRBRRRRBBRRRBBBRR
> WGGGWWWGGWWWWGWWWWWRWWWWRRWWWRRRWWRRRRWRRRRRBRRRRBBRRRBBBRRBBBBRBBBBBY
> WWGWWWWWRWWWWRRWWWRRRWWRRRRWRRRRRBRRRRBBRRRBBBRRBBBBRBBBBBYBBBBYYBBBYY
> WWRRWWWRRRWWRRRRWRRRRRBRRRRBBRRRBBBRRBBBBRBBBBBYBBBBYYBBBYYYBBYYYYBYYY
> WRRWWWRRRWWRRRRWRRRRRBRRRRBBRRRBBBRRBBBBRBBBBBYBBBBYYBBBYYYBBYYYYBYYYY
>
>
> I did not want to post 200 lines here but I made a similar (sloppy and
> temporary) HTML file of 200x100 in an attempt to make it easier to
> understand the color patterns. Is this an acceptable pattern?
> http://www.kakueki.com/ruby/q127.html

They both look fine to me.  Nice work.

James Edward Gray II
2f4d4f9c35ea851bffb9a9cc2e086365?d=identicon&s=25 Harry Kakueki (Guest)
on 2007-06-10 15:01
(Received via mailing list)
On 6/8/07, Ruby Quiz <james@grayproductions.net> wrote:
>
>
> So the challenge is to make a pattern generator to generate the patterns as seen
> in the ASCI and picture.
>
>

# Here is my solution.
# I built a string to use for unpacking.
# Then I used the string to unpack.
# It should work for a blanket of any size if the
# 'colors' string is long enough.


# Code Start
colors = "GWRBYRGRRGRYBRWG"
unp = "aXaXaXaXaa"
  (1...colors.length).each do
    (1..4).each {|y| unp<<"X"<<"Xa"*(5-y)<<"a"<<"Xa"*y}
  unp << "a"
  end
row = colors.unpack(unp)
  35.times do
  puts row[0..69].join
  row.shift
  end


# Harry

--

A Look into Japanese Ruby List in English
http://www.kakueki.com/
B63d3390f6285b1af0f1662687911a95?d=identicon&s=25 Jesse Merriman (Guest)
on 2007-06-10 16:29
(Received via mailing list)
My solution builds a color string long enough to run across the top of
the
blanket, turn, and run down the right side. This is shifted left for
every
row. You can visualize the blanket as being a rectangular window on the
parallelogram that is created, like this for 10x5:

    [--------]
    xxxxxxxxxxxxxx
   xxxxxxxxxxxxxx
  xxxxxxxxxxxxxx
 xxxxxxxxxxxxxx
xxxxxxxxxxxxxx
    [--------]

Building the edge string is done mainly by my Blanket.edge method, which
repeatedly chooses a random color to gradient with the previous color.
Blanket.symmetrical_edge can create pattern-and-color symmetrical edges
by creating a half-length edge and flipping it, or pattern-but-not-color
symmetrical edges by doing this and mapping the colors to random colors.

I was able to re-use some of my code from Quiz 125 (fractals) to create
images with RMagick.

Examples:

$ ./mexican_blanket.rb 60 15
OOOOOOWOOOOOWWOOOOWWWOOOWWWWOOWWWWWOWWWWWWWRWWWWWWRRWWWWWRRR
OOOOOWOOOOOWWOOOOWWWOOOWWWWOOWWWWWOWWWWWWWRWWWWWWRRWWWWWRRRW
OOOOWOOOOOWWOOOOWWWOOOWWWWOOWWWWWOWWWWWWWRWWWWWWRRWWWWWRRRWW
OOOWOOOOOWWOOOOWWWOOOWWWWOOWWWWWOWWWWWWWRWWWWWWRRWWWWWRRRWWW
OOWOOOOOWWOOOOWWWOOOWWWWOOWWWWWOWWWWWWWRWWWWWWRRWWWWWRRRWWWW
OWOOOOOWWOOOOWWWOOOWWWWOOWWWWWOWWWWWWWRWWWWWWRRWWWWWRRRWWWWR
WOOOOOWWOOOOWWWOOOWWWWOOWWWWWOWWWWWWWRWWWWWWRRWWWWWRRRWWWWRR
OOOOOWWOOOOWWWOOOWWWWOOWWWWWOWWWWWWWRWWWWWWRRWWWWWRRRWWWWRRR
OOOOWWOOOOWWWOOOWWWWOOWWWWWOWWWWWWWRWWWWWWRRWWWWWRRRWWWWRRRR
OOOWWOOOOWWWOOOWWWWOOWWWWWOWWWWWWWRWWWWWWRRWWWWWRRRWWWWRRRRW
OOWWOOOOWWWOOOWWWWOOWWWWWOWWWWWWWRWWWWWWRRWWWWWRRRWWWWRRRRWW
OWWOOOOWWWOOOWWWWOOWWWWWOWWWWWWWRWWWWWWRRWWWWWRRRWWWWRRRRWWW
WWOOOOWWWOOOWWWWOOWWWWWOWWWWWWWRWWWWWWRRWWWWWRRRWWWWRRRRWWWR
WOOOOWWWOOOWWWWOOWWWWWOWWWWWWWRWWWWWWRRWWWWWRRRWWWWRRRRWWWRR
OOOOWWWOOOWWWWOOWWWWWOWWWWWWWRWWWWWWRRWWWWWRRRWWWWRRRRWWWRRR

$ # Blanket displayed on screen:
$ ./mexican_blanket.rb 300 400 --format display

$ ./mexican_blanket.rb 20 5 --symmetric
GGGGGGGGOGGGYYYRYYYY
GGGGGGGOGGGYYYRYYYYY
GGGGGGOGGGYYYRYYYYYY
GGGGGOGGGYYYRYYYYYYY
GGGGOGGGYYYRYYYYYYYY

$ ./mexican_blanket.rb 20 5 --color-symmetric
OOOOOROOOORRRROOOORO
OOOOROOOORRRROOOOROO
OOOROOOORRRROOOOROOO
OOROOOORRRROOOOROOOO
OROOOORRRROOOOROOOOO

$ ./mexican_blanket.rb 400 600 --format png
Wrote blanket to blanket_400x600.png

An example of that last one can be seen here:
http://www.jessemerriman.com/images/ruby_quiz/127_...

The full usage statement is at the top of mexican_blanket.rb

If you want to change the colors or stripe widths, its hard-coded at the
top
of the Blanket class, and for graphics at the top of BlanketDraw.
2f4d4f9c35ea851bffb9a9cc2e086365?d=identicon&s=25 Harry Kakueki (Guest)
on 2007-06-10 16:33
(Received via mailing list)
On 6/8/07, Ruby Quiz <james@grayproductions.net> wrote:
>
> So the challenge is to make a pattern generator to generate the patterns as seen
> in the ASCI and picture. It should, or rather NEEDS to, contain the Mexican flag
>
>

# My HTML version prints a blanket in color.
# It is quick and dirty HTML that could certainly
# be improved but it is viewable.
# This makes a 200x100 blanket.
# It looks like this http://www.kakueki.com/ruby/q127.html

# Code Start
outs = File.new("./q127.html","w")
colors = "GWRBYRGRRGRYBRWG"
ahash = {}
ahash.store("G","<font color=\"#00ff00\">"+"o" + "</font>")
ahash.store("W","<font color=\"#ffffff\">"+"o" + "</font>")
ahash.store("R","<font color=\"#ff0000\">"+"o" + "</font>")
ahash.store("B","<font color=\"#0000ff\">"+"o" + "</font>")
ahash.store("Y","<font color=\"#ffff00\">"+"o" + "</font>")

unp = "aXaXaXaXaa"
  (1...colors.length).each do
    (1..4).each {|y| unp<<"X"<<"Xa"*(5-y)<<"a"<<"Xa"*y}
  unp << "a"
  end
row = colors.unpack(unp)
row.map! {|x| ahash[x] }
outs.puts "<html><body bgcolor=\"#dddddd\">"

  200.times do
  outs.print row[0..99].join
  outs.print "<br>\n"
  row.shift
  end

outs.puts "</body></html>"
outs.close


# Harry


--

A Look into Japanese Ruby List in English
http://www.kakueki.com/
0cb023654f46a6bd171405af6419baf6?d=identicon&s=25 darren kirby (Guest)
on 2007-06-10 18:47
(Received via mailing list)
I have been meaning to have a closer look at RMagick for a while, so
This quiz
seemed a good opportunity to do so.The actual pattern generator came
together
surprisingly quickly, and it was a breeze to draw with RMagick, so I
spent
most of my time working on the 'random bits' to give the blankets a bit
of
personality.

My script is fairly simple, it takes as argument first, Xpx and Ypx, and
then
any number of colours. If you want to use only a few colours but still
have
some changes you can specify them multiple times, ie: "yellow red green
yellow red green yellow red green" etc. Otherwise the pattern is just
extended the width of the image.

I posted a couple finished blankets on my site:
$ mexican_blanket.rb 200 400 green yellow red
created: http://badcomputer.org/images/blanket1.png (7K)

$ mexican_blanket.rb 400 800 400 800 blue green yellow white red orange
brown
green yellow blue
created: http://badcomputer.org/images/blanket2.png (25K)

And the code:
------------------------------------------------------------------
#!/usr/bin/ruby
#
# mexican_blanket.rb - Draw a png image which resembles
# a traditional mexican blanket
#
# Usage:
#  $ mexican_blanket.rb width height colour [colour]...
#
#  width and height are integers
#  colours may be specified by name eg: 'yellow'
#      or by hex code eg: '#FFFF00'
#

require 'RMagick'

COLUMNS = ARGV[0].to_i  # First arg is blanket width in px
ROWS = ARGV[1].to_i     # Second arg is blanket height in px
colours = ARGV[2..-1]   # Remaining args are colours

def mexican_blanket_magic(colours)
  ptr = -1
  pattern = []
  colours.size.times do
    # Next 10 lines build the gradient
    5.times { pattern[ptr += 1] = colours[0] }
    pattern[ptr+=1] = colours[1]

    4.times { pattern[ptr+=1] = colours[0] }
    2.times { pattern[ptr+=1] = colours[1] }

    3.times { pattern[ptr+=1] = colours[0] }
    3.times { pattern[ptr+=1] = colours[1] }

    2.times { pattern[ptr+=1] = colours[0] }
    4.times { pattern[ptr+=1] = colours[1] }

    pattern[ptr+=1] = colours[0]
    5.times { pattern[ptr+=1] = colours[1] }

    # This is our black stripe which divides the gradients
    n = rand(100)
    if n <= 20    # Aprox. 20% chance of 3px wide line
      3.times { pattern[ptr+=1] = "black" }
    elsif n >= 80 # Aprox. 20% chance of 7px wide line
      7.times { pattern[ptr+=1] = "black" }
    end           # Aprox. 60% chance of no line

    # Aprox. 20% chance we will get a diagonal bar
    # of random colour and size (<= 21px)
    if rand(100) < 20
      clr = colours[rand(colours.size)]
      (rand(20)+1).times { pattern[ptr+=1] = clr }
    end

    # Rotate through the colours in order
    cs = colours.shift
    colours.push(cs)
  end

  # Pad out the pattern to match image width
  ptr = -1
  while pattern.size < COLUMNS
    pattern << pattern[ptr+=1]
  end
  return pattern
end

# Create our pattern
pattern = mexican_blanket_magic(colours)

# Create the image canvas
blanket = Magick::Image.new(COLUMNS, ROWS)
stitch = Magick::Draw.new

# This is the plotter. Basically we just
# stamp out a row, left-shift the pattern
# and continue till all the rows are done.
yptr = 0
ROWS.times do
  xptr = 0
  COLUMNS.times do
    stitch.fill(pattern[xptr])  # Set colour
    stitch.point(xptr, yptr)    # Plot point
    xptr += 1
  end
  cc = pattern.shift
  pattern.push(cc)
  yptr += 1
end

# Write the drawing to the canvas,
# and the canvas to a file.

stitch.draw(blanket)
blanket.write("blanket.png")

# end mexican_blanket.rb
-------------------------------------------------------
BTW, sorry for all the 'ptr' damage, been writing a lot of C lately...

-d
A926d232eac59abbc32ac6b53858b4e1?d=identicon&s=25 Andreas Launila (Guest)
on 2007-06-10 18:53
(Received via mailing list)
Ruby Quiz wrote:
> So the challenge is to make a pattern generator to generate the patterns as seen
> in the ASCI and picture.
>

Idea: Each line constains a repeating pattern, and the lines themselves
are also repeating patterns. Therefore compute the supersequence of the
lines, then extract subsequences from that to retrieve the complete
pattern.



INTERVAL_LENGTH = 5 # The interval at which the color-pattern changes.
LINE_LENGTH = 100
ROW_COUNT = 200

# Mexican flag.
colors = %w{G W R}

# Cycle the colors if we need more. The relationship between
# supersequence length l, interval_length w, and number of colors c is
# l = (1 + (c-1) * w) * w <=> c = ceil((l - w) / w^2) + 1
length_needed = ROW_COUNT + LINE_LENGTH - 1
colors_needed = ((length_needed - INTERVAL_LENGTH).to_f /
  INTERVAL_LENGTH**2).ceil + 1
colors *= (colors_needed.to_f / colors.size).ceil

# Create a supersequence for the lines. Reverse the colors and slice
# from the end of the pattern to get the correct order.
color_pattern = colors.reverse.map{ |c| c * INTERVAL_LENGTH }.join
line_superseq = ''
INTERVAL_LENGTH.upto(color_pattern.size) do |i|
  line_superseq << color_pattern.slice(-i, INTERVAL_LENGTH)
end

# Sample the line-sequence a few times to produce the complete pattern.
ROW_COUNT.times do |i|
  puts line_superseq[i, LINE_LENGTH]
end


I didn't really understand what kind of symmetry was requested, but
replacing the colors with the following can be used to create the
pattern posted by Harry, the need for repetition of colors probably
means I misunderstod something though.


# Recreates the pattern posted by Harry.
colors = %w{G W R B Y R G R}
colors += colors.reverse
A926d232eac59abbc32ac6b53858b4e1?d=identicon&s=25 Andreas Launila (Guest)
on 2007-06-10 19:25
(Received via mailing list)
Andreas Launila wrote:
> # Cycle the colors if we need more. The relationship between
> # supersequence length l, interval_length w, and number of colors c is
> # l = (1 + (c-1) * w) * w <=> c = ceil((l - w) / w^2) + 1

And yes, that's wrong. There shouldn't be an equivalence there.
B63d3390f6285b1af0f1662687911a95?d=identicon&s=25 Jesse Merriman (Guest)
on 2007-06-10 20:21
(Received via mailing list)
On Sunday 10 June 2007 12:38, Tim Hunter wrote:
> Again, very cool!
>
> Tim

Right you are Tim. Attached is my new blanket_image.rb improved in the
way
you suggested (blanket_draw.rb can be discarded now). Quite a
difference:

$ time ./mexican_blanket.orig.rb 800 800 --format png
Wrote blanket to blanket_800x800.png

real    0m28.706s
user    0m25.757s
sys     0m0.454s

$ time ./mexican_blanket.rb 800 800 --format png
Wrote blanket to blanket_800x800.png

real    0m7.687s
user    0m7.452s
sys     0m0.070s

(BTW, your message appears to have been sent to just me, not the list.
Not
sure if this was intentional. If so, hope you don't mind me moving to
the
list.)
F2c730957ef0aec5616091b30fe88075?d=identicon&s=25 Ruben Medellin (chubas)
on 2007-06-10 22:01
(Received via mailing list)
On Jun 8, 7:46 am, Ruby Quiz <j...@grayproductions.net> wrote:
> The three rules of Ruby Quiz:

Here is my solution. It's rather long, but provides enough flexibility
to
reuse the code, add more colors, etc.
Displays in Unix terminals and PPM Images

http://pastie.caboo.se/69309

> It should, or rather NEEDS to, contain the Mexican flag
> as it is respectful to the culture where this challenge comes from.

As a mexican, I shall say the don't need the Mexican Flag colors, as
their origin comes from prehispanic times, way before those colors
appeared as a national symbol. Nevertheless, they are often included
nowadays.

Thanks for the quizes.

Ruben Medellin.
Aaa2b1f12b65d33422e8cdc48d70c0f9?d=identicon&s=25 Stephen Ball (Guest)
on 2007-06-10 23:54
(Received via mailing list)
I'm very much a newb, but I figure that the best way to learn Ruby
isn't sitting on the sidelines. So here's my solution (hacked, but it
works):

==== mexican_blanket.rb ====
class MexicanBlanket
 def initialize(colors, length, max_width)
   colors = colors
   @length = length
   max_width = max_width

   @current_row = 0

   # generate 'complete' line
   @complete_pattern = generate_complete_pattern(colors,max_width)
 end

 def first_row
   @complete_pattern[0...@length].to_s
 end

 def next_row
   length = @length + @current_row
   row = @complete_pattern[@current_row ... length]
   @current_row += 1
   row.to_s
 end

 def generate_complete_pattern(colors, max_width)
   first_two = mix_two_colors(colors[0..1],max_width)
   last_two = mix_two_colors(colors[1..2],max_width)
   last_two.shift;
   complete_pattern = first_two
   complete_pattern << last_two
   return complete_pattern.to_s.split(//)
 end

 def mix_two_colors(colors,max_width)
   first_color = colors[0]
   second_color = colors[1]
   first_width = max_width
   second_width = 1
   two_colors = []
   until (second_width > max_width)
     two_colors << first_color * first_width << second_color *
second_width
     first_width -= 1
     second_width += 1
   end
   return two_colors
 end

 attr_reader :complete_pattern # for testing
end
==== end mexican_blanket.rb ====

==== print_mexican_flag_blanket.rb ====
#!/usr/bin/env ruby -wKU

require "../lib/mexican_blanket"

colors = ["G","W","R"]
line_length = 28
color_max_width = 5
rows = 28

mexican_flag_blanket =
MexicanBlanket.new(colors,line_length,color_max_width)

rows.times { |n| puts mexican_flag_blanket.next_row }
==== end print_mexican_flag_blanket.rb ====
546405d327acc3598d5310ea6353b96a?d=identicon&s=25 Ivo Dancet (Guest)
on 2007-06-12 18:40
(Received via mailing list)
My solution..., I've spent an extra 10 minutes to put the code into a
class.

http://pastie.caboo.se/69806

#! /usr/bin/ruby
#
# usage: $> ruby quiz#127.rb <width> <rows> <color_width>
# without args it prints carpet of 100*200 with color_width 5.

class MexicanCarpet
   COLORS = %w(W Y G W B)

   def initialize(width, rows, color_width)
     @width, @rows, @color_width = width, rows, color_width
     total_width = @width * @rows

     @color_stack = ""

     # make one long line of colors
     (COLORS.length-1).times do |i|
       color1 = COLORS[i]
       color2 = COLORS[i+1]

       color_width.times do |i|
         @color_stack << color1 * (@color_width-i)
         @color_stack << color2 * (i + 1) if i < @color_width - 1
       end
     end

     while total_width > @color_stack.length do
       @color_stack_ = @color_stack_ ? @color_stack_.reverse :
@color_stack[5..-1].reverse
       @color_stack += (@color_stack[-2..-2] * @color_width) +
@color_stack_
     end

     @color_stack << COLORS.last * @color_width
     print_carpet
   end

   def print_carpet
     # print slices of the color_stack
     @rows.times do |row|
       puts @color_stack[row..row+@width]
     end
   end

end

width       = ARGV[0] ? ARGV[0].to_i : 100
rows        = ARGV[1] ? ARGV[1].to_i : 200
color_width = ARGV[2] ? ARGV[2].to_i : 5

MexicanCarpet.new( width, rows, color_width )
This topic is locked and can not be replied to.