6. Functions
6. Functions
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.
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.
• y_coordinate('BEGIN 12.2_1014:18.6E,34.0S NWOT EPAC END') returns
'34.0S'.
• x_coordinate('BEGIN 12.2_1014:18.6E,34.0S NWOT EPAC END') returns
'18.6E'.
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()
Mimicking the structure of the program of question one, to complete the program:
Here are the rules you should use to implement the count_syllables:
• A syllable can be identified if there is a sequence of vowels {a, e, i, o, u, y} in a
word. NB: y is considered a vowel.
• If the letter “e” appears at the end of a word it should be disregarded unless it
happens to be the only vowel in the word.
• The lowest number of syllables a word can have is 1.
Examples:
Word Number of
Syllables
Harry 2
Hair 1
Hare 1
The 1
NOTE: The automatic marker will test your program AND will test your function
independent of the main routine.
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 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.
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 extract.py, sullables.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.
APPENDIX A – A program that calculates the
hypotenuse of a right-angled triangle given the other two
sides.
To explain by example, consider a scenario where you are asked to write a program called side.py
to calculate the side, b, of a right-angled triangle given the sides a and c using Pythagoras’ Theorem.
Here is a solution that demonstrates the use of functions:
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.
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.
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….