Skip to document

9. Recursion - Assignment 9 on Recusions

Assignment 9 on Recusions
Course

Computer science1015 (CSC1015F)

257 Documents
Students shared 257 documents in this course
Academic year: 2024/2025
Uploaded by:
0followers
8Uploads
0upvotes

Comments

Please sign in or register to post comments.

Preview text

Version: 2024/04/15 11:27:

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. '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.

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’ 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:

Pattern Possible word matches

letter letter

l?ad lead, load

l*ad lad, launchpad, lead, letterhead, lipread, load, loggerhead, lunkhead, ...

l?*ad launchpad, lead, letterhead, lipread, load, loggerhead, lunkhead, ...

*action abreaction, abstraction, action, attraction, benefaction, coaction, ...

And here are the rules:

  • 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.

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.

Question Four [30 marks]

Write a program called ‘palindromeprimes’ that uses recursive functions to find all

palindromic primes between two integers N, M, supplied as input. (start and end points are

included).

  • 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.

Some examples of palindromic primes are: 11, 191, 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 (30000)

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’ (where ABCXYZ123 is YOUR student number)

containing simplematch, palindromeprimes, advancedmatch and

palindrome.

Was this document helpful?

9. Recursion - Assignment 9 on Recusions

Course: Computer science1015 (CSC1015F)

257 Documents
Students shared 257 documents in this course
Was this document helpful?
Version: 2024/04/15 11:27:07
Page 1 of 5
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.