Assignment 3-2
Assignment 3-2
Instructions
1. Submit one .ipynb file containing all answers. The name should be
[student name]_assignment3.ipynb
2. Write questions in separate text blocks in Jupyter Notebook before the code blocks containing
answers.
3. Read the questions carefully before answering. It must be followed if a question asks to follow a
particular approach or use a specific data structure.
1. Write a Python program that takes a list of integers and a list of target integers as input from
the user. Use linear search to find all occurrences of each target integer in the list. The program
should return a dictionary where the keys are the target integers and the values are lists of
2. Implement the binary search algorithm to find the index of the target integer in the list.
a. Print whether the target is found and its index or a message if it’s not found.
b. Modify the above binary search function to find the closest value to a target integer in a
sorted list. Print the closest value and its index. 0.5
3. Write a program that receives an input array of distinct numbers in the range [1, n] and outputs
the single number in the range that is absent from the array.
5. Write a program that sorts a list of integers using bubble sort with an optimization that exits
early if no swaps are made during a pass. Print the number of passes made before sorting is
complete. 0.5
6. Implement a program that inputs a list of integers and an integer k. Use bubble sort to sort the
list and then find and print the kth largest element. Handle the case where k is greater than the
7. Given a semi-sorted array, find out how many elements need to change their position to make