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.
Note: the solution will include some exercises
for you that you will need to submit.
Assignment problems
Demo problem.
Consider these two lists:
A = [1, 3, 5, 7, 9, 11, 13]
B = [2, 5, 7, 9, 10, 11]
C = [10, 2, 3, 5, 7, 9, 13]
D = [3, 5, 7, 9, 11, 13]
Notice that the sub-list 5,7,9 in the first list appears
in the second list whereas there is no 3-element sub-sequence in
list A that appears in list C.
The goal of this problem is to write a function that
prints the start position in each list for the first 3-element
sublist found in both, going left to right:
def search_sublist(X, Y):
# Determine whether and where a 3-element sub-list of X exists in Y.
A = [1, 3, 5, 7, 9, 11, 13]
B = [2, 5, 7, 9, 10, 11]
C = [10, 2, 5, 7, 15, 13]
D = [3, 5, 7, 9, 11, 13]
# This should print: Found: at i=4 in X and j=3 in Y
search_sublist(A, B)
# This should print: No 3-element sublist found
search_sublist(A, C)
# This should print: Found: at i=1 in X and j=0 in Y
search_sublist(A, D)
Such sublist searching is a common operation in dealing with DNA
sequences in biology (except that their sequences have letters and not
numbers).
At this point, do not read further and try to address the following:
First understand what is being asked.
Do you see loops and if so, how would they range over the lists?
Can the problem be broken down into parts, where you can solve
the parts and put the solution together afterwards?
Try writing some code to get at least some of the output.
Don't forget to submit your solutions to the exercises within.
In this problem, you examine a list of strings and identify whether
the list has composite words consisting of a concatenation of
words already in the list. For example, consider:
X = ['hithere', 'hello', 'world', 'hi', 'welcome', 'helloworld', 'there', 'hiya']
We can see that there are two composites. One is:
X = ['hithere', 'hello', 'world', 'hi', 'welcome', 'helloworld', 'there', 'hiya']
The other is:
X = ['hithere', 'hello', 'world', 'hi', 'welcome', 'helloworld', 'there', 'hiya']
In this problem, you will write code in
my_composites.py
to complete the function below:
def find_composites(A):
# Write your code here
X = ['hithere', 'hello', 'world', 'hi', 'welcome', 'helloworld', 'there', 'hiya']
Y = ['house', 'hold', 'houseboat', 'oat']
find_composites(X)
find_composites(Y)
The output should be:
Composite words found:
hello world helloworld
hi there hithere
No composites found
In this problem, you will write code for two functions
in the file
my_best_second.py
def second_largest(A):
# Write your code here
def best_second(A, B):
# Write your code here
X = [1, 3, 5, 7, 9]
print(second_largest(X)) # Should print 7
Y = [9, 3, 8, 5, 2]
print(second_largest(Y)) # Should print 8
print(best_second(X,Y)) # Should print 8
The first function returns the second-largest element
in the given list. The second function takes two lists, compares
their second-largest elements and returns the larger of the two.
The following shows a plot of the sine function:
Note: the x coordinate ranges between 0 and 3.141 (approximately π)
while the y value is sin(x). It so happens that sin(x) is
a value between 0 and 1 in this case.
What we'd like to do is calculate the area below the
curve.
That is, the area of the shape whose one boundary is the curve, and
the other boundary is the x-axis. We will do this in an unusual way:
We'll generate n random points, where the x value is
randomly selected between 0 and 3.141, and the y value is
randomly selected between 0 and 1.
Then, for those points that lie below the sin(x) function,
we'll color them green. All other points will be colored yellow.
This is what it'll look like:
The ratio of the number of green points to the total number
of points (green plus yellow) should be a pretty good approximation
of the ratio of the area under curve to the area of the whole rectangle
(with sides 3.141 and 1).
Your goal is to do the generation and count. How does one generate
a random value within a particular range? Use
x = random.uniform(0, 3.141)
to generate a random x value in the range from 0 to 3.141.
Find a TED talk that relates to computing or computer science.
In your
assignment1.pdf,
write the title, include the URL, a little about the speaker,
and a one paragraph summary. Add a second paragraph with your
own views on the topic.
A1.4 Audio:
How to submit:
Write all your programs and assignment1.pdf
in a directory called assignment1.