Algortimhs
Recursive Thinking) Carefully read the comment of the following function, then complete the function so that it correctly solves the problem. Verify your answer by writing down the three questions rule and answer the questions for your completed
/* Return true if there exist duplicate values in sublist a[first...last], i.e., there are a[i]==a[j] for some i,j where i is not equal to j
@param a: the
indices of list
@return return true if there are duplicate values in a[first...last],
otherwise return false */ bool ContainDuplicate (vector<int> a, int first, int last) {
//1. what should be the base case? What should be returned?
// general case if (ContainDuplicate (a, first+1, last)==true)
_______________;
//Finish the rest based upon comment:
// if a[first] does not equal to ANY value in a[first+1...last], // then return false;
// else return true
}
(Recursive Thinking) For the problem, think of a different way to solve it recursively. Write your algorithm in the space below, and verify your algorithm using the three-questions rule.
Answer:
I have done code and also I have attached code as well as output
Step by stepSolved in 4 steps with 1 images
- The goal is to rewrite the function, below, such that passes in a different list of parameters, particularly eliminating the need to pass low and high for each recursive call to binary_search. defbinary_search(nums,low,high,item): mid=(low+high)//2iflow>high:returnFalse #The item doesn't exist in the list!elifnums[mid]==item:returnTrue# The item exists in the list!elifitem<nums[mid]:returnbinary_search(nums,low,mid-1,item)else:returnbinary_search(nums,mid+1,high,item) The new function should be prototyped below. The number of changes between the given version, and the one requested is not significant. defbinary_search(nums,item):pass# Remove this and fill in with your code Tip: If you consider that high and low are used to create a smaller version of our problem to be processed recursively, the version requested will do the same thing, just through a different, more Pythonic technique.arrow_forwardModify the recursive Fibonacci function to employ the memoization technique discussed in this chapter. The function creates a dictionary and then defines a nested recursive helper function named memoizedFib. You will need to create a dictionary to cache the sum of the fib function. The base case of the fib function is the same as before. However, before making any recursive calls, the helper function looks up the value for the function’s current argument in the dictionary (use the method get, with None as the default value). If the value exists, the function returns it and skips any recursive calls. Otherwise, after the helper function adds the results of its two recursive calls, it saves the sum in the dictionary with the current argument of the function as the key. Note: The program should output in the following format: n fib(n) 2 1 4 3 8 21 16 987 32 2178309 ---------------------------------------------------------------------------------- def…arrow_forward1. Below, enter code to complete implementation of a recursive function sum allintegers(), which takes an input n and adds all integers preceding it, up to n: add all integers(n):arrow_forward
- Course: Algorithm Project: We will use the defintion of of n-Queens Problem from the chapter Backtracking. In this project you need to describe Problem and Algorithm and Indicate input and output clearly. Analyze and prove the time complexity of your algorithm. Implement the algorithm using backtracking(including writing testing case).illustrate key functions with comments indicating: What it does, what each parameter is used for, how it handles errors etc. Indicate the testing scenarios and testing the results in a clear way. Make sure source is commented appropriately and structured well.arrow_forwardGive a recursive definition for the set of all strings of a’s and b’s that begins with an a and ends in a b. Say, S = { ab, aab, abb, aaab, aabb, abbb, abab..} Let S be the set of all strings of a’s and b’s that begins with a and ends in a b. The recursive definition is as follows – Base:... Recursion: If u ∈ S, then... Restriction: There are no elements of S other than those obtained from the base and recursion of S.arrow_forwardPlease answer quick in pythonarrow_forward
- DO NOT COPY ANSWER FROM ANOTHER QUESTION LEAVE IF you cannot do it perfectly. Otherwise, I will not only DOWN the vote but also REPORT you to the official Chegg team.I won't stop calling Chegg until they really pay attention to this trash behavior. Write a structurally recursive function (prefix->postfix exp) that takes as input an expression in the same little language as Problem 4. prefix->postfix returns an expression of the same form as its input, except that all function applications have been reversed. Finish this Racket code below to answer the question above.(define prefix->postfix(lambda (exp)))arrow_forward‘Write a function nesting(), which takes an arbitrary number of parameters, and returns a list containing the arguments. Write another function unnesting(), which takes a list L as the parameter and retums a 1D list. Notice that, you should be able to use loops to solve thisproblem and should not use recursive functions (which we will not cover in this course). ‘Write assertions to test the two functions.For example nesting(1, nesting(2, 3, nesting(4, 5, [6])), [7, 8], 9)unnesting([1, [2, 3, [4, 5, [6]]], [7. 8], 9]) (1, [2, 3, [4, 5, [6]]], [7, 8], 9][1, 2,3, 4, 5, 6, 7, 8 9]arrow_forward•rewrite calculateSum function as a recursive function. m(i) = m(i-1) + i/(i+1), where i >=1arrow_forward
- Computer Networking: A Top-Down Approach (7th Edi...Computer EngineeringISBN:9780133594140Author:James Kurose, Keith RossPublisher:PEARSONComputer Organization and Design MIPS Edition, Fi...Computer EngineeringISBN:9780124077263Author:David A. Patterson, John L. HennessyPublisher:Elsevier ScienceNetwork+ Guide to Networks (MindTap Course List)Computer EngineeringISBN:9781337569330Author:Jill West, Tamara Dean, Jean AndrewsPublisher:Cengage Learning
- Concepts of Database ManagementComputer EngineeringISBN:9781337093422Author:Joy L. Starks, Philip J. Pratt, Mary Z. LastPublisher:Cengage LearningPrelude to ProgrammingComputer EngineeringISBN:9780133750423Author:VENIT, StewartPublisher:Pearson EducationSc Business Data Communications and Networking, T...Computer EngineeringISBN:9781119368830Author:FITZGERALDPublisher:WILEY