Hack this Site Programming Problem 2 Solution

January 29th, 2010 Robert Kania No comments

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: , ,
Your Ad Here

Walk Right to Avoid Plight

January 14th, 2010 Robert Kania No comments

Have you ever had one of those awkward encounters when someone else is walking towards you, but neither of you know which way to move as to let the other pass by? usually it leads to several awkward seconds where both of you stare at each other and try to go right or left, but find the other person was trying to go that way too. Sometimes various back and forth movements follow as you each try to find a proper way around the other until one person decides to just stand still and let the other person walk around them. But this is still a waste of a few seconds of commuting time, even making you late, and it can really be frustrating.

My solution to this problem is Just Walk Right.  That way, if everyone follows the same pattern, no one would be forced to encounter another awkward standing moment.

This is also a page on Facebook.  Please fan.

Categories: Fun Tags:
Your Ad Here

Merry Christmas!!!

December 25th, 2009 Robert Kania No comments

Have a wonderful Christmas everyone!

Categories: Uncategorized Tags:
Your Ad Here

Who’s Your Daddy Farmville? Happy Farm

December 25th, 2009 Robert Kania No comments

Click to Enlarge

We all know that Facebook’s application Farmville has nearly 70 million monthly active users despite its simplicity.  However most people do not know that Farmville was actually the child of a Chinese game that is also on Facebook, but has a much lower user base (currently 3,372,366 monthly users), called Happy Farm.  It lacks in organization and some features that Farmville has, but has other redeeming traits including nicer graphics, different items, and different abilities, such as stealing.  Being Farmville’s predecessor, Happy Farm was rated in the Top 15 Most Influential Games of the Decade.

Farmville and Happy Farm Similarities


Neighbors


Just like in Farmville, Happy Farm revolves around you having neighbors.  You need neighbors to build a bigger farm so you can grow more crops just like in Farmville.

Happy Farm allows to interact with you neighbors in a few interesting ways:

There are four ways to interact with your neighbors: helping care for their farms, stealing some harvest, sending gifts and sending messages.

Gifts

Just like in Farmville you can send and receive gifts.  There are 5 different kinds of gifts: “free gifts, flower bouquet, toy and decoration, some other special items.”

Crops

What would a farm game be without crops? Even though both have crops, Happy Farm tends to revolve more around flowers where Farmville includes many types of crops.  Also, Happy Farm crops tend to have pests on them so another action in the game requires you to remove the pests.

Farmville and Happy Farm Differences


Exchange Center

Happy Farm includes a place where users can exchange items such as Dog Food and Toy Seed.  Sometimes special items can be found here as well.

Pet Store

The pet store is where you can buy different pets to help guard your property from thieves.  Thieves drop coins if caught by a pet.

Stealing

Unlike in Farmville, you can steal neighbors crops in Happy Farm but simply clicking on their crop when visiting their farm and choosing “Loot Harvest.”

Conclusion



Obviously both games are fun and have their redeeming qualities. I think Happy Farm looks nice and has some interesting features, but its user interface is fairly complicated.  I started using the application and I couldn’t figure out where things were.  While Farmville uses words such as Market and Gift on its icons, Happy Farm just uses pictures, making finding stuff complicated.  Also, happy Farm doesn’t have much room for expansion, but for the precursor to Farmville I think it is pretty fun.   Obviously the rest of the Facebook population agrees with Farmville being the better of the two.

Your Ad Here

Java Pong

December 24th, 2009 Robert Kania No comments

Pong by Bobby KaniaA few years ago I saw this article challenging people to make pong in any language and decided to design Pong in Java.  At that time I had taking Java in high school for about 5 months, but we were still no where near making applets.  I had already designed Chess and Checkers, and even though they were basic, they gave me the knowledge I needed to develop pong.  Of course this version of pong is really outdated because I have learned a lot since, but I have not felt the urge really to go back and change.  The AI is a bit strange since it follows the ball and randomly goes the opposite direction that it does not track the ball perfectly and I developed a new AI that finds where the ball will land and attempts to go that point, but I haven’t implemented it yet.  Writing this post makes me realize just how many unfinished projects I have.

Categories: Fun, websites Tags: , ,