Archive

Archive for the ‘Programming’ Category

Javabat Solutions

March 7th, 2010 Robert Kania No comments

Warmup-1

Sleep In: Show ▼

Monkey Trouble: Show ▼

Warmup-2

String Times: Show ▼

Front Times: Show ▼

String-1

Hello Name: Show ▼

Make ABBA: Show ▼

Make Tags: Show ▼

Make Out Word: Show ▼

Extra End: Show ▼

First Two: Show ▼

First Half: Show ▼

Without End: Show ▼

Combo String: Show ▼

Categories: Programming Tags:

Hack this Site Programming Problem 2 Solution

February 9th, 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: , ,

PHP Code to Include Content in Layout

December 13th, 2009 Robert Kania No comments

When I didn’t know PHP I spent a lot of time trying to find the coveted script that would allow me to include content in a page by including a page so that I could change the layout and it would remain the same for all pages, only the content would change. I of course didn’t want to use an iframe because these scroll and look hideous. The solution I found, but did not understand for several months later. When a bit later I lost this code I realized it was time for me to learn PHP. The code I wished to know was the code that allows you to include pages with content like http://example.com/index.php?page=home.php. The layout would be in the file index.php but the content for the homepage would be at home.php. The code to do this would be:

<?php if($_GET['page'] == null) { include “default.php”; } else { include $_GET['page']; } ?>

The explaination of this is:

$_GET['page']
is the PHP way to get a parameter from a url.  In PHP all the page parameters are stored after the ? in the url.  For example in this page index.php?page=anything.php the only variable is page and in this case page is equal to “anything.php”.  The $_GET['page'] gets the value of page.  If you wish to add more variables to the url you simply add an & in between the variables.  For example “index.php?page=anything.php&loggedin=false” Thus $_GET['loggedin'] would result in false.   Next time you do a go0ogle search check the url and you can see these variables.

include “default.php”
The first include will be shown if the page variable is null.  In this case it will show the default page you assign it.

include $_GET['page']
This will show the page of whatever follows the page variable in the url.

For an example of this visit my homepage http://rkania.com/index.php?view=home.  My site has customized code and mod rewrite so it won’t work exactly the same.

Categories: Programming Tags: ,

Have you ever?

December 9th, 2009 Robert Kania No comments
usage

The user statistics of my app.

Have you ever used Facebook? You have, have you? Well then I built a great application for you. The Have You Ever Facebook application. I built this Facebook application last year in a night, but never blogged about it. It was down for a few months while I upgraded my webhost, but now it is back up and slightly improved. I currently have over 3000 current users and approximately 250 monthly active users which has increased from about 150 before I upgraded it last week. This application allows you to ask your friends a question and have them respond yes or no to the question. I am trying to improve this application so that it can do more things, but I will to sort out the source code. In order to create this application I had to use the Facebook Develper’s PHP api. I learned a lot from this project.  I got the idea to create this application from the game we had to play at college orientation.  We stood in a circle with some sort of marker where we stood.  Then someone in the center calls out something that they have done or are wearing etc and everyone who has also has to move to a new spot.

To visit my application go to: http://apps.facebook.com/have_you_ever/

haveyouever

My Application

Categories: Programming, Web/Internet, websites Tags:

Fun in Computer Science….

April 13th, 2009 Robert Kania No comments

I’m taking a Problem Solving in Computer Science class this semester and one of the problems was:

How could a (normal, human) baby (accidently!) fall out of a twenty-story building onto the ground and live?

One of our solutions?  Drop the baby onto wet cement >_> What?!  It will survive (at least for a short time).  Fine what other suggestions do you have?


Categories: Programming Tags: