Tutorial 9 Worksheet: COMP1117A Computer Programming I 2018-2019
Tutorial 9 Worksheet: COMP1117A Computer Programming I 2018-2019
Tutorial objectives:
- To practice searching in Python.
Tutorial 9 Exercises Deadline: Dec 6, 2018 14:20
We are going to write a program that reads two lists of integers and then finds all the items of the second
list in the first list using linear search. We are then going to report the number of comparisons made (i.e.,
count how many time the == operator was executed).
Consider the following function that will use linear search to find an item in a list.
In this exercise, we will evaluate the performance of linear search based on the number of comparison
performed. To do this, we change the seqSearch() function so that it returns the number of comparisons
made.
We define a variable that counts the number of comparisons. Then increment the count before the
comparison is performed. Finally we will return the count. To test this function, we search the value 3 in
the list [1, 2, 3, 4, 5] as follows.
linear search will find the value 3 in the list [1, 2, 3, 4, 5]. In this case the output of the program will be 3,
i.e., there were three comparisons made.
1
Next, let’s read a list of integers from the input.
haystack = []
for item in input().split(','):
haystack.append(int(item))
The split() function is used to split a comma separated list, then a for-loop converts each string to an
integer and add it to a list. The same is done to a second list.
needles = []
for item in input().split(','):
needles.append(int(item))
Finally, to complete the task, we search each item of the second list in the first list and calculate the
average number of comparisons performed.
sum = 0
for needle in needles:
sum += countLinSearch(needle, haystack)
print(sum / len(needles))
With the program completed, you can try different input to evaluate the performance of the linear search
algorithm. Try to answer the following questions:
● What is the average number of comparisons needed for a successful search in a list of size N?
● What is the average number of comparisons needed for an unsuccessful search in a list of size N?
We are going to write a program that reads two lists of integers and then finds all the items of the second
list in the first list using binary search. We are then going to report the number of comparisons made (i.e.,
count how many time the == operator was executed).
Consider the following recursive function that will use binary search to find an item in a list.
2
mid = len(candidates)//2
if item == candidates[mid]:
return True
if item < candidates[mid]:
return binSearch(item, candidates[:mid])
else:
return binSearch(item, candidates[mid+1:])
return False
We are going to repeat what we have done in the previous exercise. We first modify the function to return a
count instead of True / False. There are quite a number of return statements this time and it is going to be a
bit more difficult than before.
First, for the case where we return True or False, only one comparison (==) was performed.
The remaining two cases are recursive. The count depends on the result of the recursive call of the
function. We need to add one count to the result.
Now you should be able to complete the remaining parts. Notice that when you read the first list of values,
you have to sort them.
haystack = []
for item in input().split(','):
haystack.append(int(item))
haystack.sort()
3
● What is the average number of comparisons needed for a successful search in a list of size N?
● What is the average number of comparisons needed for an unsuccessful search in a list of size N?
Read a list of numbers in a comma-separated list then store them in a sorted list. Read another value from
input and perform binary search on the sorted list. Output each of the values checked in the process.
Output “Yes” once the value is found and “No” at the end of the search if the value is not found. Try to
pass the following test cases.
Found 3,1,5 3
1 1
Yes
Read a number n, then the user will input n friends’ information. The information contains a name and
followed by the birthday (in the format of day/month). Print the names followed by the birthday in
ascending order of birthday.
4
Doreen
5/4
Erica
9/9