Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

def memoizeLSS ( a ) : T = { } # Initialize the memo table to empty dictionary # Now populate the entries for the

def memoizeLSS(a):
T ={} # Initialize the memo table to empty dictionary
# Now populate the entries for the base case
n = len(a)
for j in range(-1, n):
T[(n, j)]=0 # i = n and j
# Now fill out the table : figure out the two nested for loops
# It is important to also figure out the order in which you iterate the indices i and j
# Use the recurrence structure itself as a guide: see for instance that T[(i,j)] will depend on T[(i+1, j)]
# your code here
return T
def lssLength(a, i, j):
assert False, 'Redefining lssLength: You should not be calling this function from your memoization code'
def checkMemoTableHasEntries(a, T):
for i in range(len(a)+1):
for j in range(i):
assert (i, j) in T, f'entry for {(i,j)} not in memo table'
def checkMemoTableBaseCase(a, T):
n = len(a)
for j in range(-1, n):
assert T[(n, j)]==0, f'entry for {(n,j)} is not zero as expected'
print('-- Test 1--')
a1=[1,4,2,-2,0,-1,2,3]
print(a1)
T1= memoizeLSS(a1)
checkMemoTableHasEntries(a1, T1)
checkMemoTableBaseCase(a1, T1)
assert T1[(0,-1)]==4, f'Test 1: Expected answer is 4. your code returns {T1[(0,-1)]}'
print('Passed')
print('--Test2--')
a2=[1,2,3,4,0,1,-1,-2,-3,-4,5,-5,-6]
print(a2)
T2= memoizeLSS(a2)
checkMemoTableHasEntries(a2, T2)
checkMemoTableBaseCase(a2, T2)
assert T2[(0,-1)]==8, f'Test 2: Expected answer is 8. Your code returns {T2[(0,-1)]}'
print('--Test3--')
a3=[0,2,4,6,8,10,12]
print(a3)
T3= memoizeLSS(a3)
checkMemoTableHasEntries(a3, T3)
checkMemoTableBaseCase(a3, T3)
assert T3[(0,-1)]==1, f'Test 3: Expected answer is 1. Your code returns {T3[(0,-1)]}'
print('--Test4--')
a4=[4,8,7,5,3,2,5,6,7,1,3,-1,0,-2,-3,0,1,2,1,3,1,0,-1,2,4,5,0,2,-3,-9,-4,-2,-3,-1]
print(a4)
T4= memoizeLSS(a4)
checkMemoTableHasEntries(a4, T4)
checkMemoTableBaseCase(a4, T4)
assert T4[(0,-1)]==14, f'Text 4: Expected answer is 14. Your code returns {T4[(0,-1)]}'
print('All tests passed (7 points)') Problem 1 : Longest Stable Subsequence
Consider a list of numbers a0,a1,cdots,an-1. Our goal is to find the the longest stable subsequence: ai1,ai2,cdots,aik which is a sub-list of the original list
that selects elements at indices i1,i2,dots,ik from the original list such that
aij+1-1aijaij+1+1|aij+1-aij|1+-1k1,4,2,-2,0,-1,2,31,0,-11,2,2,34,31,2,2,3na0,dots,an-1(i,aj)ai,dots,an-1aiai1|ai1-aj|10.0in.i=najai,dots,an-1|a-aj|1aj=aj Part 2: Memoize the Recurrence
Construct a memo table as a dictionary that maps from (i,j) where 0in and LSSLength(a,i,aj)aj=a[j]j0aj=(n2)-1jto the value LSSLength(a,i,aj) where aj=a[j]
ifj0 else aj= None.
Your code should run in worst case time (n2).
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions