Home > Programming > Hack this Site Programming Problem 2 Solution

Hack this Site Programming Problem 2 Solution

A few years ago I joined the site, Hack This Site in order to practice coding and see how much I knew.  Although the name is suggestive of illegal material, this site is in no way promoting hacking.  It simply is a site to practice programming and learning better techniques.  One of the challenges, Programming Problem 2 involves writing a program to take an image filled with black and white pixels, find the distance between the white pixels, which are ascii characters.  Convert these ascii characters then into Morse code, which then is translated into numbers and letters.  I spent some time initially looking for a language that could read an image in pixel by pixel and the one I settled on was Python.  This was the second program I had written in Python.

The exact instructions for the program were

The pixels in the above image are numbered 0..99 for the first row, 100..199 for the second row etc. White pixels represent ascii codes. The ascii code for a particular white pixel is equal to the offset from the last white pixel. For example, the first white pixel at location 65 would represent ascii code 65 (‘A’), the next at location 131 would represent ascii code (131 – 65) = 66 (‘B’) and so on.

The text contained in the image is the answer encoded in Morse, where “a test” would be encoded as “.- / – . … -”

You have 15 seconds time to send the solution.

Now of course it says you have 15 seconds which is true and I did do it in 15 seconds, but since this site uses Javascript to count, you can simply disabe javascript to have unlimited time by using the Firefox plugin NoScript.

For example, the image I used was:

which gave the following numbers:

45 46 46 32 45 46 46 32 45 46 46 46 46 32 45 45 45 32 46 45 32 45 46 45 32 46 46 46 32 46 46 46 46 32 46 45 45 45 45 32 46 45 32

which was translated in the following ascii characters

-..  -..  -….  —  .-  -.-  …  ….  .—-  .-

which produced the following output using Morse code to translate:

dd6oaksh1a

The code that I used to program this was:

import Image
def multipleReplace(text, morse_code):
    for key in morse_code:
        text = text.replace(key, morse_code[key])
    return text
morse_code = {' .- ':'a',' -... ':'b',' -.-. ':'c',' -.. ':'d',' . ':'e',
            ' ..-. ':'f',' --. ':'g',' .... ':'h',' .. ':'i',' .--- ':'j',
            ' -.- ':'k',' .-.. ':'l',' -- ':'m',' -. ':'n',' --- ':'o',
            ' .--. ':'p',' --.- ':'q',' .-. ':'r',' ... ':'s',' _ ':'t',
            ' ..- ':'u',' ...- ':'v',' .-- ':'w',' -..- ':'x',' -.-- ':'y',
            ' --.. ':'z',' ----- ':'0',' .---- ':'1',' ..--- ':'2',
            ' ...-- ':'3',' ....- ':'4',' ..... ':'5',' -.... ':'6',
            ' --... ':'7',' ---.. ':'8',' ----. ':'9'}
im = Image.open("C:\PNG.png")
size = im.size
width = size[0]
height = size[1]
L = list()
last = 0
#The loop transforms the image into a list
#If the pixel is white, return 0, otherwise return 1
for i in range (0,height):
    K = list()
    for j in range (0,width):
        checker = (int)(im.getpixel((j,i)))
        if(checker == 1):
            K.append(1)
        else:
            K.append(0)
    L.append(K)
ascii = list()
count = 0
for i in range (0,height):
    for j in range (0,width):
        if(L[i][j] == 1): #determines if the pixel is on
            char = chr(count-last) #converts the distance
            #between the pixels to ascii
            if(char == " "):
                char = "  "
            ascii.append(char)
            last = count
        count+=1
morseCode = " "
for i in range (0,len(ascii)-1):
    morseCode += ascii[i]
morseCode += " "
print multipleReplace(morseCode, morse_code) #replaces the morse code
#with letters and numbers
Categories: Programming Tags: , ,
  1. December 5th, 2011 at 08:36 | #1

    Gift baskets this particular holidays are something that everyone ought to be thinking about in the course of Holiday day plus this short article I’m going to offer you a handful of explanations why It is very essential that you as well think about buying gift baskets to your family members.

  2. December 5th, 2011 at 08:58 | #2

    Certainly very interesting post. In all cases I will try what is written here to pursue. greeting

  3. December 5th, 2011 at 09:21 | #3

    I truly wanted to jot down a small message to say thanks to you for the superb advice you are writing at this site. My long internet investigation has at the end of the day been paid with incredibly good ideas to write about with my relatives. I would assume that many of us website visitors actually are definitely blessed to dwell in a magnificent network with so many brilliant individuals with beneficial advice. I feel rather fortunate to have seen your website page and look forward to so many more fabulous minutes reading here. Thanks once more for everything.

  4. December 5th, 2011 at 09:26 | #4

    Excellent gift baskets found online

  5. December 5th, 2011 at 10:00 | #5

    You made a few good points there. I did a search on the subject and located most people will agree together with your blog. http://www.theiminsider.com/how-make-a-website

  6. December 5th, 2011 at 10:13 | #6

    Hi there, I found your site via Google while looking for a related topic, your website came up, it looks good. I have bookmarked it in my google bookmarks.

  7. December 5th, 2011 at 10:35 | #7

    Pourquoi ne pas noter les valeurs apportées la semaine dernieres par le ministere national? Au moins on aurait la capacité bavarder avec les vrais valeurs?

  8. December 5th, 2011 at 10:39 | #8

    There is evidently a bundle to identify about this. I suppose you made certain nice points in features also.

  9. December 5th, 2011 at 12:49 | #9

    Wow! This could be one particular of the most beneficial blogs We have ever arrive across on this subject. Actually Great. I’m also a specialist in this topic so I can understand your hard work.

  10. December 5th, 2011 at 13:30 | #10

    This page appears to get a good ammount of visitors. How do you advertise it? It offers a nice individual twist on things. I guess having something authentic or substantial to talk about is the most important thing. I would appreciate if you could visit my site http://www.buysellonline.in and give me some tips.

  11. December 5th, 2011 at 14:35 | #11

    Wow, wonderful blog layout! How long have you been blogging for? you make blogging look easy. The overall look of your website is excellent, as well as the content!

  12. December 5th, 2011 at 14:36 | #12

    I’m not that much of a online reader to be honest but your sites really nice, keep it up! I’ll go ahead and bookmark your site to come back down the road. Cheers

  13. December 5th, 2011 at 15:20 | #13

    Thank you for your thoughtful post!

  14. January 29th, 2012 at 08:13 | #14

    Thanks for writing this! It is info definitely worth sharing … check out the rest of the blog!

Comment pages
1 13 14 15 602
  1. November 21st, 2011 at 12:26 | #1
  2. November 22nd, 2011 at 18:16 | #2
  3. November 29th, 2011 at 02:08 | #3
  4. November 29th, 2011 at 22:39 | #4
  5. December 1st, 2011 at 12:00 | #5
  6. December 1st, 2011 at 12:24 | #6
  7. December 1st, 2011 at 12:26 | #7
  8. December 2nd, 2011 at 03:58 | #8
  9. December 3rd, 2011 at 14:29 | #9
  10. December 3rd, 2011 at 15:05 | #10
  11. December 4th, 2011 at 05:46 | #11
  12. December 4th, 2011 at 09:01 | #12
  13. December 4th, 2011 at 10:29 | #13
  14. December 4th, 2011 at 15:53 | #14
  15. December 4th, 2011 at 17:43 | #15
  16. December 5th, 2011 at 09:39 | #16
  17. December 5th, 2011 at 10:50 | #17
  18. December 5th, 2011 at 14:57 | #18