CSE 2050 Exam 2 - Recursion, Sorting, and Hashing with Correct Verified Answers| Latest 2024
5 views 0 purchase
Course
CSE 2050
Institution
CSE 2050
emoization - ANSWER Write the recursive function top-down.
Alter the function to check if we've already calculated the value.
If so, use the pre-calculated value,
If not, do the recursive call.
Fib - Memo - ANSWER def fib(n, fib_cache):
if n in fib_cache:
return fib_cache[n]
fib_cache[n] = f...
CSE 2050 Exam 2 - Recursion, Sorting, and Hashing with Correct Verified Answers | Latest 2024 Memoization - ANSWER Write the recursive function top -down. Alter the function to check if we've already calculated the value. If so, use the pre -calculated value, If not, do the recursive call. Fib - Memo - ANSWER def fib(n, f ib_cache): if n in fib_cache: return fib_cache[n] fib_cache[n] = fib(n -1, fib_cache) + fib(n -2, fib_cache) return fib_cache[n] Memoization Pros - ANSWER Can be more intuitive to code / understand. Can be memory saving if you don't need answers to all subproblems. Memoization Cons - ANSWER Depending on implementation, larger overhead due to recursion. Tabulation - ANSWER Bottom -Up. Solve smaller problems first. Then bigger problems. ... Then finally solve the real problem. Fib - Tab - ANSWER def fib(n): f = [] f.append(1) f.append(1) for i in range(2, n + 1): f.append(f[i -1] + f[i -2]) return f[n] Linear Search - ANSWER Look for an item x in a sorted list. Approach: Step through a n array of items one at a time. Look for the item x. Search stops when... Item x is found. Or when search has examined all items and x is not found. Running Time: O(n) Binary Search - ANSWER Classic, recursive algorithm. If you are looking for an item in a sorted list, you break the list in half and repeat the search on whichever side you could contain the missing element, which can be found by comparing the median element. Then, repeating on the smaller list is just a single recursive call. Binary Se arch Poor Implementation - ANSWER def bs(L, item): if len(L) == 0: return False
The benefits of buying summaries with Stuvia:
Guaranteed quality through customer reviews
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller Examsplug. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for $12.49. You're not tied to anything after your purchase.