Tutorial 9 Worksheet
COMP1117B Computer Programming I 2024-2025
Tutorial objectives:
- To practice the use of recursive functions in Python.
Deadline: 23:59 May 6, 2025
Exercise 9.1 Substitution Cipher
Write a simple cipher program to perform encryption and decryption. The plain text and the
corresponding cipher as follow:
Plaintext alphabets a b c d e f g h i j k l m
Cipher alphabets c g i n e l o s u r y h p
Plaintext alphabets n o p q r s t u v w x y z
Cipher alphabets v a b z j k x d t f w m q
The user can input 'E' for encryption and 'D' for decryption. And then input the message. Your
program will perform the encryption or decryption accordingly.
For further information:https://en.wikipedia.org/wiki/Substitution_cipher
In this question, assume that the input message will only consist of alphabets a to z.
Sample Test Cases:
Input Output
D i love you
u hate mad
E u hate mad
i love you
Exercise 9.2 Substring counting
Consider the following Python code.
def main():
message = input()
word = input()
print(countWord(message, word))
main()
Implement the countWord() function that accepts two strings, message and word, as input
argument and returns the number of times the word appears in the message.
You need to handle the case that words overlapping with each other in this exercise.
Sample Test Cases:
Input Expected output
banana 0
c
banana 1
banana
banana 2
na
banana 2
ana
Exercise 9.3 Pascal triangle
The following figure shows the first five rows in the Pascal Triangle and how the numbers are
computed.
Write a program to ask the user to input a positive integer n and print the first n rows of the Pascal
Triangle.
Input Expected output
1 [1]
7 [1]
[1, 1]
[1, 2, 1]
[1, 3, 3, 1]
[1, 4, 6, 4, 1]
[1, 5, 10, 10, 5, 1]
[1, 6, 15, 20, 15, 6, 1]
Exercise 9.4 Arabic to Roman Number Converter
Roman numerals are represented by seven different letters:
Roman Arabic
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
For example, the Roman numeral for 27 is written as XXVII, which looks like XX + V + II when
broken down. Roman numerals are usually written from largest to smallest and from left to right.
However, Romans didn’t like four of the same numerals written in a row, so they developed a
subtraction system.
The Roman numeral for three is written as ‘III’, however, the numeral for four is not ‘IIII’. Instead,
we use the subtractive principle. Four is written as ‘IV’, the digits for one and five. Because the
one is before the five,we subtract it making four. The same principle applies to the number nine,
which is written as ‘IX’. There are six instances where subtraction is used:
• I can be placed before V (5) and X (10) to make 4 and 9.
• X can be placed before L (50) and C (100) to make 40 and 90.
• C can be placed before D (500) and M (1000) to make 400 and 900.
Complete the code below by defining the function convert_to_roman(n), which converts an Arabic
number into a Roman numeral.
n = int(input())
if n >= 0 and n <= 3999:
print(convert_to_roman(n))
You can assume the input is between 0 and 3999.
Input Expected output
27 XXVII
440 CDXL
1117 MCXVII
2019 MMXIX
1994 MCMXCIV