Pair work Ch 1.4
- Due Jan 31, 2022 by 9:50am
- Points 0
- Available after Jan 31, 2022 at 9am
Note: due to Monday's class being cancelled, this pair work is optional.
Problem 1
In the last class, you looked at a solution to the following problem:
"The Library of Congress stores its holdings on 838 miles of shelves. Assuming an average book is one inch thick, how many books would this hold? Write Python code to solve this problem." (Havill, Ch. 1.3)
The answer to this problem is: 53,095,680. Here's a program that generalizes this a bit for a bookshelf of any length and any average thickness of book:
print('Bookshelf Storage Calculator')
print('='*60)
lengthOfBookshelf = input('How many miles long is the bookshelf: ')
lengthOfBookshelf = float(lengthOfBookshelf)
averageLengthOfBooks = input('How many inches thick is an average book you want to store? ')
averageLengthOfBooks = float(averageLengthOfBooks)
INCHES_PER_MILE = 63360
print(f'You can store approximately {int(lengthOfBookshelf*INCHES_PER_MILE/averageLengthOfBooks)} books on your bookshelf')
Write four test cases for this program that will ensure it works as expected. Recall the testing rules of thumb:
- disallowed inputs should be listed or documented
- test with common legal inputs (consider entire range)
- test boundary cases
- at the limits of legal/illegal inputs
- test corner cases (rare inputs)
Problem 2
What is the complexity of the code from Problem 1: O(1) constant time or O(n) linear time? Explain.
Problem 3
Consider an algorithm that takes a user's name, counts down from 10, then wishes them a happy birthday:
Algorithm: Happy BirthdayInput: name (a person's name)
1. counter <- 10
2. while counter is greater than 0:
3. counter <- counter - 1
4. display: Happy Birthday, name!
Output: nothing
What is the complexity of the code from Problem 1: O(1) constant time or O(n) linear time? Explain.
Problem 4
Look at the code from Problem 1. Re-write the code to be more compact (fewer lines of code overall).