2.4 Conditionals and Strings
Be not thou lukewarm
But be either hot xor cold
"Jay" == "Jay": False
2.4.1 Objectives
how to use numeric comparison operators
how to use the boolean operators &&, ||, and !
when to use a auxiliary function
"clean up" a function by creating auxiliary functions for common computations
2.4.2 Exercises (due 2011/09/19 08:45:00)
1. What is the truth value of 4 > 3 && 10 <= 100?
2. What are the truth values of x > 3, 4 > x && x > 3, and x * x == x when x is 4?
3. Encode the following interval as a function: (3,7]
4. Encode the following interval as a function: everything outside [1,3]
5. Which interval does this expression represent? -3 < x && x < 0
6. Write a function that identifies a solution to this equation: 4n + 2 = 62
7. Develop the function interest. Like interestRate, it consumes a deposit amount. Instead of the rate, it produces the actual amount of interest that the money earns in a year. The bank pays a flat 6% for deposits of up to $1,600, a flat 7.5% per year for deposits of up to $9,000, and a flat 8% for deposits of more than $9,000.
8. Some credit card companies pay back a small portion of the charges a customer makes over a year. One company returns .25% for the first $500 of charges, .50% for the next $1000 (that is, the portion between $500 and $1500), .75% for the next $1000 (that is, the portion between $1500 and $2500), and 1.0% for everything above $2500.
Thus, a customer who charges $400 a year receives $1.00, which is 0.25 · 1/100 · 400, and one who charges $1,400 a year receives $5.75, which is 1.25 = 0.25 · 1/100 · 500 for the first $500 and 0.50 · 1/100 · 900 = 4.50 for the next $900.
Determine by hand the pay-backs for a customer who charged $2000 and one who charged $2600.
Define the function payback, which consumes a charge amount and computes the corresponding pay-back amount.
2.4.3 Optional Exercises (due 2011/09/23 08:45:00)
9. What are the truth values of x > 3, 4 > x && x > 3, and x * x == x when x is 7/2?
10. Encode the following interval as a function: [3,9)
11. Write a function that identifies a solution to this equation: 4n^2 + 6n + 2 = 462
12. Develop the function tax, which consumes the gross pay and produces the amount of tax owed. For a gross pay of $240 or less, the tax is 0%; for over $240 and $480 or less, the tax rate is 15%; and for any pay over $480, the tax rate is 28%.
Develop netpay. The function determines the net pay of an employee from the number of hours worked. The net pay is the gross pay minus the tax. Assume the hourly pay rate is $12.
2.4.4 Notes
These notes are primarily for my sake, but I don’t see any reason to hide them from you.
Divide by zero |
Area of tube |
TAs! |
|
----- |
|
Computers deal with different situations... is the click on the demon or the shopkeeper? Was that a positive or negative number? |
|
Consider: |
|
Company XYZ & Co. pays all its employees $12 per hour. A typical employee works between 20 and 65 hours per week. Develop a program that determines the wage of an employee from the number of hours of work, if the number is within the proper range. |
|
Math deals with true and false claims... these are the different booleans |
|
We can create claims... ==, <, >, etc |
|
We can combine claims... with and, or, or not |
|
Functions can return booleans... |
- isFiveHuh |
- isBetween5and6Huh |
- isBetween5and6OrOver10Huh |
|
We can think of these as intervals on the number line |
|
We can go back and forth |
|
We can write programs that identify solutions to equations like |
- 6n + 3 = 12 |
- 2n^2 = 74 |
- 3n^2 + 2n + 11 = 19 |
|
These programs all /produce/ booleans. What about programs that /consume/ booleans? |
|
When do we need this in the real world? |
|
"Suppose the bank pays 4% for deposits of up to $1,000 (inclusive), 4.5% for deposits of up to $5,000 (inclusive), and 5% for deposits of more than $5,000." |
|
if and else |
|
information => data => examples |
|
intervals mean there must be an if that decides which part of the interval the input comes from |
|
our "take stock" part of the design recipe has a new step |
|
sometimes we can simplify programs because we know earlier conditions are false |