VIEWS: 6 PAGES: 3 POSTED ON: 3/17/2010 Public Domain
1 Programming Fundamentals 50:198:111 (Fall 2009) Homework: 2 Professor: Suneeta Ramaswami Due Date: 10/04/09 E-mail: rsuneeta@camden.rutgers.edu Oﬃce: 321 BSB URL: http://crab.rutgers.edu/~rsuneeta Phone: (856)-225-6439 Homework Assignment 2 The assignment is due by 11:59PM of the due date. The point value is indicated in square braces next to each problem. Each solution must be the student’s own work. Assistance should only be sought or accepted from the course instructor. Any violation of this rule will be dealt with harshly. Please read the submission guidelines at the end of this document before you start your work. In this homework assignment, you will get further practice with accumulator patterns and using loops, boolean conditions, and conditional statements. Problem 1 [10 points ] Zeno’s Series. A mathematical series is the sum of an inﬁnite sequence of numbers. The following, known as Zeno’s series, is an example of a mathematical series: 1 1 1 1 1 1 1 1 + + + + + + + + ... 2 4 8 16 32 64 128 256 It can be shown that the above series converges to 1. What this means is that as you add more and more terms of the series, the resulting value gets closer and closer to 1. For example, 1 • The sum of the ﬁrst term of the series is 2 , which is 0.5 1 • The sum of the ﬁrst two terms of the series is 2 + 1 , which is 0.75 4 1 1 • The sum of the ﬁrst three terms of the series is 2 + 4 + 1 , which is 0.875 8 1 1 1 1 • The sum of the ﬁrst four terms of the series is 2 + 4 + 8 + 16 , which is 0.9375 1 1 1 1 1 • The sum of the ﬁrst ﬁve terms of the series is 2 + 4 + 8 + 16 + 32 , which is 0.96875 • . . . and so on Write a function called zenos series with a single parameter N . The function calculates and returns the sum of the ﬁrst N terms of Zeno’s Series. You must use a loop to calculate the result. Problem 2 [10 points ] Number of 2s Write a function called number of 2s with a single parameter N , where N is a positive integer that is no more than 10 digits long. The func- tion should return the number of times the digit 2 occurs in the number N . For example, number of 2s(72252) returns 3, number of 2s(891) returns 0, and number of 2s(2200000000) returns 2. You may assume that N is valid, i.e., that it is positive and no more than 10 digits long. Hint: The mod (%) and integer division (//) operators will be useful here. Problem 3 [15 points ] Tax Calculator In this problem, you are asked to write a program to compute income tax for the year 2008. For the tax year 2008, assume that annual income is taxed at a ﬂat rate of 15%. If taxes are ﬁled strictly before April 1, there is no penalty. 2 If taxes are ﬁled after April 1, there is a 4% penalty for every whole month of delay. For every partial month of delay, the penalty is prorated according to the number of days in that month. For example, suppose the annual salary is $10,000, and taxes are ﬁled on June 20. It follows that the taxes owed are $1,500, and the penalty is $1,066.67 ($400 penalty for April, $400 penalty for May, and 20 .400 = $266.67 penalty for the 20 days of June). 30 Write a function called calculate taxes with three parameters: salary (the annual salary), month (the month of the date in 2008 when taxes are ﬁled), and day (the day of the date in 2008 when taxes are ﬁled). Note that month values should lie in the range 1-12, and day values should lie in the correct range for that month. Your program must check for the validity of the date. If invalid month, day arguments are passed to calculate taxes, it should print an appropriate message and return (see below for some examples). If the date is valid, the function should print out the taxes owed, the penalty, and the total due. Some sample runs in the python interpreter are shown below. >>> from problem4 import * >>> calculate_taxes(10000.00, 6, 20) Taxes: $ 1500.00 Penalty: $ 1066.67 ------------ Total due: $ 2566.67 ------------ >>> >>> calculate_taxes(28625.25, 3 29) Taxes: $ 4293.79 Penalty: $ 0.00 ------------ Total due: $ 4293.79 ------------ >>> calculate_taxes(100560.00, 4, 31) Invalid date! Please try again. >>> calculate_taxes(100560.00, 5, 15) Taxes: $ 15084.00 Penalty: $ 5968.72 ------------ Total due: $ 21052.72 ------------ Problem 4 [15 points ] Bank Loan Program A bank has the following loan program for loans of amounts $3000 or less: no down payment, an interest rate of 18% per year (hence 1.5% per month), and monthly payments of $50. Each monthly payment of $50 is used to pay the interest for that month, and the remaining amount is used to pay oﬀ part of the loan. For 1.5 example, if the loan amount is $1000, the interest is 100 × 1000 = $15 for the ﬁrst month. The remaining amount of $35 is deducted from the loan, leaving a loan of $965.00. In the 1.5 second month, the interest is 100 × 965 = $14.48. The remaining amount of $35.52 ( = 50 - 14.48) is deducted from the loan, leaving a loan of $929.48. In the third month, the interest 3 1.5 is 100 × 929.48 = $13.94. The remaining amount of $36.06 is deducted from the loan, leaving a loan of $893.42, and so on. We are interested in calculating how many months it will take to completely pay oﬀ the loan. Write a function called loan program with a single parameter loan amount, the original loan amount, which must be $3000.00 or less. If it is a value greater than 3000.00, your function should print a message that the entry is invalid, and return. For valid entries, the function should print out the number of months it will take to pay oﬀ the loan, as well as the total interest paid over the life of the loan. Use a while loop to calculate the amount of interest and the remaining debt after each month, and to count the number of months it takes for the loan to reduce to zero. Note that the last payment may be less than $50 and that the very last payment also includes interest on the remaining loan amount. In particular, if the remaining loan amount is $50, the monthly payment of $50 does not completely pay oﬀ the loan because an interest of $0.75 must be applied. Some sample runs in the python interpreter are shown below. >>> from problem3 import * >>> loan_program(2250.50) Number of months to repay the loan in full: 76 Total interest paid: $1525.61 >>> >>> loan_program(5000.00) Sorry, cannot offer loans larger than $3000.00! >>> >>> loan_program(1200.00) Number of months to repay the loan in full: 30 Total interest paid: $298.76 Submission Guidelines You must create a directory called P2 YourLastName (where YourLastName is your actual last name only, all in lower case letters; do not include your ﬁrst name). Under this directory, create four ﬁles, one for each problem. Call these ﬁles problem1.py, problem2.py, problem3.py, and problem4.py for problems #1, #2, #3, and #4, respectively. Before you submit your program, you should create a compressed, tar ﬁle of the directory P2 YourLastName. This is done in two steps as given below (make sure you are one level above the P2 YourLastName directory before typing these commands): % tar cvf P2 YourLastName.tar P2 YourLastName % gzip P2 YourLastName.tar The ’%’ in the above indicates the shell prompt; do not type that in your command. These com- mands should create a ﬁle called P2 YourLastName.tar.gz. E-mail this ﬁle as an attachment to cs111@camden.rutgers.edu. Please make sure everything is working correctly before submitting your assignment. In other words, please avoid multiple submissions. You must submit your assignment at or before 11:59PM on October 4, 2009.