CPSC 120 B




Lecture
MWF 10:50am - 11:50am
Lab
MWF 12:00pm - 1:00pm

Scotty Smith

Office
Trexler 365-B
Office Hours
Monday / Thursday
3:00pm - 5:00pm
Email
chssmithATroanoke.edu

< Back
Printable Copy

Lecture 23 - Binary and String Methods


As usual, create two directories for today's class. Create a directory called lecture23 under activities, and a directory called lab23 under labs.


Randomizer!

It's that time again. This one spans back before break! Including activities from lectures 20, 21, and 22.


In-Class Activity 1

In a file called string_operations, Write a function called slice. This function takes 3 parameters: A string, and two integer parameters. The first integer parameter specifies a starting index, and the second integer parameter specifies an ending index. Your function should return a "substring" of the string input parameter, which is a string of all characters in the range from start (inclusive) to end (exclusive).

Don't forget to define your test cases. Your program should execute your test cases automatically.

Challenge

The way your code is currently structured, it likely requires you to specify only positive values. So to remove just the last character of the string s, your would call slice(s, 0, len(s) - 1). However, that can get combersome, if your variable for s is somewhat long.

Rewrite your function so that you can specify negative values for start and end. You should simply need to pre-process negative inputs into their positive equivalents, and continue on as usual.


A Brief Binary Interlude

On Wednesday, we took a look at how numbers are represented in Binary, so our computers can actually understand and store them. However, we really focused on Integers. While that works for Integers (obviously) and Characters (remember the ord function?), that doesn't quite cut the mustard as far as floating point numbers. Let's take a brief look at how we can represent floating point numbers in Binary.


More Strings!

Since I'm not going to be here on Monday, you get two lectures for the price of one today! Let's examine some more things that the String class provides for us. Specifically:


In-Class Activity 2

1337 $P3@k

For those of you not aware, there is a dialect of English out on the web known as "Leet Speak." Leet Speak is simply a textual substitution of characters within some string, which makes the text (purposefully) harder to read, and should be such that only the "elite" of the Internet can really understand it.

Create a file called leet_speak.py in your lecture23 directory. You should write a function convert_to_leet that takes a single parameter, a string, and returns the Leet Speak equivalent of the input string. You should use the built in replace method to accomplish this, following the table below. Ignore the case of the original characters.

Original CharacterNew "Character"
a@
e3
i!
o0
u|_|
l1
m/\/\
r|2
s$
t7
w\/\/

Don't forget to define your test cases. Your program should execute your test cases automatically.

Challenge

Another common theme in leet speak is that characters have a randomly determined case. This causes wEIrD LoOkiNg words, which magnify the effect. In addition to the above changes, chose a random case for each non-translated letter in the string.


Lab Assignment 23

Pig Latin Translator

Pig Latin is a somewhat silly language parody that is actually entirely English based. It is a simple transposition of letters that gives the effect of a "foreign" sounding language. However, even Thomas Jefferson couldn't resist writing letters to his friends in Pig Latin. The rules for converting an English word to Pig Latin are incredibly easy:

For example, the word scram becomes am'scray, while the word immediate become immediateyay.

Your goal for this assignment is to write a function convert_to_pig_latin, in a file called pig_latin.py. This function should take a string of alphabetic characters, and returns a string of alphabetic characters, the Pig Latin equivalent of the input word.

Your program should gracefully handle capitalization of words. For example, Hello should become Ello'hay, as opposed to ello'Hay. It may be useful to take a look at the string methods Python provides, to reduce some of this overhead.

Challenge
Ofyay ourse'cay, ityay akes'may ittle'lay ense'say o'tay ite'wray ayay ogram'pray o'tay anslate'tray ayay ord'way o'tay ig'pay atin'lay ifyay ityay annot'cay anslate'tray ack'bay e'thay otheryay ay'way! Ite'wray ayay unction'fay onvert_from_pig_latin'cay at'thay akes'tay ayay ord'way alreadyyay inyay ig'pay atin'lay, andyay eturns'ray e'thay Englishyay equivalentyay.
Ake'may ure'say youyay est'tay is'thay unction'fay appropriatelyyay asyay ell'way!

Submission

When you have finished, create a tar file of your lab23 directory. To create a tar file, execute the following commands:

cd ~/cs120/labs
tar czvf lab23.tgz lab23/

To submit your activity, go to cseval.roanoke.edu. You should see an available assignment called Lab Assignment 23. Only one of your pair should submit your activity. Make sure both partners are listed in the header of your files.

Do not forget to email your partner today's files!


Last modified: Thu Oct 24 20:41:31 EDT 2013