< BACKMake Note | BookmarkCONTINUE >
156135250194107072078175030179198180024228156016206217188240240204175200043194169082000143

Exercises

1:

Strings. Are there any string methods or functions in the string module that will help me determine if a string is part of a larger string?

2:

String Identifiers. Modify the idcheck.py script in Example 6-1 such that it will determine the validity of identifiers of length 1 as well as be able to detect if an identifier is a keyword. For the latter part of the exercise, you may use the keyword module (specifically the keyword.kwlist list) to aid in your cause.

3:

Sorting.

(a) Enter a list of numbers and sort the values in largest-to-smallest order.

(b) Do the same thing, but for strings and in reverse alphabetical (largest-to-smallest lexicographic) order.

4:

Arithmetic. Update your solution to the test score exercise in the previous chapter such that the test scores are entered into a list. Your code should also be able to come up with an average score. See Exercises 2-9 and 5-3.

5:

Strings.

(a) Display a string one character-at-a-time forward and backward as well.

(b) Determine if two strings match (without using comparison operators or the cmp() built-in function) by scanning each string. EXTRA CREDIT: Add case-insensitivity to your solution.

(c) Determine if a string is palindromic (the same backwards as it is for wards). EXTRA CREDIT: add code to suppress symbols and white space if you want to process anything other than strict palindromes.

(d) Take a string and append a backwards copy of that string, making a palindrome.

6:

Strings. Create the equivalent to string.strip(): Take a string and remove all leading and trailing whitespace. (Use of string.*strip() defeats the purpose of this exercise.)

7:

Debugging. Take a look at the code we present in Example 6.4 (buggy.py).

(a) Study the code and describe what this program does. Add a comment to every place you see a comment sign ( # ). Run the program.

(b) This problem has a big bug in it. It fails on inputs of 6, 12, 20, 30, etc., not to mention any even number in general. What is wrong with this program?

(c) Fix the bug in (b).

Example 6.4. buggy program(buggy.py)

This is the program listing for Exercise 6-7. You will determine what this program does, add comments where you see "#"s, determine what is wrong with it, and provide a fix for it.

 <$nopage>
001 1 #!/usr/bin/env python
002 2 
003 3 #
004 4 import string
005 5 
006 6 #
007 7 num_str = raw_input('Enter a number: ')
008 8 
009 9 #
010 10 num_num = string.atoi(num_str)
011 11 
012 12 #
013 13 fac_list = range(1, num_num+1)
014 14 print "BEFORE:", 'fac_list'
015 15 
016 16 #
017 17 i = 0
018 18 
019 19 #
020 20 while i < len(fac_list):
021 21 
022 22     #
023 23     if num_num % fac_list[i] == 0:
024 24         del fac_list[i]
025 25 
026 26     #
027 27     i = i + 1
028 28 
029 29  #
030 30 print "AFTER:", 'fac_list'
031  <$nopage>
8:

Lists. Given an integer value, return a string with the equivalent English text of each digit. For example, an input of 89 results in "eight nine" being returned. EXTRA CREDIT: return English text with proper usage, i.e., "eighty-nine." For this part of the exercise, restrict values to be between zero and a thousand.

9:

Conversion. Create a sister function to your solution for Exercise 6-13 to take the total number of minutes and return the same time interval in hours and minutes, maximizing on the total number of hours.

10:

Strings. Create a function that will return another string similar to the input string, but with its case inverted. For example, input of "Mr. Ed" will result in "mR. eD" as the output string.

11:

Conversion.

(a) Create a program that will convert from an integer to an Internet Protocol (IP) address in the four octet format of WWW.XXX.YYY.ZZZ.

(b) Update your program to be able to do the vice versa of the above.

12:

Strings.

(a) Create a function called findchr(), with the following declaration:

								
    def findchr(string, char)

							

findchr() will look for character char in string and return the index of the first occurrence of char, or -1 if that char is not part of string. You cannot use string.*find() or string.*index() functions or methods.

(b) Create another function called rfindchr() which will find the last occurrence of a character in a string. Naturally this works similarly to findchr() but it starts its search from the end of the input string.

(c) Create a third function called subchar() with the following declaration:

								
    def subchr(string, origchar, newchar)

							

subchr() is similar to findchr() except that whenever origchar is found, it is replaced by newchar. The modified string is the return value.

13:

Strings. The string module contains three functions, atoi(), atol(), and atof(), that convert strings to integers, long integers, and floating point numbers, respectively. As of Python 1.5, the Python built-in functions int(), long(), and float() can also perform the same tasks, in addition to complex() which can turn a string into a complex number. (Prior to 1.5 however, those built-in functions converted only between numeric types.)

An atoc() was never implemented in the string module, so that is your task here. atoc() takes a single string as input, a string representation of a complex number, i.e., '-1.23e+4-5.67j', and returns the equivalent complex number object with the given value. You cannot use eval(), but complex() is available. However, you can use only complex() with the following restricted syntax: complex(real, imag) where real and imag are floating point values. See Table 6.4 for more information regarding the use of complex().

14:

*Random numbers. Design a "rock, paper, scissors" game, sometimes called "Rochambeau," a game you may have played as a kid. If you don't know the rules, they are: at the same time, both you and your opponent have to pick from one of the following: rock, paper, or scissors using specified hand motions. The winner is determined by these rules, which form somewhat of a fun paradox: (a) the paper covers the rock, (b) the rock breaks the scissors, (c) the scissors cut the paper. In your computerized version, the user enters his/her guess, the computer randomly chooses, and your program should indicate a winner or draw/tie. NOTE: the most algorithmic solutions use the fewest number of if statements.

15:

Conversion.

(a) Given a pair of dates in some recognizable standard format such as MM/DD/YY or DD/MM/YY, determine the total number of days that fall between both dates.

(b) Given a person's birth date, determine the total number of days that person has been alive, including all leap days.

(c) Armed with the same information from part (b) above, determine the number of days remaining until that person's next birthday.

16:

Matrices. Process the addition and multiplication of a pair of M by N matrices.

17:

Methods. Implement a function called my pop(), which is similar to the list pop() method. Take a list as input, remove the last object from the list and return it.


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

< BACKMake Note | BookmarkCONTINUE >

© 2002, O'Reilly & Associates, Inc.