< BACKMake Note | BookmarkCONTINUE >
156135250194107072078175030179198180024228156016206217188240240204175204055035117210164078

Exercises

1:

Conditionals. Study the following code:

								
# statement A
if x > 0:
     # statement B
     pass

elif x < 0:
     # statement C
     pass

else:
    # statement D
    pass

# statement E

							

(a) Which of the statements above (A, B, C, D, E) will be executed if x < 0?

(b) Which of the statements above will be executed if x == 0?

(c) Which of the statements above will be executed if x > 0?

2:

Loops. Write a program to have the user input three (3) numbers: (f)rom, (t)o, and (i)ncrement. Count from f to t in increments of i, inclusive of f and t. For example, if the input is f == 2, t == 24, and i == 4, the program would output: 2, 6, 10, 14, 18, 22.

3:

range(). What argument(s) could we give to the range() built-in function if we wanted the following lists to be generated?

(a)[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

(b)[3, 6, 9, 12, 15, 18]

(c)[-20, 200, 420, 640, 860]

4:

Prime Numbers. We presented some code in this chapter to determine a number's largest factor or if it is prime. Turn this code into a Boolean function called isprime() such that the input is a single value, and the result returned is 1 if the number is prime and 0 otherwise.

5:

Factors. Write a function called getfactors() that takes a single integer as an argument and returns a list of all its factors, including 1 and itself.

6:

PrimeFactorization. Take your solutions for isprime() and getfactors() in the previous problems and create a function that takes an integer as input and returns a list of its prime factors. This process, known as prime factorization, should output a list of factors such that if multiplied together, they will result in the original number. Note that there could be repeats in the list. So if you gave an input of 20, the output would be [2, 2, 5].

7:

Perfect Numbers. A perfect number is one whose factors (except itself) sum to itself. For example, the factors of 6 are 1, 2, 3, and 6. Since 1 + 2 + 3 is 6, it (6) is considered a perfect number. Write a function called isperfect() which takes a single integer input and outputs 1 if the number is perfect and 0 otherwise.

8:

Factorial. The factorial of a number is defined as the product of all values from one to that number. A shorthand for N factorial is N! where N! == factorial (N) == 1 * 2 * 3 * … * (N-2) * (N-1) * N!. So 4! == 1 * 2 * 3 * 4. Write a routine such that given N, the value N! is returned.

9:

Fibonacci Numbers. The Fibonacci number sequence is 1, 1, 2, 3, 5, 8, 13, 21, etc. In other words, the next value of the sequence is the sum of the previous two values in the sequence.

10:

Text Processing. Determine the total number of vowels, consonants, and words (separated by spaces) in a text sentence. Ignore special cases for vowels and consonants such as "h," "y," "qu," etc.

11:

Text Processing. Write a program to ask the user to input a list of names, in the format "Last Name, First Name," i.e., last name, comma, first name. Write a function that manages the input so that when/if the user types the names in the wrong order, i.e., "First Name Last Name," the error is corrected, and the user is notified. This function should also keep track of the number of input mistakes. When the user is done, sort the list, and display the sorted names in "Last Name, First Name" order.

EXAMPLE input and output: (you don't have to do it this way exactly)

								
% nametrack.py
Enter total number of names: 5

Please enter name 0: Smith, Joe
Please enter name 1: Mary Wong
>> Wrong format…   should be Last, First
>> You have done this 1 time(s) already.  Fixing input…
Please enter name 2: Hamilton, Gerald
Please enter name 3: Royce, Linda
Please enter name 4: Winston Salem
>> Wrong format…   should be Last, First
>> You have done this 2 time(s) already.  Fixing input…

The sorted list (by last name) is:
    Hamilton, Gerald
    Royce, Linda
    Salem, Winston
    Smith, Joe
    Wong, Mary
12:

(Integer) Bit Operators. Write a program that takes begin and end values and prints out a decimal, binary, octal, hexadecimal chart like below. If any of the characters are printable ASCII characters, then print those, too. If none is, you may omit the ASCII column header.

								
SAMPLE OUTPUT 1
---------------
Enter begin value: 9
Enter end value: 18
DEC    BIN    OCT    HEX
---------------------------
  9    01001   11     9
 10    01010   12     a
 11    01011   13     b
 12    01100   14     c
 13    01101   15     d
 14    01110   16     e
 15    01111   17     f
 16    10000   20    10
 17    10001   21    11
 18    10010   22    12
SAMPLE OUTPUT 2
---------------
Enter begin value: 26
Enter end value: 41
DEC    BIN     OCT    HEX    ASCII
-------------------------------------
 26   011010    32    1a
 27   011011    33    1b
 28   011100    34    1c
 29   011101    35    1d
 30   011110    36    1e
 31   011111    37    1f
 32   100000    40    20
 33   100001    41    21     !
 34   100010    42    22     "
 35   100011    43    23     #
 36   100100    44    24     $
 37   100101    45    25     %
 38   100110    46    26     &
 39   100111    47    27     '
 40   101000    50    28     (
 41   101001    51    29     )

							
13:

Performance. In Section 8.5.2, we examined two basic ways of iterating over a sequence: (1) by sequence item, and (2) via sequence index. We pointed out at the end that the latter does not perform as well over the long haul (on my system here, a test suite shows performance is nearly twice as bad [83% worse]). Why do you think that is, and what are the reasons?


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

< BACKMake Note | BookmarkCONTINUE >

© 2002, O'Reilly & Associates, Inc.