Recursion
Recursion
CSC1015F Assignment 9
Recursion
Assignment Instructions
This assignment involves constructing Python functions that use recursion. You must NOT use loop
constructs (such as ‘for’ and ‘while’) in your solutions.
There are four questions. The first concerns identifying palindromes, the second two concern
constructing a module containing pattern matching functions. The last concerns the identification of
palindromic prime numbers.
Assessment
Your code will be automatically marked. Say that there are N trials for a question. The first (N-1)
trials will check that your code functions correctly by executing it on test inputs. The Nth is a penalty
test. It scans the code for evidence of the use of iteration or the use of list/string reversal
expressions. If it finds evidence, then it deducts the marks for the question.
In some cases, the penalty test will report a false positive. For instance, it thinks you're using loops
but you are not, you simply have a variable name containing the word 'for', e.g. 'former', 'afford'.
Furthermore, 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. Up to 10 marks will be
deducted for deficiencies.
Page 1 of 5
Question one [20 marks]
A palindrome is a word, phrase, number, or other sequence of symbols or elements, whose meaning
may be interpreted the same way in either forward or reverse direction (see Wikipedia).
Write a Python program called ‘palindrome.py’ that uses a recursive function to calculate
whether or not a string is a palindrome (reads the same if reversed).
You may NOT use iteration, or a string slice expression (or any other technique) to reverse the string
without using recursion!
Sample IO (The input from the user is shown in bold font – do not program this):
Enter a string:
able was I ere I saw elba
Palindrome!
Sample IO (The input from the user is shown in bold font – do not program this):
Enter a string:
elba is a noob
Not a palindrome!
Pattern matching
Let’s say that we are building a ‘dictionary’ program that people can use to look up the spelling and
meaning of words. People don’t always know the spelling of the word they’re looking for.
For the program to function well, some sort of pattern matching is required. Pattern matching is a
process by which we search for words with some given characteristics. The characteristics of words
include their length, character content and character order.
A pattern is a description of required characteristics.
A simple way of representing a pattern is by using a sequence of letters and special ‘wild card’
characters. Question 2 and 3 concern such a scheme.
We’ll introduce the scheme with examples:
• A pattern is a sequence of letters and the wild card characters ‘?’ and ‘*’.
• When a letter appears in a pattern it indicates that precisely that letter must appear at the
same position in matching words.
• When the wildcard character ‘?’ appears in a pattern it indicates that any letter may appear
at that position.
• When the wildcard character ‘*’ appears in a pattern it indicates that a sequence of zero or
more of letters is acceptable at that position.
Page 2 of 5
Question two [20 marks]
Write a Python module called simplematch.py that contains a recursive function called
‘match(pattern, word)’, that can be used to determine if a given pattern matches a given word.
A ‘?’ wildcard matches any letter at the corresponding position in the word.
On the Amathuba page for this assignment you will find a program called testsimple.py that you can
use to check your function. (The automatic marker will use this.)
Sample IO (The input from the user is shown in bold font – do not program this):
Enter a pattern (or 'q' to quit):
l?ad
Enter a word
lead
It's a match.
Enter a pattern:
le?d
Enter a word
led
They don't match.
Enter a pattern (or 'q' to quit):
l??d
Enter a word
lend
It's a match.
Enter a pattern (or 'q' to quit):
q
HINTS:
• I can think of three base cases. One is when both strings are empty. Another is when one is
empty and the other is not. And the third is …
• The palindrome function makes progress by ‘eating’ the characters at the ends of the string.
This function makes progress by eating the first letter of each string.
In this case, a pattern consists of letters and ‘?’ and ‘*’ wildcards.
A ‘?’ wildcard matches any letter at the corresponding position in the word.
A ‘*’ wildcard matches zero or more letters at the corresponding position in the word.
On the Amathuba page for this assignment you will find a program called testadvanced.py that can
be used to check your function. (The automatic marker will use this.)
Page 3 of 5
Sample IO (The input from the user is shown in bold font – do not program this):
Enter a pattern (or 'q' to quit):
l*ad
Enter a word:
launchpad
It's a match.
Enter a pattern (or 'q' to quit):
*ad
Enter a word:
lead
It's a match.
Enter a pattern (or 'q' to quit):
**ad
Enter a word:
lard
They don't match.
Enter a pattern (or 'q' to quit):
q
HINTS:
• We now have an extra base case: if word is ‘’ and pattern is ‘*’ then return True.
• Generally, when the first character of the pattern is a ‘*’ wildcard, try to (i) match the rest of
the pattern to the word, or (ii) match the pattern to the rest of the word.
• A palindrome number is a number that reads the same from the front and the back.
Examples are: 212, 44, 9009, 4567654.
• To calculate whether a number is a palindrome or not, incorporate your answer to question
1.
• A prime number is a number greater than 1 that is only divisible by 1 and itself.
Examples are: 3, 11, 313.
You may assume it’s always the case that N>1, and that N≤M.
You MUST NOT use any form of loop in your program! (Yes, we’re pretty obsessive about this.)
Add the following lines at the top of your program to increase the amount of recursion that Python
will allow:
import sys
sys.setrecursionlimit (30000)
Page 4 of 5
Sample IO (The input from the user is shown in bold font – do not program this):
Enter the starting point N:
200
Enter the ending point M:
800
The palindromic primes are:
313
353
373
383
727
757
787
797
Submission
Create and submit a Zip file called ‘ABCXYZ123.zip’ (where ABCXYZ123 is YOUR student number)
containing simplematch.py, palindromeprimes.py, advancedmatch.py and
palindrome.py.
Page 5 of 5