Paired Programming Activity
Loop Problems, part 1
Partners
- Unit 3 Partners
Activities
I would like you to attempt to complete several small Python scripts working with your partner.
Please try to make your program output look as much like the example as possible and verify it works with the example input provided. If you have difficulty finding the information or understanding how to approach the problem, you should contact me.
To complete this activity, you will need to create the following scripts:
- LOOP 1 - discountTable()
- LOOP 2 - calculateBill()
- LOOP 3 - collatz()
- LOOP 4 - finalPopulation() [Challenge Problem]
Replit appears to be slowly breaking Teams for Education. As such, I encourage you to use the autograders on Autolab.cs.uni.edu under Replit Replacement for Problems 3 and 4. I am still trying to figure our how to get a valid grader for Problem 1 and 2.
LOOP 1
Santa put Kohls in your stocking
If you have ever shopped at one of those stores like Kohls that seems to have CONSTANT sales, you will now that they often have signs showing you the original price of the item and the sales price of the item. If there are lots of items with different prices they will often print a table of multiple items.
Write a Python function that conforms to the following:
- Function name
- discountTable()
- Parameters
- A (lower) starting Price (must be an integer)
- A (higher) ending Price (must be an integer)
- An interval between prices (must be an integer)
- A % off discount (could be a float)
- Return value
- Nothing.
- This is what is known as a "non-fruitful" function. It doesn't produce results in the traditional sense. Instead it prints inside of the function.
- So what does it print:
- The function should print a table of all the original price and the sale price for all amounts in the interval defined by the first three parameters.
- Place three spaces between the two "columns"
- Round the prices to 2 decimals (Remember, that this won't add decimal places. Just remove them)
- Include $ symbols as shown.
- NOTE, this will only work if your printing matches mine EXACTLY.
- Examples
LOOP 2
Golden Panda has the best Mu Shu Pork
A particular Chinese buffet determines the price of your meal based on the age of the guests.
- Guests 2 and younger are free
- Children between 3 and 12 are $4.50
- Seniors 65 and over cost $9.50
- All other customers cost $11.00
Write a Python function that conform to the following:
- Function name
- calculateBill()
- Parameters
- None
- What it does
- Uses the input() function inside of the calculateBill() function to keep asking for the ages of the customers. It stops when it sees a -1 as an input.
- Return value
- The total bill rounded to two decimal places
- Examples
NOTE: There is not an automated tester for this in Replit. There are some challenges in getting this particular style to work. But I encourage you to test it thoroughly yourself. Try a variety of inputs and see if you get the expected outputs.
LOOP 3
I have a conjecture
According to Wikipedia, "The Collatz conjecture is one of the most famous unsolved problems in mathematics. The conjecture asks whether repeating two sipmle arithmetic operations will eventually transofrm every positive integer into 1." It is named after mathematician Lothar Collatz who first introcued the idea in 1937
The process is as follows:
Then you continue this process on the new number repeatedly until you finally arrive at the number 1.
For example:
f(3) = because 3 is odd we take 3*n+1 or 3*3+1 = 10f(10) = because 10 is even we take n/2 or 10/2 = 5
f(5) = because 5 is odd we take 3*n+1 or 3*5+1 = 16
f(16) = because 16 is even we take n/2 or 16/2 = 8
f(8) = 4
f(4) = 2
f(2) = 1
In other words, it took us 7 moves to go from 3 to 1.
While it hasn't been officially proven yet, Collatz has proposed that EVERY positive integer will eventually work its way to 1.
Write a Python function that conforms to the following:
- Function name
- collatz()
- Parameters
- A positive integer
- Return value
- The number of moves it takes to go from the starting parameter to 1
- Examples
LOOP 4
It's a Growing Problem
NOTE: This is a challenge problem.
I think it is a great example of a basic loop problem.
BUT, the math in it is a bit tricky and requires you to put together some things we have done in very different ways.
The challenge isn't the loop - it's the math that controls the loop. I encourage you to give it an honest try and then
watch the video to see how I would solve it.
Steph Infection is a biologist who is studying the generation time of various bacteria.
In biology the generation time is the time it takes for a population to double in size. For example, when E Coli bacteria are placed in a favorable medium, such as Glucose salts, they have a generation time of approximately 20 minutes.
Steph wants to be able to calculate how many bacteria she should expect to find in her petri dishes after a certain amount of time.
- Write code that asks the user for:
- The starting size of the bacteria culture
- The generation time of the bacteria (assumed to be in minutes)
- How much time will elapse (assumed to be in hours)
- The program will then
- Calculate how many generation intervals have occured.
- Round to the nearest interval number
- Calculate and print how many bacteria are in the final sample
Write a Python function that conforms to the following:
- Function name
- finalPopulation()
- Parameters
- The initial size of the bacteria culture
- The generation time of the bacteria in the culture (assumed to be in minutes)
- How much time will elapse (assumed to be in hours)
- Return value
- The number of bacteria expected in the final sample
- Examples
- Special Notes
- This is a looping problem where every time through the loop the population doubles. You should be using a loop and not a single mathematical formula to calculate the results.
- You will have to determine how many "generations" occur during the time elapsed so you know how many times to run the loop.
- For example, a 20 minute generation time over 3 hours (180 minutes) is 9 generations.
- If the number of generations isn't an integer you should ROUND to the nearest integer
- For example, a 17 minute generation time over 3 hours is 10.588 generations which we will round to 11
- As another example, a 16 minute generation time over 3 hours is 11.25 which we will also round to 11