Section 5.9. Exercises


5.9. Exercises

The exercises in this chapter may first be implemented as applications. Once full functionality and correctness have been verified, we recommend that the reader convert his or her code to functions that can be used in future exercises. On a related note, one style suggestion is not to use print statements in functions that return a calculation. The caller can perform any output desired with the return value. This keeps the code adaptable and reusable.

5-1.

Integers. Name the differences between Python's regular and long integers.

5-2.

Operators.

  1. Create a function to calculate and return the product of two numbers.

  2. The code which calls this function should display the result.

5-3.

Standard Type Operators. Take test score input from the user and output letter grades according to the following grade scale/curve:

  1. 90-100

  2. 80-89

  3. 70-79

  4. 60-69

  5. <60

5-4.

Modulus. Determine whether a given year is a leap year, using the following formula: a leap year is one that is divisible by four, but not by one hundred, unless it is also divisible by four hundred. For example, 1992, 1996, and 2000 are leap years, but 1967 and 1900 are not. The next leap year falling on a century is 2400.

5-5.

Modulus. Calculate the number of basic American coins given a value less than 1 dollar. A penny is worth 1 cent, a nickel is worth 5 cents, a dime is worth 10 cents, and a quarter is worth 25 cents. It takes 100 cents to make 1 dollar. So given an amount less than 1 dollar (if using floats, convert to integers for this exercise), calculate the number of each type of coin necessary to achieve the amount, maximizing the number of larger denomination coins. For example, given $0.76, or 76 cents, the correct output would be "3 quarters and 1 penny." Output such as "76 pennies" and "2 quarters, 2 dimes, 1 nickel, and 1 penny" are not acceptable.

5-6.

Arithmetic. Create a calculator application. Write code that will take two numbers and an operator in the format: N1 OP N2, where N1 and N2 are floating point or integer values, and OP is one of the following: +, -, *, /, %, **, representing addition, subtraction, multiplication, division, modulus/remainder, and exponentiation, respectively, and displays the result of carrying out that operation on the input operands. Hint: You may use the string split() method, but you cannot use the exal() built-in function.

5-7.

Sales Tax. Take a monetary amount (i.e., floating point dollar amount [or whatever currency you use]), and determine a new amount figuring all the sales taxes you must pay where you live.

5-8.

Geometry. Calculate the area and volume of:

  1. squares and cubes

  2. circles and spheres

5-9.

Style. Answer the following numeric format questions:

  1. Why does 17 + 32 give you 49, but 017 + 32 give you 47 and 017 + 032 give you 41, as indicated in the examples below?

    >>> 17 + 32 49 >>> 017+ 32 47 >>> 017 + 032 41

  2. Why do we get 134L and not 1342 in the example below?

    >>> 56l + 78l 134L

5-10.

Conversion. Create a pair of functions to convert Fahrenheit to Celsius temperature values. C = (F - 32) * (5 / 9 ) should help you get started. We recommend you try true division with this exercise, otherwise take whatever steps are necessary to ensure accurate results.

5-11.

Modulus.

  1. Using loops and numeric operators, output all even numbers from 0 to 20.

  2. Same as part (a), but output all odd numbers up to 20.

  3. From parts (a) and (b), what is an easy way to tell the difference between even and odd numbers?

  4. Using part (c), write some code to determine if one number divides another. In your solution, ask the user for both numbers and have your function answer "yes" or "no" as to whether one number divides another by returning TRue or False, respectively.

5-12.

Limits. Determine the largest and smallest ints, floats, and complex numbers that your system can handle.

5-13.

Conversion. Write a function that will take a time period measured in hours and minutes and return the total time in minutes only.

5-14.

Bank Account Interest. Create a function to take an interest percentage rate for a bank account, say, a Certificate of Deposit (CD). Calculate and return the Annual Percentage Yield (APY) if the account balance was compounded daily.

5-15.

GCD and LCM. Determine the greatest common divisor and least common multiple of a pair of integers.

5-16.

Home Finance. Take an opening balance and a monthly payment. Using a loop, determine remaining balances for succeeding months, including the final payment. "Payment 0" should just be the opening balance and schedule monthly payment amount. The output should be in a schedule format similar to the following (the numbers used in this example are for illustrative purposes only):

Enter opening balance:100.00 Enter monthly payment: 16.13          Amount    Remaining Pymt#     Paid      Balance -----    ------    ---------   0      $ 0.00     $100.00   1      $16.13     $ 83.87   2      $16.13     $ 67.74   3      $16.13     $ 51.61   4      $16.13     $ 35.48   5      $16.13     $ 19.35   6      $16.13     $  3.22   7      $ 3.22     $  0.00


5-17.

*Random Numbers. Read up on the random module and do the following problem: Generate a list of a random number (1 < N <= 100) of random numbers (0 <= n <= 231-1). Then randomly select a set of these numbers (1 <= N <= 100), sort them, and display this subset.



Core Python Programming
Core Python Programming (2nd Edition)
ISBN: 0132269937
EAN: 2147483647
Year: 2004
Pages: 334
Authors: Wesley J Chun

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net