Tutorial 10 Worksheet
Tutorial 10 Worksheet
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.
In this question, assume that the input message will only consist of alphabets a to z.
Input Output
D i love you
u hate mad
E u hate mad
i love you
Exercise 10.2 Substring counting
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.
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.
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:
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))