< BACKMake Note | BookmarkCONTINUE >
156135250194107072078175030179198180024228156016206217188240240204175202199139030046143245

Exercises

1:

Dictionary Methods. What dictionary method would we use to combine two dictionaries together?

2:

Dictionary Keys. We know that dictionary values can be arbitrary Python objects, but what about the keys? Try using different types of objects as the key other than numbers or strings. What worked for you and what didn't? As for the failures, why do you think they didn't succeed?

3:

Dictionary and List Methods.

(a) Create a dictionary and display its keys alphabetically.

(b) Now display both the keys and values sorted in alphabetical order by the key.

(c) Same as part (b), but sorted in alphabetical order by the value. (Note: this generally has no practical purpose in dictionaries or hash tables in general because most access and ordering [if any] is based on the keys. This is merely an exercise.)

4:

Creating Dictionaries. Given a pair of identically-sized lists, say, [1, 2, 3,], and ['abc', 'def', 'ghi',], process all that list data into a single dictionary that looks like: {1: 'abc', 2: 'def', 3: 'ghi',}.

5:

userpw2.pw. Following problem deals with the program in Example 7.1 a manager of a database of name-password key-value pairs.

(a) Update the script so that a timestamp is also kept with the password indicating date and time of last login. This interface should prompt for login and password and indicate a successful or failed login as before, but if successful, it should update the last login timestamp. If the login occurs within four hours of the last login, tell the user, "You already logged in at: <last_login_timestamp>."

(b) Add an "administration" menu to include the following two menu options: (1) remove a user and (2) display a list of all users in the system and their passwords

(c) The passwords are currently not encrypted. Add password-encryption if so desired (see the crypt, rotor, or other cryptographic modules)

(d) Add a GUI interface, i.e., Tkinter, on top of this application.

6:

Lists and Dictionaries. Create a crude stock portfolio database system: There should be at least four data columns: stock ticker symbol, number of shares, purchase price, and current price (you can add more if you wish). Have the user input values for each column to create a single row. Each row should be created as list. Another all-encompassing list will hold all these rows. Once the data is entered, prompt the user for one column to use as the sort metric. Extract the data values of that column into a dictionary as keys, with their corresponding values being the row that contains that key. Be mindful that the sort metric must have non-coincidental keys or else you will lose a row because dictionaries are not allowed to have more than one value with the same key. You may also choose to have additional calculated output, such as percentage gain/loss, current portfolio values, etc.

7:

Inverting Dictionaries. Take a dictionary as input and return one as output, but the values are now the keys and vice versa.

8:

Human Resources. Create a simple name and employee number dictionary application. Have the user enter a list of names and employee numbers. Your interface should allow a sorted output (sorted by name) that displays employee names followed by their employee numbers. EXTRA CREDIT: come up with an additional feature that allows for output to be sorted by employee numbers.

9:

Translations.

(a) Create a character translator (that works similar to the Unix tr command). This function, which we will call tr(), takes three strings as arguments: source, destination, and base strings, and has the following declaration:

								
def tr(srcstr, dststr, string)

							

srcstr contains the set of characters you want "translated," dststr contains the set of characters to translate to, and string is the string to perform the translation on. For example, if srcstr == 'abc', dststr == 'mno', and string == 'abcdef', then tr() would output 'mno-def'. Note that len(srcstr) == len(dststr). For this exercise, you can use the chr() and ord() built-in functions, but they are not necessary to arrive at a solution.

(b) Add a new flag argument to this function to perform case-insensitive translations.

(c) Update your solution so that it can process character deletions. Any extra characters in srcstr which are beyond those which could be mapped to characters in dststr should be filtered. In other words, these characters are mapped to no characters in dststr, and are thus filtered from the modified string which is returned. For example, if srcstr == 'abcdef', dststr == 'mno', and string == 'abcdefghi', then tr() would output 'mnoghi'. Note now that len(srcstr) >= len(dststr).

10:

Encryption. Using your solution to the previous problem, and create a "rot13" translator. "rot13" is an old and fairly simplistic encryption routine where by each letter of the alphabet is rotated 13 characters. Letters in the first half of the alphabet will be rotated to the equivalent letter in the second half and vice versa, retaining case. For example, 'a' goes to 'n' and 'X' goes to 'K'. Obviously, numbers and symbols are immune from translation.

(b) Add an application on top of your solution to prompt the user for strings to encrypt (and decrypt on reapplication of the algorithm), as in the following examples:

								
% rot13.py
Enter string to rot13: This is a short sentence.
Your string to en/decrypt was: [This is a short
sentence.].
The rot13 string is: [Guvf vf n fubeg fragrapr.].
%
% rot13.py
Enter string to rot13: Guvf vf n fubeg fragrapr.
Your string to en/decrypt was: [Guvf vf n fubeg
fragrapr.].
The rot13 string is: [This is a short sentence.].

							


Last updated on 9/14/2001
Core Python Programming, © 2002 Prentice Hall PTR

< BACKMake Note | BookmarkCONTINUE >

© 2002, O'Reilly & Associates, Inc.