Hacker Code?

Back to index

The first thing you see in the sample program down below is a variable called SUBS which is initialized with a long list of pairs of values. As the name SUBS implies this is a list of items that get substituted for each other. The part of the program which performs the actual substitution is the function called encode_message. Inspect the code below and get it up and running. We'll do a play-by-play analysis once you've had a chance to play with it a little.
#!/usr/bin/python SUBS = [['for','4'], ['to','2'], ['your','ur'],['be','B'],['you','U'], ['ate','8'], ['see','C'], ['okay','K'], ['later','L8R'], ['a','@'], ['e','3'], ['l','1'], ['o','*'], ['t','7'], ['i','!'], ['s','$'], ['and','@'],['okay','k']] def encode_message(): converted = message for s in SUBS: old = s[0] new = s[1] converted = converted.replace(old,new) return converted message = raw_input("GIVE ME INPUT NOW! ") converted_text = encode_message() print (message) print (converted_text)
The first line which is actually executed is the "message = raw_input" line. The next line actually calls the encode_message function. The substitution of items happens in the for-loop, which goes through each pair in SUBS and replaces any matching values. Once the conversion is complete, both the original message and the converted message are printed out.
ASSIGNMENT:

In the old days secretaries had to be good at dictation which required the use of shorthand. Nowadays this skill is not all that useful, largely because of the availability of recording devices. Be that as it may, shorthand is very easy to understand. Basically all you do is drop all vowels. So, for a list of vowels (a,e,i,o,u), a "" is substituted. Thus,

The sun was hot and the wind was dry.

becomes

Th sn ws ht nd th wnd ws dr

Alter the sample program so that it converts a message to shorthand. Delete all unnecessary lines of code. Do the actual conversion in a function called shorthand().