challange 18

I have been wanting to write a webapp using ruby on rails and so I have sporadically been exploring the framework. The last time I did it became quite evident that my ruby skills are getting rusty. It seemed like a perfect excuse to get back to the Pytho- I mean, Ruby Challange.

Challenge 18 just shows two pictures of a goose side by side. The pictures are the same but one is brighter than the other. The title asks “can you tell the difference?” and the page source says
"-- it is more obvious that what you might think --"
And it is indeed obvious, don’t think too hard about it and change the url. After a few tries I found brightness.html which is an identical page with one difference, the page source says
"-- maybe consider deltas.gz --"

deltas.gz contains a textfile with two columns of of 18 two digit hex values. They start out the same but they quickly start to change. It seems pretty obvious that the differences need to be found; my suspicion was backed up by a google search of ‘deltas python’ with the difflib page being top ranked.

Looking around a bit I was unable to find a standard library in ruby for finding the differences in two files or strings the way gnu diff or pythons difflib does. This makes sense when you consider that most of the time ruby is running on a unix/linux system with perfectly capable binary tools for such work.

I did find several gems that provide similar functionality, but nothing that would work quit right. HTMLDiff was the closest, its output is formated in html which was a minor annoyance. However the bigger problem I had was that it frequently falsely classified lines as new and old rather than just marking the lines around them as changed. This resulted in unchanged lines being sent to both the new and the old categories rather than the unchanged category. After banging my head against various gems I finally gave up and used gnu diff. The output is three png files.

# Challenge 18
require 'zlib''deltas.gz') {|gzFile|
# Found lots of hexidecimal values in two columns

        column, output = ['', ''], ['', '', '']
        splitLines ="\n")
# Split the two columns apart row by row 
        splitLines.each { |line|
                if line[0, 53].rstrip != ""
                        column[0] += (line[0, 53].rstrip + "\n")
                if line[56, 53].rstrip != ""
                        column[1] += (line[56, 53].rstrip + "\n")
# Since I am using gnu diff I need to write them to files 
                column.length.times {|i|
                        f ="column" + i.to_s, "w")
        system("diff column0 column1 --old-line-format='- %L' --new-line-format='+ %L'  \
               --unchanged-line-format='%L' >output")
# Separate the output of diff into three strings        
        differences ="output", "r")"\n").each { |line|
                if line[0, 2] == "+ "
                        output[0] += (line.gsub("+ ", "") + ' ') 
                elsif line[0, 2] == "- " 
                        output[1] += (line.gsub("- ", "") + ' ') 
                        output[2] += (line + ' ')
# Finally, change the hex values into their equivalent characters and write them to file.
        output.length.times { |i|
            chrString = ''
                output[i].split().each {|hexVal|
                        chrString += hexVal.hex.chr
                        f ="deltas18_" + i.to_s + ".png", "w")

With a new url, login and password we arrive at challange 19: “Please!”


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s