7. Functions (1)
7. Functions (1)
CSC1015F Assignment 7
Functions
Assignment Instructions
Previous assignments have involved solving programming problems using input and output
statements, 'if' and 'if-else' control flow statements, 'while' statements, ‘for’ statements, and
statements that perform numerical and string manipulation.
This assignment builds on these technologies and offers practice using functions and modules.
Functions are very effective when used in conjunction with a divide-and-conquer approach to problem
solving. An example on the use of functions is given in Appendix A.
NOTE Your solutions to this assignment will be evaluated for correctness and for the following
qualities:
• Documentation
o Use of comments at the top of your code to identify program purpose, author and
date.
o Use of comments within your code to explain each non-obvious functional unit of
code.
• General style/readability
o The use of meaningful names for variables and functions.
• Algorithmic qualities
o Efficiency, simplicity
These criteria will be manually assessed by a tutor and commented upon. In this assignment, up to
10 marks will be deducted for deficiencies.
Height
Diameter
1
The area, A, of a circle of diameter d is 𝜋𝑑 2 .
4
Page 1 of 11
Version 30/03/2023 10:39
Your program should consist of three functions. One called ‘circle_area’, another called
‘cylinder_volume’, and the third called ‘main’.
Here is a program skeleton:
import math
def circle_area(diameter):
# Your code here
def cylinder_volume(diameter, height):
# Your code here
def main():
# Your code here
if __name__=='__main__':
main()
The circle_area function has a diameter as a parameter. It will calculate and return the area
of the circle with that diameter.
The cylinder_volume function has a diameter and a height as parameters. It will calculate
and return the volume of the cylinder with that diameter and height. It will call the circle_area
function to obtain a value for A.
The main function will ask the user to input diameter and height and will call
cylinder_volume, printing out the result.
Sample IO (The input from the user is shown in bold font – do not program this):
Enter diameter:
10
Enter height:
5
The volume of the cylinder is 392.70
Using the Wing IDE Python shell, you should be able to test your functions as illustrated with the
cylinder program example.
Page 2 of 11
Version 30/03/2023 10:39
NOTE that the automatic marker will test each of your functions individually. To enable this, your
program MUST, as shown in the skeleton, have the following lines at the end:
if __name__=='__main__':
main()
The way in which functions are tested is like that illustrated with the Wing IDE Python shell (see
above). A trial consists of trying to execute a code snippet that uses the function under test. If a trial
fails, typically, you will see the code snippet – an import statement followed by one or more function
call expressions.
Page 3 of 11
Version 30/03/2023 10:39
Sample IO (The input from the user is shown in bold font – do not program this):
Enter the raw data:
fbkjf kfjkdb fds BEGIN 12.2_1014:18.6E,34.0S NWOT EPAC END fdfdfds
Site information:
Location: Cape Town
Coordinates: 34.0S 18.6E
Temperature: 12.20 C
Pressure: 1014.00 hPa
To complete the program you must implement the location, temperature, pressure, y_coordinate,
x_coordinate, and get_block functions. The functions have been identified by applying a divide-and-
conquer strategy. Each solves a part of the overall problem:
• get_block(raw_data)
Given a string of raw data as a parameter, the get_block function extracts the sub string
starting with ‘BEGIN’ and ending with ‘END’.
• location(block)
Given a block string as a parameter, the location function returns the location component in
title case.
• pressure(block)
Given a block string as a parameter, the pressure function returns the pressure component as
a real number value.
• temperature(block)
Given a block string as a parameter, the temperature function returns the temperature
component as a real number value.
• y_coordinate(block)
Given a block string as a parameter, the y_coordinate function returns the y coordinate
component as a string.
• x_coordinate(block)
Given a block string as a parameter, the x_coordinate function returns the x coordinate
component as a string.
Examples
• get_block('fds BEGIN 12.2_1014:18.6E,34.0S NWOT EPAC END fdf fds ')
returns the block string 'BEGIN 12.2_1014:18.6E,34.0S NWOT EPAC END'.
• location('BEGIN 12.2_1014:18.6E,34.0S NWOT EPAC END') returns 'Cape
Town' .
• pressure('BEGIN 12.2_1014:18.6E,34.0S NWOT EPAC END') returns 1014.0.
• temperature('BEGIN 12.2_1014:18.6E,34.0S NWOT EPAC END') returns 12.2.
Page 4 of 11
Version 30/03/2023 10:39
NOTE: The automatic marker will test each of your functions individually. To enable this, you MUST
NOT remove the following lines from the skeleton:
if __name__=='__main__':
main()
Sample IO (The input from the user is shown in bold font – do not program this):
Enter n:
4
Enter k:
2
Number of permutations: 12
Page 5 of 11
Version 30/03/2023 10:39
Sample IO (The input from the user is shown in bold font – do not program this):
Enter month:
May
Enter year:
2020
May
Mo Tu We Th Fr Sa Su
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
To complete the program you are required to complete a number of functions, including a main
function.
• day_of_week(day, month, year)
Given a date consisting of day of month, month number and year, return the day of the week
on which it falls. The function returns 1 for Monday, 2 for Tuesday, …, 7 for Sunday.
• is_leap(year)
Given a year return True if it is a leap year, False otherwise. This function returns a
Boolean value.
• month_num(month_name)
Given the name of a month, return the month number i.e. 1 for January, 2 for February, …, 12
for December. The name can be in UPPER CASE, lower case or Title Case.
• num_days_in(month_num, year)
Given a month number and year, return the number of days in the month.
• num_weeks(month_num, year)
Given a month number and year, return the number of weeks that the month spans. (The
first week is the week in which the first of the month falls, the last week is the week in which
the last day of the month falls. Counting from first to last gives the number of weeks.)
• week(week_num, start_day, days_in_month)
Given a week number, (1st, 2nd, …), the day on which the 1st of the month falls (1 for
Monday, 2 for Tuesday, …), and the number of days in the month, return a string consisting of
Page 6 of 11
Version 30/03/2023 10:39
the day of the month for each day in that week, starting with Monday and ending with
Sunday.
• main()
Obtain the name of a month and a year from the user and then print the calendar for that
month by obtaining the number of weeks and then obtaining the week string for each.
The functions have been identified by applying a divide-and-conquer strategy. Each solves a part of
the overall problem.
Examples
• day_of_week(1, 4, 2020) returns integer 3 (which represents Wednesday).
• is_leap(2020) returns the Boolean value True.
• month_num('April') returns integer 4.
• num_days_in(4, 2020) returns integer 30.
• num_weeks(4, 2020) returns integer 5.
• week(1, 3, 30) returns the string ' 1 2 3 4 5'
• week(2, 3, 30) returns the string ' 6 7 8 9 10 11 12’
HINTS:
• Days of the month.
• We can use Gauss’s formula to calculate the day of the week on which the 1st of January of a
given year falls. This could be adapted for the day_of_week function.
Page 7 of 11
Version 30/03/2023 10:39
NOTE: The automatic marker will test each of your functions individually. To enable this, you MUST
NOT remove the following lines from the skeleton:
if __name__=='__main__':
main()
Submission
Create and submit a Zip file called ‘ABCXYZ123.zip’ (where ABCXYZ123 is YOUR student
number) containing cylinder.py, extract.py, fcombine.py, mymath.py
and calendar_month.py.
NOTES:
1. FOLDERS ARE NOT ALLOWED IN THE ZIP FILE.
2. As you will submit your assignment to the Automarker, the Assignment tab may say
something like “Not Complete”. THIS IS COMPLETELY NORMAL. IGNORE IT.
Page 8 of 11
Version 30/03/2023 10:39
There are two functions, one called ‘main’, and the other ‘CalculateSideB’. Between them,
they break the problem into two parts.
• The main function is responsible for handling user input and output.
• The CalculateSideB function is responsible for calculation.
The main function contains a ‘function call’.
• It calls CalculateSideB, passing the floating point values of a and c.
• The CalculateSideB function uses the values to calculate and return the value of the side b.
• The main function assigns the value it receives to the variable ‘result’.
• On the next line (and final line) it prints out the value of result correct to 2 decimal places.
Page 9 of 11
Version 30/03/2023 10:39
By breaking the programming problem into two parts, each can be concentrated on without concern
for the other. The CalculateSideB function can be developed without concern for where a and c come
from. The main function can be developed without concern for exactly what CalculateSideB will do.
It’s enough to know simply that it requires two values (the sides a and c) and that it will calculate the
length of b.
Admittedly, this programming problem is simple, and can probably be solved quite satisfactorily
without the use of functions, however, it serves to convey the idea.
The idea of divide-and-conquer works best if you have the techniques and technology to fully support
working on one part without concern for another. If, say, you were developing the Pythagoras’
Theorem program and you chose to concentrate on the CalculateSideB function first, you’d probably
want to check it worked correctly. But surely that means you need the main function so that you can
obtain useful inputs?
A technique for dealing with this is to have a ‘stub’ main function which with which to make test
calls to CalculateSideB e.g.
def main():
print(CalculateSideB(3, 4))
print(CalculateSideB(5, 7))
# ...
Alternatively, Wing IDE provides a piece of technology in the form of a Python shell. Within the shell
you can import functions that you are working on and then write expressions that use them (see the
function calls inside the red rectangle below):
The screenshot illustrates its use. Lines entered by the user have a prompt, ‘>>>’ beside them. Lines
without the prompt are responses from the Python shell.
1. The user enters an import statement for the CalculateSideB function.
Page 10 of 11
Version 30/03/2023 10:39
2. The user then enters a function call expression, calling CalculateSideB with the values 3 and 4 for a
and c.
3. The result, 2.6457513110645907 is printed on the next line.
4. The user enters another function call expression, this time with the values 5, and 7,
5. And the result, 4.898979485566356 is printed on the next line.
etc….
Page 11 of 11