Decompostion and algorithm practice questions - OCRDesigning an algorithm – example two

Every programming problem needs decomposing so that it can be properly understood. From this, an algorithm can be designed and tested.

Part ofComputer ScienceStudy skills

Designing an algorithm – example two

Algorithm design option one - flow diagram

Flow diagram showing the process required for a password to be accepted

Algorithm design option two - pseudocode

#set error flag to True to enable loop
error = True #iterate until a valid password is entered
while error == True #set all variables to default values error = False lowerCase = 0 upperCase = 0 number = 0 #input the password password = input ("Type in a password of at least 5 characters including 1 l/c letters, 1 u/c, 1 number: ") #check password length - error if not at least 5 characters if len(password)< 5 then error = True endif #if the password is the right length then #inspect each character in the password #add 1 to the number of uppercase for each uppercase character #add 1 to the number of lowercase for each lowercase character #add 1 to the number of numbers for each number if error == False then for position in range(0,len(password)) if password[position] >="A" and password[position] < ="Z" then upperCase += 1 elseif password[position] >="a" and password[position]< ="z" then lowerCase += 1 elseif password[position] >="0" and password[position]< ="9" then number += 1 next position endif #if character requirements are not met, an error has occured if upperCase < 1 or lowerCase < 1 or number < 1 then error = True endif #output acceptance
print("Password accepted: ",password)

Testing table

Tests will be required to test that the final program runs correctly.

Test noDescriptionTest dataTest typeExpected outcome
1Enter a valid passwordAbcd36ValidPassword should be accepted
2Enter a ten-character passwordABcd457AnzValidPassword should be accepted
3Enter a five-character passwordAbc67ExtremePassword should be accepted
4Password has at least one lowercase characterABCd57ExtremePassword should be accepted
5Password has at least one numberABcd36ExtremePassword should be accepted
6Password has at least one uppercase characterAbcd45ExtremePassword should be accepted
7Password is too shortAbc5InvalidPassword must be re-entered
8Password does not contain at least one uppercase characterabcd5InvalidPassword must be re-entered
9Password does not contain at least one lowercase characterABCD8InvalidPassword must be re-entered
10Password does not contain at least one numberABcdeInvalidPassword must be re-entered
Test no1
DescriptionEnter a valid password
Test dataAbcd36
Test typeValid
Expected outcomePassword should be accepted
Test no2
DescriptionEnter a ten-character password
Test dataABcd457Anz
Test typeValid
Expected outcomePassword should be accepted
Test no3
DescriptionEnter a five-character password
Test dataAbc67
Test typeExtreme
Expected outcomePassword should be accepted
Test no4
DescriptionPassword has at least one lowercase character
Test dataABCd57
Test typeExtreme
Expected outcomePassword should be accepted
Test no5
DescriptionPassword has at least one number
Test dataABcd36
Test typeExtreme
Expected outcomePassword should be accepted
Test no6
DescriptionPassword has at least one uppercase character
Test dataAbcd45
Test typeExtreme
Expected outcomePassword should be accepted
Test no7
DescriptionPassword is too short
Test dataAbc5
Test typeInvalid
Expected outcomePassword must be re-entered
Test no8
DescriptionPassword does not contain at least one uppercase character
Test dataabcd5
Test typeInvalid
Expected outcomePassword must be re-entered
Test no9
DescriptionPassword does not contain at least one lowercase character
Test dataABCD8
Test typeInvalid
Expected outcomePassword must be re-entered
Test no10
DescriptionPassword does not contain at least one number
Test dataABcde
Test typeInvalid
Expected outcomePassword must be re-entered

The problem has now been fully decomposed and an algorithm has been designed.