Introduction to Python:Session 3

From ThinkHausWiki
Jump to: navigation, search


Download the examples here


CamelCase is for camels, not programmers

Camel case (CamelCase) is a form of writing where the start of each word is capitalized, and the words are squashed together with no spaces. Some programmers use this as a convention when writing their code - variables, functions, class names (more on classes later), etc are often written in camel case. Often the very first letter is not capitalized. Some examples:


An alternative is to use underscores to break up words. Something like this:


Write a program that prompts the user for an input string in camel case. The program converts the string from camel case into the underscored equivalent. Hint: In the same way that you can check if a number is between two other numbers like this:

   if (x >= 0 and x < 10):
      print("Single digit number!")

You can do the same with characters. You can check if a given character is between other characters:

   if (ch >= 'x' and ch <= 'z'):
      print("That letter is the name of an axis in math!")

What's the frequency Kenneth?

Write a program that reads in a text file and counts the number of times each character occurs, and outputs the information sorted by the character. Note that characters include letters like "A", "B" and "C" but also includes digits, punctuation and even spaces.

Some of the characters don't have a nice printable format. For example the newline character (character #13) doesn't print anything but instead tells the console to move the cursor to a new line. You can use the repr function to convert the strings into their representation for printing. (ie newline is represented by '\n')

Hint: A dictionary would be a nice place to store all those counts. You should also know that when a key isn't found in a dictionary, it raises a KeyError exception.

For extra points, with a small modification you can have your program output a word frequency count. (to make things easier, call a word anything that is separated with spaces, tabs or newlines)


Often in programming you'll find yourself writing the same code over and over, maybe with different variable names or values. Consider the encryption program from problem set #2, (head there to see the original question and download the solution) It had you substituting vowels for other letters:

   if (letter == 'a'): letter = 'e'
   elif (letter == 'e'): letter = 'i'
   elif (letter == 'o'): letter = 'u'

When you see a regular pattern like this it often means there's an easier/more compact way to write the code. In this case we can replace the entire if/elif block by using a dictionary to map one letter onto another. Rewrite this program using a dictionary.

More words = more encryption

Acme Crypto Solutions have identified a flaw in their previous attempt at implementing a super-secure encryption algorithm. They believe they have come up with an alternative encryption routine and would like you to implement a beta version for testing.

The algorithm represents every letter of the alphabet with a word beginning with that letter. The letter 'a' will be replaced with the word 'apple' in the encrypted text. You can download the dictionary of replacement words [1] and have your program read them using the open command. (note you will have to unzip the file after you download)

Write a program that reads in this dictionary file, then prompts the user for a sentence and outputs the encrypted text based on their new algorithm. Example output:

   Enter your sentence to encrypt: This is my super-secret code!
   Encrypted text: testing hello important student important student monkey yellow student under people earnest 
   runner- student earnest correction runner earnest testing correction opposite destiny earnest!

Note that you treat upper case letters as lower case letters.