Tutorial 3 – Python Selection and Iteration solved

$35.00

Category: You will receive a download link of the .ZIP file upon Payment

Description

5/5 - (1 vote)

Introduction
Learning outcomes
Skills
 Be able to construct and reason about Python solutions to programming problems involving
selection and/or iteration.
Including,
o Constructing and reasoning about compound Boolean expressions involving the use
of python comparison operators.
o Constructing and reasoning about data selection expressions that use of string
slicing and casting.
Knowledge
 The forms of Python if, if-else and if-elif statements.
 Python comparison operators, the Boolean operators ‘and’, ‘not’ and ‘or’, and operator
precedence.
 The form of the Python for loop, and the use of break and continue statements.
 The role of the range function in for loops.
 The distinction between definite and indefinite loop structures.
 Forms and possible uses of string slice and casting operations.
Exercise One [25 marks]
This exercise concerns decoding South African identity (ID) numbers.
A South African ID number is a 13-digit number with the following format (courtesy Western Cape
Government):
YYMMDDSSSSCAZ
 The first six digits are the person’s date of birth, with 2 digits for year, then 2 digits for
month, then 2 digits for day e.g. 6th June 2017 is represented as 170606.
 The next four digits identify the person’s gender. Females are assigned numbers in the range
0000-4999. Males are assigned numbers in the range 5000-9999.
 The next digit indicates whether the person is South African citizen, or a permanent
resident. A 0 denotes a South African citizen. A 1 denotes a permanent resident.
 The last digit is a checksum digit that is used to check that the number sequence is accurate.
 The second to last digit is not used.
Part A [10 marks]
Write a program called simpleparser.py that asks the user to input their ID number, and then
decodes and prints out their date of birth, gender, and citizenship status.
Sample I/O:
Please enter your ID number:
9202204720082
CONTINUED
Your date of birth is 20/02/92.
You are female.
You are a South African citizen.
Sample I/O:
Please enter your ID number:
1703295010174
Your date of birth is 29/03/17.
You are male.
You are a permanent resident.
The Luhn algorithm
The check digit at the end of an ID number is used to detect accidental mistakes, such a through
misreading or mistyping. Given an ID, the validity (correctness) can be determined by recalculating
the check digit and comparing it to that which is given.
A check digit is calculated by applying the Luhn algorithm to the first 12 digits:
𝑑11𝑑10𝑑9𝑑8𝑑7𝑑6𝑑5𝑑4𝑑3𝑑2𝑑1𝑑0𝑑𝑐
Essentially, the algorithm requires calculating the sum of the digits i.e.
𝑠𝑢𝑚 = 𝑑11+𝑑10+𝑑9+𝑑8+𝑑7+𝑑6+𝑑5+𝑑4+𝑑3+𝑑2+𝑑1+𝑑0
However, alternate digits are not added directly. For each of these, multiply by 2, obtain the
modulus of 9, and add this to the total. (i.e for each 𝑑 in 𝑑10,𝑑8,𝑑6𝑑4𝑑2𝑑0, obtain (𝑑 ×
2) 𝑚𝑜𝑑𝑢𝑙𝑢𝑠 9).
The full summation is:
𝑠𝑢𝑚 = 𝑑11+(𝑑10 × 2) 𝑚𝑜𝑑𝑢𝑙𝑢𝑠 9 + 𝑑9+ ⋯ + 𝑑1 + (𝑑0 × 2) 𝑚𝑜𝑑𝑢𝑙𝑢𝑠 9)
Calculate the expected check digit by obtaining 𝑠𝑢𝑚 𝑚𝑜𝑑𝑢𝑙𝑢𝑠 10.
An ID number is valid if:
10 – (𝑠𝑢𝑚 𝑚𝑜𝑑𝑢𝑙𝑢𝑠 10) = 𝑑𝑐
Part B [15 marks]
Write a program called advancedparser.py that asks the user to input their ID number, and then
does two things:
(i) using the check digit, it determines whether the number is valid;
(ii) (ii) if the number is valid then it decodes and prints out the user’s date of birth, gender,
citizenship status.
Sample I/O:
Please enter your ID number:
9202204720082
Invalid ID number.
CONTINUED
Sample I/O:
Please enter your ID number:
1703295010174
Your date of birth is 29/03/17.
You are male.
You are a permanent resident.
Exercise Two [25 marks]
In South Africa, the department of transport issues different driving licenses depending on the
category (or categories) of vehicle that the recipient wishes to drive (or learn to drive).
How is a vehicle classified? It depends on the Gross Vehicle Mass (GVM), and on whether the vehicle
is ‘articulated’, or whether a trailer is being towed.
The GVM dictates the base class to which the vehicle belongs: B, C1, or C.
GVM≤3500 Class B
3500