You are on page 1of 10

.

ICT133
Structured Programming

Tutor-Marked Assignment

January 2020 Semester Presentation


ICT133 Tutor-Marked Assignment

TUTOR-MARKED ASSIGNMENT (TMA)

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.

Failing to do so can incur a penalty of as much as 50% of the mark allotted.

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.

SINGAPORE UNIVERSITY OF SOCIAL SCIENCES (SUSS) Page 2 of 10


ICT133 Tutor-Marked Assignment

Answer all questions. (Total 100 marks)

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.

Figure. Q1.1 Symmetric Encryption

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.

SINGAPORE UNIVERSITY OF SOCIAL SCIENCES (SUSS) Page 3 of 10


ICT133 Tutor-Marked Assignment

(i) EncryptPhoneNumber assumes input values adhere to these restrictions:


The message is an 8 digit Singapore phone number.
The key is 5 digits.

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)

(ii) Employ structured programing principles to consolidate the TWO (2)


programs into one program to avoid their weaknesses, based on your
discussion in Q1(c)(i).
(7 marks)

SINGAPORE UNIVERSITY OF SOCIAL SCIENCES (SUSS) Page 4 of 10


ICT133 Tutor-Marked Assignment

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.

Singapore’s CPF scheme is a compulsory comprehensive savings plan for working


Singaporeans and permanent residents.

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.

Employee’ Employee’s total Total CPF contributions Employee’s share of CPF


s Age wages for the (Employer’s & Employee’s contributions
(Years) calendar month share)
55 & ≤ $50 Nil Nil
below > $50 to $500 17% (TW) Nil
> $500 to < $750 17% (TW) + 0.6 (TW - $500) 0.6 (TW - $500)
≥ $750 [37% (OW)]* + 37% (AW) [20% (OW)]* + 20% (AW)
* Max. of $2,220 * Max. of $1,200
Above 55 - ≤ $50 Nil Nil
60 > $50 to $500 13% (TW) Nil
> $500 to < $750 13% (TW) + 0.39 (TW - $500) 0.39 (TW - $500)
≥ $750 [26% (OW)]* + 26% (AW) [13% (OW)]* + 13% (AW)
* Max. of $1,560 * Max. of $780
Above 60 - ≤ $50 Nil Nil
65 > $50 to $500 9 % (TW) Nil
> $500 to < $750 9% (TW) + 0.225 (TW - $500) 0.225 (TW - $500)
≥ $750 [16.5% (OW)]* + 16.5% (AW) [7.5% (OW)]* + 7.5% (AW)
*Max. of $990 * Max. of $450
Above 65 ≤ $50 Nil Nil
> $50 to $500 7.5% (TW) Nil
> $500 to < $750 7.5% (TW) + 0.15 (TW - $500) 0.15 (TW - $500)
≥ $750 [12.5% (OW)]* + 12.5% (AW) [5% (OW)]* + 5% (AW)
* Max. of $750 * Max. of $300
Table Q2.1 CPF Contribution Rate (adapted from table published in cpf.gov.sg)

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.

SINGAPORE UNIVERSITY OF SOCIAL SCIENCES (SUSS) Page 5 of 10


ICT133 Tutor-Marked Assignment

Medisave Account (MA) – for hospitalisation and approved medical insurance.

The allocation rates for the 3 accounts according to the age groups as shown in Table
Q2.2.

Employee’s Age Credited to


(Years) Ordinary Account Special Account MediSave Account
(Ratio of (Ratio of (Ratio of
Contribution) Contribution) Contribution)
35 & below 0.6217 0.1621 0.2162
Above 35 – 45 0.5677 0.1891 0.2432
Above 45 – 50 0.5136 0.2162 0.2702
Above 50 – 55 0.4055 0.3108 0.2837
Above 55 – 60 0.4616 0.1346 0.4038
Above 60 – 65 0.2122 0.1515 0.6363
Above 65 0.08 0.08 0.84
Table Q2.2 Rates for CPF Account Allocation

(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)

(d) Write a function that takes in the following parameters:


Month and Year of Birth
Ordinary Wages
Additional Wages

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.

SINGAPORE UNIVERSITY OF SOCIAL SCIENCES (SUSS) Page 6 of 10


ICT133 Tutor-Marked Assignment

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.

A sample output is shown here, with input underlined:


