Uploaded by Alvaro Leandro

CW211 (4)

advertisement
Coursework 1. List, matrices, and recursion.
Submission deadline 12/11/21
1
Student marks
Throughout this question M ARKS is a matrix whose rows correspond to students
and columns correspond to modules (you can think of indices of rows and
columns as ID numbers of students and modules respectively). M ARKS[i][j]
is the mark of student i for module j. The mark may be −1: this means that
student i has not taken module j.
Please solve the following questions.
1.1
Student averages (5 marks)
Write a function ST Averages whose input is M ARKS. The function should
return the list of averages of students on modules they have taken (excluding
−1). If a student has not taken any module the average should be −1 (to avoid
zero division).
1.2
Module averages (5 marks)
Write a function M ODAverages whose input is M ARKS. The function should
return the list of averages of modules excluding students that got −1 for the
respective module. If no student has taken a particular module the average
should be −1.
1.3
Possibility of graduation (5 marks)
Let COM P U LSORY be a list of compulsory modules. If M ARKS has m
columns then COM P U LSORY contains a subset of numbers in the range
0, . . . , m − 1. Assume that a student can graduate if s/he has taken all the
compulsory modules and received a mark at least 40 and, in addition, passed
a least 3 optional (non-compulsory) modules with marks at least 40. Write a
function CanGraduate whose inputs are M ARKS, COM P U LSORY and a
student ID number s (0 ≤ s ≤ len(M ARKS)). The function returns true if the
student can graduate according to the above conditions and f alse otherwise.
1
1.4
List of graduates (5 marks)
Let N AM ES be the list of names of the students. That is len(N AM ES) =
len(M ARKS) and N AM ES[i] is a string whose content is the name of the
student with ID number i. Write a function GraduatesList whose input is
M ARKS, COM P U LSORY , and N AM ES. The function should print the list
of all graduates along with their respective averages.
2
2.1
List within list
Iterative version (10 marks)
Write a function F irstInSecond(A, B) where A and B are lists of integers.
The function should return true if every element of A also occurs in B and
f alse otherwise. For example the function the function should return true for
A = [3, 3, 2, 4, 4, 4] and B = [2, 3, 4, 1] and f alse in case 5 is appended to A.
The function should use loops only, no recursion.
2.2
Recursive version (10 marks)
Write a function F irstInSecondRec(A, b) having the same specification as the
function in the first part of this question except that it has to use recursion
only and not to use loops.
3
3.1
Matrix row without repetitions
Iterative version (10 marks)
Write a function IsN onrepeatedRow. The input of this function is a matrix A.
The function should return true if A has a row with no multiple occurrence of
the same element and f alse otherwise. The function should use loops only, no
recursion.
3.2
Recursive version (10 marks)
Write a recursive version of the function with input and output as specified in
the first part of this question.
4
A set of hubs (20 marks)
In this and the next questions, we consider an n × n matrix Distances of
distances between cities. The cities correspond both to rows and column. For
the purpose of this and the next questions, the cities are denoted not by names
but by numbers 0, . . . n − 1. The rows and columns of Distances correspond
to cities and each element of the matrix is the distance in miles between the
2
city corresponding to the row of the element and the city corresponding to the
column of the element.
Let us say that a subset S of cities is a hub if each city x is at distance at
most 10 miles away from some city in S.
For example, for the matrix below, {1, 2} is a hub while {0, 1} is not.
[[0,10,100,100],
[10,0,100,100],
[100,100,0,10]
[100,100,10,0]]
Write a function IsHub(Distances, S) where S is a subset of {0, . . . , n − 1}.
The function should return T rue if S is a hub and F alse otherwise.
5
Remote cities
With the same data as in the previous question, let us say that a city x is
remote if it is at least 100 miles away from any other city.
Please answer the following questions.
• A remote city is necessarily included in any hub. Why? (5 marks)
• Write a function N umRemote(Distances) that returns the number of
remote cities. (15 marks)
3
Download