Assignment 1: Problem Solving Example

Unit 1 > Assignment 1 > problem solving


Let's start with some visualization:

 

A1.1 Exercise: Write up the above in my_demo_problem1.py. Test it with different lengths to make sure you print all applicable start positions. Justify your code in your assignment1.pdf.
 

 

How do we compare the triple beginning at i in the first list against the triple beginning at j in the second list?

 

A1.2 Exercise: Complete the other conditions in the if-statement in my_demo_problem2.py. And also complete the print statement.
 

Finally, we need to account for the case when no such triple is found.
 

A1.3 Exercise: When do we know for sure that no such triple exists? Explain how you solved the latter issue (no such triple found) in assignment1.pdf. Complete the whole program in my_demo_problem3.py by including the above ideas in the code needed for the function search_sublist():

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 = [9, 11, 13, 14, 15]

# This should print: Found: at i=2 in X and j=1 in Y
search_sublist(A, B)

# This should print: No 3-element sublist found
search_sublist(A, C)

# This should print: Found: at i=4 in X and j=0 in Y
search_sublist(A, D)
  



© 2020, Rahul Simha