Enter month and year, separated by space: 1 1990
Enter amount for monthly ordinary wages: 3567.12
Enter amount for monthly additional wages: 312.31
Total CPF contribution: $1435
Employee CPF contribution: $775
Employer CPF contribution: $660
Contribution to Ordinary Account: $892.14
Contribution to Special Account: $232.61
Contribution to Medisave Account: $310.25
(4 marks)

(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)

SINGAPORE UNIVERSITY OF SOCIAL SCIENCES (SUSS) Page 7 of 10


ICT133 Tutor-Marked Assignment

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

The following examples are invalid:


“ABBG” – it contains duplicate characters of “B”.
“LHAD” – it contains character, “L”, outside of the range between “A” to “H”.
“DBA” –it contains only 3 characters instead of the required 4.

You are given the following functions.


chooseOneLetter(base1, base2) accepts 2 lists of letters, base1 and
base2, and returns one letter chosen from either one of the two lists.
Note that the chosen letter is removed from the respective list so that it cannot be
chosen in subsequent calls.
getSecretCode(base1, base2) accepts 2 lists of letters, and returns a 4-
letter valid secret code by calling chooseOneLetter four times.

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

def getSecretCode(base1, base2):


secretCode = ""
for i in range(4):
chosenLetter = chooseOneLetter (base1, base2)
secretCode += chosenLetter
return secretCode

# base1 = ["A", "B", "C", "D"]


# base2 = ["E", "F", "G", "H"]

(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,

SINGAPORE UNIVERSITY OF SOCIAL SCIENCES (SUSS) Page 8 of 10


ICT133 Tutor-Marked Assignment

The second variable references a dictionary with letters guessed in incorrect


positions as keys, and the number of times the letters were guessed in
incorrect positions as values.

An example content of the two variables as displayed during a game is shown


here:
The correct letters in correct positions: ['B', None, None,
None]
The correct letters and the number of times found in incorrect
positions: {'A': 2, 'B': 1, 'D': 2}

In the example, the secret code is BAFD.


The first variable references ['B', None, None, None].
The player has correctly guessed that letter B is in the first position, but all
other letters in other positions are incorrect.
The second variable references {'A': 2, 'B': 1, 'D': 2}.
The player has guessed 3 correct letters thus far: A is guessed in wrong
positions twice, B in a wrong position once, and D in wrong positions twice.

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.

An example run is given here:


Enter a guess to continue or RETURN to quit: abda
Please enter 4 unique letters, A to H
Enter a guess to continue or RETURN to quit: ade
Please enter 4 unique letters, A to H
Enter a guess to continue or RETURN to quit: asbc
Please enter 4 unique letters, A to H
Enter a guess to continue or RETURN to quit: abcd
The guess is not correct, attempt no. 1
The correct letters in correct positions: [None, None, None,
None]
The correct letters and the number of times found in incorrect
positions: {'B': 1, 'C': 1, 'D': 1}
Enter a guess to continue or RETURN to quit: dhcb
The guess is not correct, attempt no. 2
The correct letters in correct positions: [None, None, None,
'B']
The correct letters and the number of times found in incorrect
positions: {'B': 1, 'C': 2, 'D': 2, 'H': 1}
Enter a guess to continue or RETURN to quit: cdhb
You guessed it correctly in 3 attempts, the secret word is CDHB
Do you want to play again? Y to play again: y

SINGAPORE UNIVERSITY OF SOCIAL SCIENCES (SUSS) Page 9 of 10


ICT133 Tutor-Marked Assignment

Enter a guess to continue or RETURN to quit: ABCD


The guess is not correct, attempt no. 1
The correct letters in correct positions: [None, None, None,
None]
The correct letters and the number of times found in incorrect
positions: {'A': 1, 'B': 1, 'D': 1}
Enter a guess to continue or RETURN to quit: BDAE
The guess is not correct, attempt no. 2
The correct letters in correct positions: ['B', None, None,
None]
The correct letters and the number of times found in incorrect
positions: {'A': 2, 'B': 1, 'D': 2}
Enter a guess to continue or RETURN to quit: BAFD
The guess is not correct, attempt no. 3
The correct letters in correct positions: ['B', 'A', None,
None]
The correct letters and the number of times found in incorrect
positions: {'A': 2, 'B': 1, 'D': 3, 'F': 1}
Enter a guess to continue or RETURN to quit:
Quitting game
Do you want to play again? Y to play again:
Application ends
(16 marks)

(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)

---- END OF ASSIGNMENT ----

SINGAPORE UNIVERSITY OF SOCIAL SCIENCES (SUSS) Page 10 of 10

You might also like