Professional Documents
Culture Documents
ICT133
Structured Programming
Tutor-Marked Assignment
This assignment is worth 18% of the final mark for ICT133, Structured Programming.
The cut-off date for this assignment is Wednesday, 04 March 2020, 2355 hrs.
Assignment Requirements:
Do NOT define classes for this TMA.
Unless specified in the question, you CANNOT use packages not covered in this
module, e.g., numpy, pandas etc..
Provide sufficient comments to your code, and ensure that your program adheres to good
programming practices such as not using global variables.
Submission Details:
Use the template word document provided - SUSS_PI_No-FullName_TMA.docx.
Rename the file with your SUSS PI and full name join with “_TMA” e.g. “SUSS_PI-
TomTanKinMeng_TMA.docx” (without the quotes).
Include the following particulars on the first page of the word document, on separate
lines: Course Code, SUSS PI No., Your Name, Tutorial Group and Submission Date.
Copy and paste the source code of each program you write in text format. Submit
screenshots for only output of your program, where applicable. If you submit the source
code as a screenshot, your code will not be marked. That is, screenshot code will be
awarded zero mark.
Submit your solution in the form of a single MS Word document. Do NOT submit as
a pdf document. You will be penalised if you fail to submit a word document.
Question 1
Open TMA Q1.fprg with Flowgorithm, and examine the flowchart for Vigenere Cipher,
a symmetric encryption, (https://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher). A
pictorial representation of symmetric key encryption is shown in Figure Q1.1.
Symmetric key encryption uses the same secret key to encrypt and decrypt a message:
The sender uses a secret key to encrypt a plain text (the message) into cipher text
(scrambled text that is intelligible).
The recipient uses the same secret key to decrypt the scrambled text to plain text.
Study the flowchart, and execute it via the slow run button on Flowgorthm. Add
breakpoints to allow you to observe the values of the variables at various points of the
flowchart.
(a) Explain and describe the purposes, input, output, and logic of each module in the
flowchart:
(i) IntToArray
(2 marks)
(ii) EncryptPhoneNumber
(3 marks)
(iii) DecryptPhoneNumber
(3 marks)
(b) Make postings to the Lecture Group Discussion Forum to discuss with fellow
students to clarify your doubts and to explain your understanding. Then derive
your own solution to parts (i) and (ii), and justify your solution using relevant
postings which you agree or disagree. Failure to justify using your postings and
others’ postings will incur a penalty.
Is it possible that some input cause an error even if the input values adhere
to the TWO (2) restrictions? Justify your answer.
If it is possible that some input can cause an error, either modify the
flowchart or the program obtained using the automatic conversion tool
provided by Flowgorithm, to avoid the error.
(6 marks)
(ii) Convert the flowchart into a Python program using the automatic
conversion tool provided by Flowgorithm. Modify the program to use the
datatype str instead of int to store the key, phone number and scrambled
phone number.
(5 marks)
(c) Github is a rich resource for source codes. Review the programs for Vigenere
Cipher found in the following two URLs:
https://github.com/JB1984/Vigenere/blob/master/Vigenere.py
https://github.com/nazaninsbr/Vigenere-Cipher-
Implementation/blob/master/main.py
Make postings to the Lecture Group Discussion Forum to discuss with fellow
students to clarify your doubts and to explain your understanding of the TWO
(2) programs. Then derive your own solution to parts (i) and (ii) using relevant
postings which you agree or disagree. Failure to justify using your postings and
others’ postings will incur a penalty.
(i) Compare and contrast the TWO (2) programs, and describe their
strengths and weaknesses. Justify your answer.
(4 marks)
Question 2
Note: This question will be answered and submitted through an online system called
nbgrader. You need to login to the system and attempt the solution online; your login
credentials will be provided to you. Further instructions will be given in the online
system, please follow those instructions to submit your solution.
The total CPF contribution is made up of two components: the employer contribution
and the employee contribution. The rates of contribution are based on the age groups and
the income levels of the employees as shown in Table Q2.1.
Notes:
OW: Ordinary Wages (capped at OW Ceiling of $6,000)
AW: Additional Wages;
TW: Total Wages = OW + AW
1. Total CPF contributions should be rounded to the nearest dollar. An amount of 50
cents should be regarded as an additional dollar.
2. Compute the employee’s share of CPF contribution (cents should be dropped).
3. Employer’s share = Total contribution - Employee’s share
The CPF contribution is deposited into the following 3 accounts for different purposes
and with different interest rates:
Ordinary Account (OA) – for housing, CPF insurance, investment and education.
Special Account (SA) – for old age and investment in retirement-related financial
products.
The allocation rates for the 3 accounts according to the age groups as shown in Table
Q2.2.
(a) Without using collection structures, write a function to calculate and return the
contributions to the 3 CPF accounts according to Table Q2.2, given whole number
input to the following parameters: age and CPF total contribution.
(4 marks)
(b) Use a collection structure, write another function that takes the same input and
performs the same task as the function described in part (a).
(8 marks)
(c) Explain which of the two functions written for parts (a) and (b) is better. You
need to define the term better.
(2 marks)
The function computes the age of employee based on both the month and year of
birth, calculates and returns the total CPF contribution and employee’s share of
CPF contribution.
Your function must use a collection structure to record the data in Table Q2.1,
and to access the collection structure for the data/formula to perform the
computation. You should apply function decomposition, that is, break your
function into other simpler functions, where possible.
(12 marks)
(e) Write an application that reads the following input: Month and Year of Birth,
Ordinary Wages and Additional Wages. Month and Year of Birth are whole
numbers but Ordinary Wages and Additional Wages are decimal numbers.
The application calls the appropriate functions and displays total CPF
contribution, employee’s CPF contribution, employee’s CPF contribution, and
contributions to the various CPF accounts.
(f) Write an application that reads the input from a file, Q2Data.txt, with this
content:
1 1959
3567.12
312.31
8 1959
3567.12
312.31
The application writes the output for each CPF contribution to a file,
Q2Output.txt.
Age: 61 OW: $3567.12 AW: $312.31
Total CPF contribution: $640
Employee CPF contribution: $290
Employer CPF contribution: $350
Contribution to Ordinary Account: $135.81
Contribution to Special Account: $96.96
Contribution to Medisave Account: $407.23
Age: 60 OW: $3567.12 AW: $312.31
Total CPF contribution: $1009
Employee CPF contribution: $504
Employer CPF contribution: $505
Contribution to Ordinary Account: $465.75
Contribution to Special Account: $135.81
Contribution to Medisave Account: $407.43
(5 marks)
Question 3
Apha MasterMind is a game adapted from MasterMind where a player attempts to guess
a secret code with as few steps as possible. Instead of colour pins, the secret code in Apha
MasterMind is a sequence of 4 letters. Each letter in a secret code is unique, and is from
‘A’ to ‘H’. Some examples of valid secret code are “ABDF”, “EGHC” and “DAFE”.
import random
def chooseOneLetter (base1, base2):
ratio = 10
seed = int(random.uniform (0, ratio*len(base1)+len(base2)))
if seed < ratio*len(base1):
chosenLetter = base1[int(seed/ratio)]
base1.remove(chosenLetter)
else:
chosenLetter = base2[(seed - ratio*len(base1))]
base2.remove(chosenLetter)
return chosenLetter
(a) Using the functions provided, develop the Alpha MasterMind game engine in
Python. The program must include the following two variables:
The first variable references a list of letters in their correct positions or None
if the letter in the guess is not correct,
The game engine tracks the number of valid guesses that the player makes to
guess the secret code. If the guess is invalid, the player is prompted to re-enter a
new guess. When a valid guess matches the secret code or if the player enters an
empty string, the game ends.
The game engine then prompts the player whether he wishes to play another
game.
(b) Suggest and describe TWO (2) additional outputs that the game engine can
provide to the player to help him make a better next guess. Your suggestion can
include only information about the guess but must not include information about
the secret code.
(4 marks)
(c) Develop a scheme and a program to analyse the distribution of letters in the secret
words generated according to getSecretCode(base1, base2) for a
population space of at least 1000 games. For example, you may wish to count the
number of letters that appear from base1 or base2.
(5 marks)
(d) Implement a computer agent that takes the place of a human player based on your
observation in Q2(c). The computer agent will solve the computational problems
related to making as few guesses as possible when playing Alpha MasterMind.
You may provide the computer agent the outcome of each guess, including
information suggested in Q2(b) so that it can make a better next guess.
(10 marks)