This project has three tasks. For all tasks, unless explicitly stated otherwise, you must write functions with for loops and set up accumulator patterns to add up the number or string sequences generated by the for loops.
You are to submit this project with a partner, chosen by your lab instructor. Exactly one of you will submit the project on Moodle, on behalf of both partners.
This task is designed to let you practice using the accumulator pattern for strings. You are required to set up an accumulator pattern to do this task. You may not use if statements or other concepts that were not covered during the first two weeks of classes.
Open up a new file window in IDLE and type in a definition for a function, IntegerBars
.
The function
should have two parameters, specifying two integers a, b
.
Your function should print the integers between a, b
, inclusive.
Between each integer, your function should include a stick,
"|"
.
If a > b
, your function should only print a
.
See the following examples:
>>> IntegerBars(1,25) 1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25 >>> IntegerBars(-5,10) -5|-4|-3|-2|-1|0|1|2|3|4|5|6|7|8|9|10 >>> IntegerBars(2,2) 2 >>> IntegerBars(2,1) 2 >>> IntegerBars(2,3) 2|3Hints and references: lecture slides from week 2 Friday and week 2 Wednesday in-class activity acc1.py. See also the
str
funtion in pages 91-92 and Session 3.7 in the textbook.
Save your program in a file called project2.py
.
Do not forget to include the contract and docstring for this function.
Use IDLE's "Run Module" command from the Run menu, or equivalently press the F5 key. If IDLE reports any syntax
errors in your program, fix them. When you get past that hurdle, use the Python shell window of IDLE to try your
IntegerBars
function out for different pairs of integer inputs. Make sure your function gives the same answers as
the examples shown above; if not, fix it.
Consider the following traditional Christmas song:
On the first day of Christmas
My true love gave to me
A partridge in a pear tree.
On the second day of Christmas
My true love gave to me
Two turtle doves
And a partridge in a pear tree.
On the third day of Christmas
My true love gave to me
Three French hens,
Two turtle doves,
And a partridge in a pear tree.
And so on, through the twelfth day of Christmas. Let's count a partridge in a pear tree as constituting a single present, so that on the first day, my true love gave me one present. Similarly on the second day my true love gave me three presents, on the third day six presents, and so on.
If I wanted to figure out how many presents I received on a particular day, the most straightforward approach would be to add them all up. Section 1.4 in our textbook explains a slicker approach based on visualizing the presents that day as arranged in a triangle. The triangle can fit together with another equal-sized triangle to form a rectangle. This allows a direct calculation of the number of presents on day n as n(n+1)/2. If all is well, both approaches ought to give the same answers.
Suppose we instead want to figure out my total haul for the entire Christmas season. Added up over all twelve days, how many presents did my true love give me? The geometric approach still works, if you can visualize three-dimensional shapes well enough. You can lay down the big triangle from day 12, stack on top of it a smaller triangle from day 11, and so forth on up to form a pyramid. If you figure out how many of those pyramids fit together to form a rectangular solid, and how long each of the three sides of the rectangular solid are, you can derive a formula for the total number of presents. But I don't recommend you try this, at least without having a more straightforward calculation to check your work against, because most humans aren't very good at rotating pyramids around in their heads. So let's write a straightforward program that totals up the answer. While we're at it, we can compare the two approaches in the single-day case as well.
Here is what you need to do for this task:
In the same file project2.py
, type in a definition for a function, presentsOnDay
. The function
should have one parameter, specifying which day of Christmas you are interested in. (For example, you would pass
in 12 to find out how many presents my true love gave me on the 12th day of Christmas.) Be sure to use a descriptive
name for the parameter. Your function should return the overall number of presents for the specified day. Use the
accumulator pattern to calculate the answer by adding up all the different kinds of presents.
Don't forget to include the correct contract and docstring.
Save your program in
a file called project2.py
.
Use IDLE's "Run Module" command from the Run menu, or equivalently press the F5 key. If IDLE reports any syntax
errors in your program, fix them. When you get past that hurdle, use the Python shell window of IDLE to try your
presentsOnDay
function out for days 1, 2, and 3. Make sure your function gives the same answers as
the ones previously mentioned; if not, fix it. Once you are confident that the function is working, find out how many
presents are on day 12 (write it down).
Add another function in the same file, called presentsThroughDay
.
Don't forget to include the correct contract and docstring.
Your function should again have a
single parameter, this time indicating the last day number you are interested in. For example, if you passed in
the number 12, it would indicate that you were interested in all days up to and including day 12. The function should
return the total number of presents, added up across all the days. You should again use the accumulator pattern to do this.
Each time your function needs to find the number of presents on a particular day, it should
use presentsOnDay
.
Make sure your presentsThroughDay
function produces the right answers if you ask it about just the
first day, or the first two days, or the first three days. Fix any errors you discover. Then find out the total
number of presents my true love gave me over the full 12-day Christmas season (write it down).
One question about an algorithm is whether it produces the correct result. But another question is how well it
scales up to larger problem sizes. My true love actually is generous enough to give me presents even outside
the Christmas season. Get a sense for how well the straightforward algorithm using two accumulations can accommodate
this by trying presentsThroughDay
out with values such as 10, 100, and 1000. (For
example, my parents have been married for over 12775 days.) How far do you have the
patience to go? If a computation is taking too long, you can stop it using the control-c key combination.
In the project2.py
file window, select your presentsOnDay
function definition by dragging
the mouse through it. Then use the "Comment Out Region" command from the Format menu, or equivalently the
control-3 key combination. This should put ##
characters at the beginning of each line. In
Python, anything from a #
character to the end of the line is a "comment," that is, not an actual
part of the program that will get run. So effectively you have removed this function definition by turning it
into nothing but comments. The advantage is that it is still visible and can be uncommented if you ever want
it back. Now add the following definition to the file:
def presentsOnDay(day): return day*(day+1)//2
Now if you go back to trying presentsThroughDay
with large numbers of days, you should find that
it scales up much better but still gives the same answers . With the new version, how large a number of days do
you have patience for?
Finally, in the template plain text file project2a.txt we have provided for you, write down the following under the corresponding question (Task 2, question 7):
There are N
people in a classroom, where N
is at least 0.
At the beginning of class,
each person shakes hands with each of the other people.
For example, if there is only one person, then no handshake takes place.
If there are two people, only one handshake takes place.
If there are three people, three handshakes happen.
Directions: First, with your partner, think about how you would compute this by hand.
Draw pictures and compute the number of handshakes for small values of N
(such as 4,5,6, etc).
Afterwards, if you wish, you can Google this question.
Finally, you can try our hint, which is to read Section 1.4: Problem-Solving Strategies (pages 6-9) in our textbook.
Write the contract, docstring, and implementation of two function handshakesNum1
and handshakesNum2
.
Each function returns the number of handshakes that take place
in a classroom with N
people.
Each function has one parameter N
.
Assume that the user will provide an input that is a non-negative integer.
The first function handshakesNum1
should use the accumulator pattern
(see Sec 2.5.1, pages 54-56).
The second function handshakesNum2
should use a different method. If neither of you are sure what other method to use, see the hints above, and ask your lab instructor.
Save your two functions handshakesNum1
and handshakesNum2
in the same file project2.py
. For each of the two functions, include the contract and docstring.
Use IDLE's "Run Module" command from the Run menu, or equivalently press the F5 key. If IDLE reports any syntax
errors in your program, fix them. When you get past that hurdle, use the Python shell window of IDLE to try your
handshakesNum1
and handshakesNum2
functions out for input parameter 0, 1, 2, 3, and 6 people. Make sure your function returns the correct number of handshakes that happen; if not, fix it.
Once you are confident that the function is working, find out how many people are in your classroom, and how many handshakes would happen (write it down).
Before submitting your work, open the following template project2template.py.
Copy and paste the commented out information (name, credit, description, etc.) to the top of your project2.py
file and complete the information. These should be included at the top of every submitted files.
You will be submitting your code using Moodle; click on the following link for instructions on submitting code using Moodle. For this project, exactly one of you will submit the following 2 files, a python file and a plain text file with the following required names and extensions:
Please make sure that exactly one of you submit the project on Moodle. If both of you submit the project on Moodle, we will mistakenly think that you are both plagiarizing, and this may cause much confusion.
Before submitting,
make sure that your .py
file project2.py
runs without error on IDLE 3 using one of the Olin 326 machines.
Make sure that your .txt
file project2a.txt
is a plain text file without any rich-text formatting.
You must give your files these exact names and extensions
(pay attention to the all lower case letters and the lack of white spaces).
If you are not sure, you can ask your lab instructor.
We will use this project2gradesheet.pdf when grading your lab.