As usual, in each such assignment set, we will help develop your
problem-solving skills by showing you how to solve one
problem, the first and often hardest problem.
However, the solution will include some exercises
for you that you will need to submit.
Assignment problems
Demo problem.
Consider a number like 14. We're going to iteratively divide by 2
using integer division as follows:
14 // 2 = 7
7 // 2 = 3
3 // 2 = 1
Here, it took three successive "divides" by 2 for 14 to reach 1.
In your
assignment2.pdf,
work through how many divides it takes for 1000.
For the programming part, we will prompt the user for
an integer and count the number of divides. In fact, we will
keep prompting the user for integers, computing and reporting
the number of divides for each, until the user decides "enough".
When the user types a negative number to indicate "enough"
we stop prompting. Here is sample output:
Enter an integer: 14
Number of divides for n=14: 3
Enter an integer: 64
Number of divides for n=64: 6
Enter an integer: 33
Number of divides for n=33: 5
Enter an integer: -1
Thank you!
Here, the user first entered 14 (resulting in 3 divides),
then entered 64 (resulting in 6 divides), then 33 (resulting in 5
divides), -1 to stop.
There are two while-loops at work, one in iterating over
user input, and the other in iterative divides.
At this point, do not read further and try to
write down pseudocode to solve the problem.
Don't forget to submit your solutions to the exercises within.
Let's use a while-loop to perform the most basic type of
compound-interest calculation. Suppose you start with $1,000
as your principal and that each year you accumulate
10% in interest. Then, after the first year, you would
have $1,100 (since 100 is 10% of 1000). The next year you would
add 10% of $1,100 to $1,100. What we'd like to know is:
how many years does it take for this compounding process
to reach a desired target of $10,000?
Write code
below to perform this calculation:
def compute_num_years(annual_rate, principal, target):
# Write your code here
n = compute_num_years(10, 1000, 10000)
print('num years =', n) # Should print 25
n = compute_num_years(8, 1000, 10000)
print('num years =', n) # Should print 30
Write your code in
my_compound_interest.py.
We've used wordtool before to loop through nouns and verbs.
For example, let's print the first 10 nouns:
words = wordtool.get_nouns()
for i in range(10):
print(words[i])
Consider the following:
def has_all_vowels(w):
# Write code here to determine whether w has all 5 vowels
words = wordtool.get_nouns()
position = 0
# Write a while-loop here to stop at the first noun that
# has all 5 vowels
print('Found', words[position], 'at position', position)
# Should print:
# Found argumentation at position 456
Thus, in
my_has_all_vowels.py
your goal here is to write code for the function
has_all_vowels() so that it returns True or False, and then
write a while loop that tries nouns in sequence until you
find the first noun that has all vowels.
In your
assignment2.pdf,
report on the first verb that has all vowels.
You will
need wordtool.py
and wordsWithPOS.txt.
This next problem is about processing data from text files.
To start, download
drawtool.py
and
my_rectangles.py,
and then read through the latter.
Then download these three files:
rectangles1.txt,
rectangles2.txt,
rectangles3.txt
as well. Examine the data files to see that each line
except the first
has data about a single rectangle: the x,y coordinates
of the bottom left corner, and the width and height.
For example, the first file is:
2
1 1 2 3
5 5 3 2
This indicates 2 rectangles. The first rectangle has its bottom left
corner at (1,1), has width 2 and height 3.
Now run the
my_rectangles.py
program. Then, in the program, change
rectangles1.txt
to
rectangles2.txt.
And once again to
rectangles3.txt.
You can then look at the files and confirm that you are
indeed seeing the rectangles in those files. You should also
see some rectangles overlap (intersect) with others, while
some rectangles are entirely isolated.
Now for the assignment. Your goal is to identify which
rectangles intersect another, and to draw them in red.
You'll see that all you have to write is the code that
determines the intersections.
Note: use while-loops instead of for-loops in your part of the code.
When you use the rectangles1.txt, you should see:
With rectangles2.txt, you should see:
With rectangles3.txt:
By now you've heard the term programmer, possibly
even software developer. Identify at least 5 more such
job titles that relate to computer programming. Then,
in your
assignment2.pdf,
write 1-2 sentences for each, so that they are all distinguished
from other each other.
A2.6 Audio:
How to submit:
Write all your programs and assignment1.pdf
in a directory called assignment1.