Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > [QUIZ] Mexican Blanket (#127)

Reply
Thread Tools

[QUIZ] Mexican Blanket (#127)

 
 
Ruby Quiz
Guest
Posts: n/a
 
      06-08-2007
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_thu...ove/2544046/2/

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).

 
Reply With Quote
 
 
 
 
MenTaLguY
Guest
Posts: n/a
 
      06-08-2007
On Fri, 8 Jun 2007 21:46:56 +0900, Ruby Quiz <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
 
James Edward Gray II
Guest
Posts: n/a
 
      06-08-2007
On Jun 8, 2007, at 11:52 AM, MenTaLguY wrote:

> On Fri, 8 Jun 2007 21:46:56 +0900, Ruby Quiz
> <(E-Mail Removed)> 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

 
Reply With Quote
 
Joshua Ballanco
Guest
Posts: n/a
 
      06-08-2007
James Gray wrote:
> On Jun 8, 2007, at 11:52 AM, MenTaLguY wrote:
>
>> On Fri, 8 Jun 2007 21:46:56 +0900, Ruby Quiz
>> <(E-Mail Removed)> 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...

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Harry Kakueki
Guest
Posts: n/a
 
      06-10-2007
On 6/8/07, Ruby Quiz <(E-Mail Removed)> 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/

 
Reply With Quote
 
Harry Kakueki
Guest
Posts: n/a
 
      06-10-2007
On 6/8/07, Ruby Quiz <(E-Mail Removed)> 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/

 
Reply With Quote
 
Andreas Launila
Guest
Posts: n/a
 
      06-10-2007
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


--
Andreas Launila

 
Reply With Quote
 
Andreas Launila
Guest
Posts: n/a
 
      06-10-2007
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.

--
Andreas Launila

 
Reply With Quote
 
Jesse Merriman
Guest
Posts: n/a
 
      06-10-2007
--Boundary-00=_yDEbGBYN+9Py7Ho
Content-Type: Multipart/Mixed;
boundary="Boundary-00=_yDEbGBYN+9Py7Ho"

--Boundary-00=_yDEbGBYN+9Py7Ho
Content-Type: text/plain;
charset="utf-8"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

On Sunday 10 June 2007 12:38, Tim Hunter wrote:
> Very cool! I'm always tickled when I see somebody using RMagick for
> something besides just making thumbnails.
>
> You're right, drawing each pixel would be slow. You could speed it up by
> creating each row as an array of Pixels, then using import_pixels or
> store_pixels to "draw" an entire row. Not only is doing things a row at
> a time faster, these methods are much, much faster than Draw#draw, which
> has to interpret the drawing primitives. Check the get_pixels.rb example
> in the RMagick distribution.
>
> 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.)


--
Jesse Merriman
http://www.velocityreviews.com/forums/(E-Mail Removed)
http://www.jessemerriman.com/

--Boundary-00=_yDEbGBYN+9Py7Ho
Content-Type: application/x-ruby;
name="blanket_image.rb"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
filename="blanket_image.rb"

# Ruby Quiz 127: Mexican Blanket
# blanket_image.rb

require 'blanket'
require 'RMagick'
include Magick

class BlanketImage < Magick::Image
# Colors from http://library.thinkquest.org/05aug/...mages/flag.jpg
# Not sure how accurate that is though..
StringToColor = { 'R' => '#fe0000', 'B' => '#0333a1', 'Y' => '#ffff49',
'O' => '#ea6520', 'G' => '#039e36', 'W' => '#ffffff' }

def initialize blanket
super blanket.width, blanket.height
draw_blanket blanket
self
end

def draw_blanket blanket
# Fill in the colors row-by-row (thanks Tim Hunter).
blanket.each_row_with_index do |row, y|
pixels = []
row.split(//).each do |color_char|
pixels << Pixel.from_color(StringToColor[color_char])
end
store_pixels 0, y, pixels.size, 1, pixels
end
end
end

--Boundary-00=_yDEbGBYN+9Py7Ho--
--Boundary-00=_yDEbGBYN+9Py7Ho--

 
Reply With Quote
 
CHubas
Guest
Posts: n/a
 
      06-10-2007
On Jun 8, 7:46 am, Ruby Quiz <(E-Mail Removed)> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Blanket font setting? RGK Python 1 09-18-2008 07:44 PM
[QUIZ] Mexican Blanket (#127) Problem Ari Brown Ruby 1 06-18-2007 01:01 AM
[SUMMARY] Mexican Blanket (#127) Ruby Quiz Ruby 0 06-14-2007 12:15 PM
[QUIZ][SOLUTION] Mexican Blanket (#127) Jesse Edelstein Ruby 1 06-10-2007 06:01 PM
blanket except clause -- best practice? George Young Python 3 10-29-2003 01:14 AM



Advertisments