0% found this document useful (0 votes)
9 views5 pages

Tutorial 9 Worksheet: COMP1117A Computer Programming I 2018-2019

The document outlines exercises for a computer programming tutorial focused on searching algorithms in Python, specifically linear and binary search. It includes code examples for implementing these searches, evaluating their performance based on the number of comparisons made, and visualizing the search process. Additionally, it covers sorting a list of friends' information by birthday.

Uploaded by

mikewaics
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

Tutorial 9 Worksheet: COMP1117A Computer Programming I 2018-2019

The document outlines exercises for a computer programming tutorial focused on searching algorithms in Python, specifically linear and binary search. It includes code examples for implementing these searches, evaluating their performance based on the number of comparisons made, and visualizing the search process. Additionally, it covers sorting a list of friends' information by birthday.

Uploaded by

mikewaics
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

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

Exercise 9.1 Analysing linear search

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.

def LinSearch(item, candidates):


for element in candidates:
if element == item:
return True
return False

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.

def countLinSearch(item, candidates):


count = 0
for element in candidates:
count += 1
if element == item:
return count
return count

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.

print(countLinSearch(3, [1, 2, 3, 4, 5]))

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?

Test case Input Output

Sorted List 1,2,3,4,5 3.0


(found) 1,3,2,4,5

Sorted List 1,2,3,4,5 5.0


(not found) 6,7,8

Exercise 9.2 Analysing binary search

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.

def binSearch(item, candidates):


if len(candidates) > 0:

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.

def countBinSearch(item, candidates):


count = 0
if len(candidates) > 0:
mid = len(candidates)//2
count += 1
if item == candidates[mid]:
return count
...
return count

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.

if item < candidates[mid]:


return count + countBinSearch(item, candidates[:mid])
else:
return count + countBinSearch(item, candidates[mid+1:])

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()

// Fill in the remaining parts yourselves

Once you are done, answer the following questions:

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?

Test case Input Output

Sorted List 1,2,3,4,5 2.2


(found) 1,3,2,4,5

Sorted List 1,2,3,4,5 2.0


(not found) 6,7,8

Exercise 9.3 Visualizing Binary Search

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.

Test case Input Output

Found 3,1,5 3
1 1
Yes

Not Found 3,1,5 3


6 5
No

Exercise 9.4 Sorting friend list

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.

Test case Input Output

In order 5 Amy 1/1


Amy Bob 4/2
1/1 Can 17/3
Bob Doreen 5/4
4/2 Erica 9/9
Can
17/3

4
Doreen
5/4
Erica
9/9

Reversed 3 Kai 31/1


order June May 5/5
6/6 June 6/6
May
5/5
Kai
31/1

You might also like