Cleaning data in pseudocode

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Cleaning data in pseudocode

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which loop body correctly builds a clean copy without changing the raw data?
A for i from 1 to length(raw) if raw[i].age < 0 then delete raw[i]
B for i from 1 to length(raw) if raw[i].age >= 0 then append(clean, raw[i])
2. A student's name field holds " aVA ". Which step standardises it before it is written to clean?
A name <- capitalise(trim(raw[i].name))
B name <- raw[i].name
3. How should a cleaning algorithm deal with a record that has a missing email?
A if raw[i].email = "" then skip the record (do not append it to clean)
B if raw[i].email = "" then append it to clean anyway
4. Records are duplicates when they share the same key (e.g. studentId). Which is the correct check?
A if studentId is not already a key in clean then append
B if studentId is not already a key in raw then append
5. True or false: a cleaning algorithm should overwrite raw[i] with the fixed values so only one copy exists.
A True - overwriting raw saves memory and is safer
B False - write the fixed record to a new clean array

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1name <- upper(trim(raw[i].name)) AWrites the good record to the new clean array, leaving the raw data untouched
2append clean, raw[i] Bif raw[i].id = clean[j].id then isDuplicate <- true end if
3Skip a record with a missing value instead of copying it to the clean array CTrims extra spaces and standardises the case of the text before it is stored
4Remove duplicates by comparing the key of the current record with keys already in the clean array DThe check only rejects negative marks, so invalid values above the maximum (such as 150) still get through. It needs an upper limit, such as raw[i].mark >= 0 and raw[i].mark <= 100.
5for i from 1 to length(raw) raw[i].name <- upper(raw[i].name) append clean, raw[i] next i Eif raw[i].mark = "" then continue end if
6for i from 1 to length(raw) if raw[i].mark >= 0 then append clean, raw[i] end if next i FThe loop changes the raw array directly, so the original data is lost. It should store the cleaned value in a variable or in a copy of the record and write that to clean.

Definitions

cleaning algorithm

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
for i from 1 to ______(raw) name ← trim(raw[i].name) append name to clean
2.
for i from 1 to length(raw) name ← _____(trim(raw[i].name)) append name to clean
3.
for i from 1 to length(raw) if raw[i].email _ "" then append raw[i] to clean
4.
age ← raw[i].age if age >= 0 ___ age <= 120 then append raw[i] to clean
5.
if ___ contains(seenIDs, raw[i].id) then append raw[i].id to seenIDs append raw[i] to clean

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A for i from 1 to n
B name <- trim(raw[i].name)
C if name = "" then
D k <- k + 1
E clean[k] <- name
F end if
Question 2
A for i from 1 to n
B town <- trim(raw[i].town)
C if town != "" then
D lower(town) <- town
E add town to clean
F end if
Question 3
A for i from 1 to n
B id <- raw[i].id
C if id in keys then
D add id to keys
E add raw[i] to clean
F end if
Question 4
A for i from 1 to n + 1
B c <- upper(trim(raw[i].cd))
C if c != "" then
D add c to clean
E end if
Question 5
A for i from 1 to n
B t <- lower(trim(raw[1].tag))
C if t != "" then
D add t to clean
E end if

Answer Key

Cleaning data in pseudocode
Section A: Quick Questions
Q1
B — for i from 1 to length(raw) if raw[i].age >= 0 then append(clean, raw[i])
Q2
A — name <- capitalise(trim(raw[i].name))
Q3
A — if raw[i].email = "" then skip the record (do not append it to clean)
Q4
A — if studentId is not already a key in clean then append
Q5
B — False - write the fixed record to a new clean array
Section B: Match the Pairs
Q1
name <- upper(trim(raw[i].name)) → Trims extra spaces and standardises the case of the text before it is stored
Q2
append clean, raw[i] → Writes the good record to the new clean array, leaving the raw data untouched
Q3
Skip a record with a missing value instead of copying it to the clean array → if raw[i].mark = "" then continue end if
Q4
Remove duplicates by comparing the key of the current record with keys already in the clean array → if raw[i].id = clean[j].id then isDuplicate <- true end if
Q5
for i from 1 to length(raw) raw[i].name <- upper(raw[i].name) append clean, raw[i] next i → The loop changes the raw array directly, so the original data is lost. It should store the cleaned value in a variable or in a copy of the record and write that to clean.
Q6
for i from 1 to length(raw) if raw[i].mark >= 0 then append clean, raw[i] end if next i → The check only rejects negative marks, so invalid values above the maximum (such as 150) still get through. It needs an upper limit, such as raw[i].mark >= 0 and raw[i].mark <= 100.
Section C: Definitions
Q1
cleaning algorithm — A loop that checks each record with if conditions, trims and standardises text, rejects or fixes bad values, and writes only the good records to a new clean array, leaving the raw data untouched.
Section D: Fill in the Blanks
Q1
for i from 1 to length(raw) name ← trim(raw[i].name) append name to clean
Q2
for i from 1 to length(raw) name ← lower(trim(raw[i].name)) append name to clean
Q3
for i from 1 to length(raw) if raw[i].email ≠ "" then append raw[i] to clean
Q4
age ← raw[i].age if age >= 0 and age <= 120 then append raw[i] to clean
Q5
if not contains(seenIDs, raw[i].id) then append raw[i].id to seenIDs append raw[i] to clean
Section E: Spot the Error
Q1
C — The test is back to front. It only lets blank names through and drops the good ones. To skip records with missing values, check that name != "".
Q2
D — Assignment goes the wrong way. The variable being set must be on the left: town <- lower(town). Otherwise the text is never standardised to lower case.
Q3
C — This keeps only duplicates and drops the first copy of every key. To remove duplicates, copy a record only if its key is not in keys yet.
Q4
A — The loop runs one step past the last record, so raw[n + 1] does not exist. A cleaning loop must stop at n: for i from 1 to n.
Q5
B — This always reads the first record instead of the current one. It should use the loop counter: raw[i].tag.