logo
 
Fun with Python
CptS 355 - Programming Language Design
Washington State University
Home
Calendar
Syllabus
Resources
People
Project turn-in

Overview

Weight: This assignment will count for approximately 7% of your final grade.
Due Date Monday, February 6, (by 11:59PM)
In this assignment you will explore some of the unique, and not so unique, features of Python.

Turning in your assignment

Note the following directions carefully. Points will be deducted for incorrectly named files and functions. All code should be developed in the one directory. Each problem solution should be placed in a separate file within that directory with a .py extension, e.g., trans.py. When you are done and certain that everything is working correctly, create a tarred, gzipped copy of your directory by executing
  tar cf one.tar one; gzip one.tar
or zip it on a Windows system. Turn in the tarred, gzipped or zipped directory using the turn-in page. The file you upload should be named one.tar.gz or one.zip. On the upload page, write a comment indicating whether your code is intended for Unix/Linux or Windows -- programs' behavior may differ slightly between the two systems; your code only has to work on one or the other. You may turn in your assignment as many times as you like. Only the last one submitted before the deadline will be graded.

The work you turn in is to be your own personal work. You may not work together with another student, copy code from the web, or anything else that lets you avoid solving the problems for yourself. If you are stuck contact the TA or me, not other students.

Grading

The assignment will be marked for good programming style (indentation and appropriate comments), as well as clean compilation and correction execution.

Warmup -- trans.py -- 30%

The text of a message can be hidden by applying a translation function to each character.

Write a function trans(ttable, s) returning the translated string. trans translates its string argument, s, according to its translation table argument, ttable. Argument ttable will consist of a tuple (pair) of strings (s1, s2), s1 and s2 being the same length with each character appearing at most once in each string. The meaning of the ttable is that a character in the input string, s, that appears in string s1 at position p should be translated to the character at position p in s2. If an input character does not appear in s1 it should appear in the output unchanged. In implementing trans do not use python's built-in translate function or method.

Write a function untrans(ttable,s) taking the same arguments as trans but mapping each character from s2 to the character at the same position in s1. Thus untrans(tt, trans(tt, s)) yields s. In implementing untrans you must use python's translate built-in string method. Look at the definition of maketrans in the string module for help in building the needed translation table argument for translate.

Both trans and untrans must be implemented in a file named trans.py. We will test your functions by using code similar to:

   import trans
   print trans.trans((s1,s2),s)
for various values of s1, s2, and s.

histo.py - 30%

Write a function, histo(s) in file histo.py. The function returns a list of characters in the input string s each paired with its frequency. Characters must appear in the list ordered from most frequent to least frequent. For example, histo('implemented') is [('e',3),('m',2), ('i',1), ('p',1), ('l',1), ('n',1), ('t',1), ('d',1)]. (Characters with the same frequency may appear in any order.) Your code will be tested by using code similar to:
   import histo
   print histo.histo(s)
for various values of s. Hint: Use an implementation of sorting from the Python library. You don't have to write your own sort.

digraphs.py - 30%

A digraph is a pair of characters that occur adjacent to one another in a text. By convention we write each digraph between a pair of '/' characters to make it easier to see where the blanks are. For example the digraphs at the beginning of the previous sentence are /A /, / d/, /di/, /ig/, etc. Digraph frequency counts are helpful in cryptological analysis of some ciphers.

The digraphs(s) function returns a list containing the number of times each digraph occurs in s. Digraphs must be listed from most frequently occurring to least frequently occurring. Digraphs that do not occur in the input (0 occurrences) should not be listed in the output.

Here is what the function might return on some hypothetical sample input: [('/e /', 83), ('/ t/', 66), ('/ a/', 56), ('/in/', 53), ('/th/', 52), ('/ </', 48), ('/co/', 47), ('/re/', 44), ('/de/', 44), ('/s /', 44), ('/to/', 42), ('/he/', 41), ('/n /', 40), ('/d /', 38), ('/ o/', 37), ('/t /', 36), ('/or/', 36), ('/ i/', 34), ('/an/', 33), ('/r /', 32), ('/. /', 31), ('/ d/', 30)]

Cryptogram - 10%

Use your trans, histo, and digraph functions to try to figure out the following cryptogram. If you haven't solved one of these before you may want to look on the web for hints on the relative frequencies of letters and digraphs in the English language. You can use your functions to try out different possibilities and get clues about the translation being used.

Put your answer in a file called cryptogram.txt and zip/tar it along with the rest of the files making up this assignment. Do not reveal your answer to other students.

ABCDEFG HIJ KEFGCILKCEIG, HFM NJJM EF OLIMG HFM GPHQQ PHPPHQG, 
RLQQLFS CDJP OB QLCJIHQQB GTUJJVLFS CDJP CE MJHCD. 
CDJB KELQ CDJPGJQWJG UA HIEUFM CDJLI AIJB, CLSDCJF, OUC 
PJIJQB GTUJJVJ DHIM JFEUSD CE GCEA CDJ AIJB'G OIJHCDLFS HFM/EI 
OQEEM KLIKUQHCLEF. ABCDEFG DHCKD NIEP JSSG, QLRJ PEGC GFHRJG. 
MJAJFMLFS EF CDJ GFHRJ, CDJ FUPOJI EN JSSG LF CDJ FJGC WHILJG 
SIJHCQB, NIEP GJWJIHQ JSSG CE EWJI EFJ-DUFMIJM. 
XDLQJ WJIB UFUGUHQ NEI GFHRJG, CDJ LFMLHF ABCDEF LFKUOHCJG DJI 
JSSG OB GDLWJILFS, CDJIJOB IHLGLFS CDJLI 
CJPAJIHCUIJ CE DJQA CDJP DHCKD TULKRJI. 
CDJ FHPJ EN CDJ ABCDEF QHFSUHSJ DHG FECDLFS CE ME XLCD GFHRJG!

                                                                                                                                                                                                                                                                                                                                             
  (c) 2003 Curtis Dyreson, (c) 2004, 2005 Carl H. Hauser           E-mail questions or comments to Prof. Carl Hauser