Question

Write a program (a3.py) that has 3 function definitions: magic-sequential(), magic-binary(), and main(). You will write...

Write a program (a3.py) that has 3 function definitions: magic-sequential(), magic-binary(), and main().

  • You will write the code for the above functions.
  • Both magic functions (magic-sequential() and magic-binary()) will look for a magic index in a given sorted list of distinct integers.
    • A magic index in a list myList[0 ... n-1] is defined to be an index i such that myList[i] = i .
    • Both functions receive the list as a parameter and return an integer: either the magic index, if one exists, or -1 if a magic index does not exist in the list. If more than one magic index exist, return the first one found.
  • The function magic-sequential() must use sequential search to traverse the list.
  • The function magic-binary() must use binary search to find the magic index.
    • You should not use any built-in python function or method to perform the search - you must write the logic for the search algorithm.
      • It's okay to use len(), print(), input(), int(), range(), append().
  • The function main() must prompt the user to enter unique integers, in ascending order. Add these numbers to a list and then call both count functions passing the same list as a parameter. Print the results from both function calls.
  • At the bottom of a3.py (after the three function definitions), call main() to execute the code.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:


def magic_sequential(l):
for i in range(len(l)):
if(l[i]==i):
return i
return -1

def magic_binary(l):
start = 0
end = len(l)-1
  
while(start<end):
mid = int((start+end)/2)
if(l[mid]==mid):
return mid
elif(l[mid]>mid):
end = mid
else:
start = mid+1
return -1

def main():
n= int(input("Enter the number of integers to be entered in ascending order: "))
l=[]
while(n>0):
k = int(input("Enter number: "))
l.append(k)
n = n-1
  
print(magic_sequential(l))
print(magic_binary(l))
  
main()

Output:

PLEASE UPVOTE IF YOU FOUND THIS HELPFUL!

Add a comment
Know the answer?
Add Answer to:
Write a program (a3.py) that has 3 function definitions: magic-sequential(), magic-binary(), and main(). You will write...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that meets the following criteria: Define a function that implements the selection sort...

    Write a program that meets the following criteria: Define a function that implements the selection sort algorithm to sort an integer array. Define a function that implements the binary search algorithm to search an array for a given integer. The function must return the location of the integer if it is found or a -1 if it is not found. The main function in your program must declare an integer array initialized with 10 unsorted values. The main function must...

  • Write a complete C++ program that is made of functions main() and rShift(). The rShift() function...

    Write a complete C++ program that is made of functions main() and rShift(). The rShift() function must be a function without return with 6 parameters. rShift() should do following. Shift the first 4 formal parameters' value one place to right circularly and send the updated values out to the caller function, i.e. main. Furthermore, after calling this function with first 4 actual parameters, say a1, a2, a3, a4, and these actual parameters should shift their value one place to right...

  • Language = c++ Write a program to find the number of comparisons using the binary search...

    Language = c++ Write a program to find the number of comparisons using the binary search and sequential search algorithms as follows: o Suppose list is an array of 1000 elements. o Use a random number generator to fill the list. o Use the function insertOrd to initially insert all the elements in the list. o You may use the following function to fill the list: void fill(orderedArrayListType& list) {       int seed = 47; int multiplier = 2743;                                ...

  • Requirements Write functions isMemberR, a recursive function, and isMemberI, which will use an iterative approach, to...

    Requirements Write functions isMemberR, a recursive function, and isMemberI, which will use an iterative approach, to implement a binary search algorithm to determine whether a given element is a member of a given sequence Each function will have two parameters, aseq, a sorted sequence, and target. isMemberR and isMemberI will return True if target is an element of the sequence, and False otherwise. Your implementations must implement the binary search algorithm described above. When function i sMemberR recursively invokes itself,...

  • Write a complete C++ program that at least consists of the main() function and a recursive...

    Write a complete C++ program that at least consists of the main() function and a recursive function gcd() with return value. Define function gcd() with two and only two parameters that calculates the greatest common divider of two given parameters. Hint: use the difference between two parameters or the remainder obtained using one parameter divide the other. In main() Read 2 positive integers with proper prompt. Call gcd() with proper syntax. Display the result, i.e. the greatest common divider of...

  • 1. Write a recursive function that returns the sum of all even integers in a LinkedBinaryTree. Yo...

    please explain each line of code! ( in python ) 1. Write a recursive function that returns the sum of all even integers in a LinkedBinaryTree. Your function should take one parameter, root node. You may assume that the tree only contains integers. You may not call any methods from the LinkedBinaryTree class. Specifically, you should traverse the tree in your function def binary tree even sum (root): Returns the sum of al1 even integers in the binary tree 2....

  • Write a C++ function binsearch that carries out the binary search algorithm on a sorted array...

    Write a C++ function binsearch that carries out the binary search algorithm on a sorted array of integers. Your function takes as parameters an array of integers (sorted in increasing order), the size of the array, and a target integer to search for. The function returns the index of the target in the array, and returns -1 if the target is not in the array. The file main1.txt on the webpage contains code that generates a specific array of integers...

  • Program: Write a complete C++ program that is made of functions main() and rShift(). The rShift()...

    Program: Write a complete C++ program that is made of functions main() and rShift(). The rShift() function must be a function without return with 6 parameters. rShift() should do following. Shift the first 4 formal parameters' value one place to right circularly and send the updated values out to the caller function, i.e. main. Furthermore, after calling this function with the first 4 actual parameters, say a1, a2, a3, a4, and these actual parameters should shift their value one place...

  • This program should use a main function and two other functions named playlist and savelist as...

    This program should use a main function and two other functions named playlist and savelist as follows: The main function: The main function should create an empty list named nums and then use a loop to add ten integers to nums, each integer in the range from 10-90. NOTE: In Python, a list is a data type. See Chapter 7. The main function should then call the playlist function and savelist function, in that order. Both of these functions take...

  • **IN C*** * In this lab, you will write a program with three recursive functions you...

    **IN C*** * In this lab, you will write a program with three recursive functions you will call in your main. For the purposes of this lab, keep all functions in a single source file: main.c Here are the three functions you will write. For each function, the output example is for this array: int array[ ] = { 35, 25, 20, 15, 10 }; • Function 1: This function is named printReverse(). It takes in an array of integers...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT