I was doing an exercise about cleaning data. This is a previous code for searching data errors that works correctly:
// Create a Tabulator instance.
var tabulator = Tabulator()
// Loop through surveyData, incrementing the count for each response.
for data in surveyData {
tabulator.incrementCount(forValue: data)
}
// Loop through the tallied shows (stored in tabulator.values), printing the information from each one.
for i in tabulator.values {
tabulator.count(forValue: i)
}
showCatalog
print("\n\n***** FIRST CLEANING PASS *****\n\n")
// Print a header
print("\n\n***** TABULATION FOR VALID DATA ******\n\n")
showCatalog
// Loop through all tabulator values. Only print the count for those that are contained in showCatalog.
var k_normal = 0
var k_bad = 0
for a in tabulator.values {
if showCatalog.contains(a) == true {
k_normal += 1
} else {
k_bad += 1
print(a)
print(k_bad)
}
}
// Create a variable to keep a count of the errors.
// Print a header
print("\n\n***** DATA ERRORS ******\n\n")
// Loop through all tabulator values.
// If a value is not contained in showCatalog:
// - Increase the error count
// - Print it
// Print the error count.
Then I have a new exercise: Some of the errors in the data seem to be simple capitalization mistakes. You've already seen a way to solve such problems in the QuestionBot app: Just convert the string to lowercase. First, you'll need to have a lowercased version of your show catalog. Recall that you can create a lowercase string by using the lowercased() method. Create a new catalog containing lowercased versions of all the shows.You should see the error count go from 13 down to just 3.
This is my next code that doesn't execute any correct or incorrect inputs. Please help me with that.
// Make a new array variable.
var newArray: [String] = []
// For all shows in showCatalog, add a lowercase version to the array.
for shows in showCatalog {
shows.lowercased()
newArray.append(shows.lowercased())
}
newArray
// Create a Tabulator instance.
var tabulator = Tabulator()
// Loop through surveyData. Make a lowercase version of each value, then increment its count.
for data in newArray {
tabulator.incrementCount(forValue: data)
}
// Loop through all tabulator values. Print only those that are contained in the lowercase version of the show catalog.
var k_normal = 0
var k_bad = 0
for a in tabulator.values {
if newArray.contains(a) == true {
k_normal += 1
} else {
k_bad += 1
print(a)
}
}
// Print a header
print(k_normal)
print(k_bad)
// Create a variable to keep a count of the errors.
// Loop through all tabulator values.
// If a value is not contained in the lowercase show catalog:
// - Increase the error count
// - Print it
// Print the error count.
Thanks for the feedback.
So you have:
showCatalog = ["Finding Alyssa", "Last One In", "Perfectly Imperfect", "Neptune Abyss", "Beyond the Night", "Mason Kemp", "My One Little Secret", "Ocean Express", "100 Stories About One Night", "Unsure", "Race to Last Place", "The Fourth Time", "Not Now", "Run Like Kate", "Up the Road", "The Ransom Project", "The Sternwood Five", "Beneath", "The Faction", "Marginalized"]
I've lowercased all these names in the array and saved them in a newArray
Note that you can do it simply with map function:
let newArray = showCatalog.map() { $0.lowercased() }
if you are not familiar with such expression, this is equivalent to
let newArray = showCatalog.map() { name in name.lowercased() }
My mission is to find in tabulator(random names, but some of these names exist in showCatalog) those names, that actually exist in showCatalog.
How is tabulator defined ?
Could you also clarify the above question ? How are random names generated ? Are they pure random or random from some collection ?
Once this is clear it will be easier to answer next point:
I have to print names in the array that are false, so they don't exist there
Another point to clarify: are tabulator values lowercased ?
If not, you should probably call:
for a in tabulator.values {
if newArray.contains(a.lowercased()) {
If they are, you should call:
for a in tabulator.values {
if newArray.contains(a) {