Menu
Home
Log in / Register
 
Home arrow Computer Science arrow Python Programming Fundamentals
< Prev   CONTENTS   Next >

3.9 Review Questions

1. Where did the term computer originate?

2. What is a sequence in Python? Give an example.

3. How do you call a method on an object? What is the general form? Give an example that's not in the book.

4. What is a class in Python?

5. What is a type in Python?

6. Definite iteration is when the number of iterations is known before the loop starts. What construct in Python is used for definite iteration?

7. Indefinite iteration is what happens when the exact number of iterations is not known before the loop begins (but still may be calculable if you know the input). What construct in Python is used for indefinite iteration?

8. How can you get at the last element of a list? Give two examples of expressions that return the last element of a list.

9. If you wanted to print all the items of a list in reverse order using a while loop, how would you do it? Write some example code that demonstrates how this might be accomplished. Remember, you must use a while loop in your answer.

10. How would you use the Guess and Check pattern to find a name in a phonebook? Write some code that searches a list of names for someone's name. Is there a more efficient way of finding a name in a phonebook?

11. Lists and strings are similar in many ways. One major difference is that lists are mutable and strings are not. What does that mean? Give an example of an operation that lists support but strings do not.

12. Why does mutable data sometimes lead to confusion when programming?

13. What is the accumulator pattern? Give an example of how it might be used.

14. There are two ways to read from a file that are presented in the text. Describe both of them. When is one more appropriate than the other?

3.10 Exercises

1. Write a program that prints all the prime numbers less than 1,000. You can write this program by creating a list of prime numbers. To begin, the list is empty. Then you write two nested for loops. The outer for loop runs through all the numbers from 2 to 999. The inner for loop runs through the list of prime numbers. If the next number in the outer for loop is not divisible by any of the prime numbers, then it is prime and can be printed as a prime and added to the list of primes. To add an element, e, to a list, lst, you can write lst.append(e). This program uses both the guess and check pattern and the accumulator pattern to build the list of prime numbers.

2. Write a menu driven program that works with an address book file as described in Example 3.14. You may want to consult Example 2.6 to see how to print a menu to the user and get input from them. Your program should have three menu items, look up a name, add a contact, and quit. Interacting with your program should look something like this:

1) Look up a person by last name

2) Add a person to the address book . 3) Quit

Enter your choice : 1

Please enter the last name to look up : Lie

Sophus Lie

2234 Valdres Rd

Decorah , IA 52101

home : 777 -555 -1234

mobile : 777 -554 -4765

1) Look up a person by last name

2) Add a person to the address book . 3) Quit

Enter your choice : 3 Done

You will want to create your own address book file for this problem. Call the file “addressbook.txt”. You can create it by selecting New in your IDE and then saving it in the same directory as your program. You should call the file “addressbook.txt”. Don't add a “.py” to the end of this text file. Be sure when you write to the file that you put a newline character at the end of each line. If you create your own file there should be a newline character at the end of each line. If you don't do this then when you try to write another record to the file it may not end up formatted correctly. You can always open the text file with Wing to take a look at it and see if it looks like the format presented in Example 3.14.

3. Write a program that asks the user to enter a list of numbers and then prints the count of the numbers in the list and the average of the numbers in the list. Do not use the len function to find the length of the list. Use the accumulator pattern instead. The program would print this when run.

Please enter a list of numbers : 1 . 0 10 3 . 5 4 . 2 10.6

There were 5 numbers in the list .

The average of the numbers was 5.86

4. Write a program that asks the user to enter a list of numbers. The program should take the list of numbers and add only those numbers between 0 and 100 to a new list. It should then print the contents of the new list. Running the program should look something like this:

Please enter a list of numbers : 10.5 -8 105 76 83.2 206

The numbers between 0 and 100 are : 10.5 76.0 83.2

5. Write a program that asks the user to enter a list and then builds a new list which is the reverse of the original list.

6. Draw a picture of the variable references and values that result from running the code in Exercise 5.

7. Write a program that asks the user to enter a list and then reverses the list in place so that after reversing, the original list has been reversed instead of creating a new list.

8. Draw a picture of the variable references and values that result from running the code in Exercise 7.

9. Write a program that asks the user to enter a list of integers one at a time. It should allow the user to terminate the list by entering a − 1. Running the program would look something like this.

Enter a list of integers terminated by a -1.

Please enter the first integer and press enter : 5

Please enter another integer : 4

Please enter another integer : 3

Please enter another integer : 8

Please enter another integer : -1 The list of integers is 5 4 3 8

10. Write a program that computes a user's GPA on a 4 point scale. Each grade on a 4 point scale is multiplied by the number of credits for that class. The sum of all the credit, grade products is divided by the total number of credits earned. Assume the 4 point scale assigns values of 4.0 for an A, 3.7 for an A−, 3.3 for

a B+, 3.0 for a B, 2.7 for a B−, 2.3 for a C+, 2.0 for a C, 1.7 for a C−, 1.3

for a D+, 1.0 for a D, 0.7 for a D−, and 0 for an F. Ask the user to enter their credit grade pairs using the following format until the enter 0 for the number of

credits.

This program computes your GPA .

Please enter your completed courses .

Terminate your entry by entering 0 credits . Credits ? 4

Grade ? A

Credits ? 3 Grade ? B+

Credits ? 4 Grade ? B -

Credits ? 2 Grade ? C

Credits ? 0

Your GPA is 3.13

11. Example 1.1 on p. 11 presented a nice algorithm for converting a base 10 integer to binary. It turns out that this algorithm works for both positive and negative integers. Write this algorithm one more time. This time, use a loop to avoid duplicating any code. Write the algorithm so it will convert any 32-bit signed

integer to its binary equivalent. Thirty-two bit signed integers are integers in the range of −231 to 231 − 1. That would be integers in the range −2, 147, 483, 648 to 2, 147, 483, 647. Be sure to eliminate any leading 0s from the result before it is printed. Your loop should terminate when the number you are converting has reached zero (according to the algorithm) or when you've reached the requisite 32 bits for your number.

 
Found a mistake? Please highlight the word and press Shift + Enter  
< Prev   CONTENTS   Next >
 
Subjects
Accounting
Business & Finance
Communication
Computer Science
Economics
Education
Engineering
Environment
Geography
Health
History
Language & Literature
Law
Management
Marketing
Philosophy
Political science
Psychology
Religion
Sociology
Travel