# Python代写 | COMP1730/COMP6730 – Programming for Scientists Practice Programming

COMP1730/COMP6730 – Programming for Scientists Practice Programming

Question 1: 3 Marks
Let X = x1, x2, . . . , xn be a sequence of numbers. The average or mean of the sequence is deﬁned as
n
i=1
xi
n .
Question 1: Write a function count_less_than_average(x): that returns the number of elements in x that are
(strictly) less than the average of x.
For example:
• If x = [1, 2, 3, 4, 5], count_less_than_average(x) should return 2, since the average of x is 3 and there
are two elements in x less than 3.
• If x = (1, 2, 3, 4, 100), count_less_than_average(x) should return 4, since the average of x is 22 and
there are four elements in x less than 22.
• If x = [], count_less_than_average(x) should return 0 since the average of an empty sequence is undeﬁned.
A skeleton ﬁle called problem1.py is provided (it will be on the desktop). You should write your solution into this
ﬁle.
• You can assume that x is a sequence (e.g. a list, tuple, etc.) of numbers.
• You can not assume that x is non-empty.
• The function must return an integer (type int) between 0 and the length of x.
• The function must not modify the argument sequence.
• You must write your solution in the ﬁle called problem1.py found on the desktop.
• The function that you write must be named count_less_than_average and it must take a single parameter.
• Remember that your solution ﬁle must contain only function deﬁnitions, comments, and optionally import
statements. If it contains anything else, it is invalid and you will receive zero marks. If you run the testing
program, it will tell you if your ﬁle is invalid.
Using the testing program
The testing program for this problem is called test_problem1.py. It is also found on the desktop. To run the testing
program, you only need to run test_problem1.py.
The program will test the implementation of the function named count_less_than_average in the ﬁle problem1.py
(in the same directory). You should not make any changes to the testing program.
Levels
This question does not have multiple levels. E-mail: vipdue@outlook.com  微信号:vipnxx 