i have this input
let teachersInfo: [[String]] = [
["names", "comments"],
["Ian", ",scientist,male, no_available"],
["Mark", "teacher,elementary school,female, available_for_new"],
["Bryan", "interior designer,male,no_available"],
["Tomas", "profesional surfer,master"],
["Justine", "no_available, scientist"],
["Malek", "teacher, elementary school, available_for_new"],
["Adrian", "scientist, profesional surfer, available_for_new"],
["Mike", "elementary schoole,male,no_available"]
]
how can i make a funtions that returns
func findTeachersSkills() -> [String] {
// TODO: return all names that are profesional surfer
return []
}
the names from the whoever is a profesional surfer
then get the names from whoever is teacher and have available_for_new
CodePudding user response:
First of all, your sample data does not contain the result which fits all your conditions so I add one item into it
let teachersInfo: [[String]] = [
["names", "comments"],
["Ian", ",scientist,male, no_available"],
["Mark", "teacher,elementary school,female, available_for_new"],
["Bryan", "interior designer,male,no_available"],
["Tomas", "profesional surfer,master"],
["Justine", "no_available, scientist"],
["Malek", "teacher, elementary school, available_for_new"],
["Adrian", "scientist, profesional surfer, available_for_new"],
["Mike", "elementary schoole,male,no_available"],
["Johan", "teacher, profesional surfer, available_for_new"]
]
And for your question, it can divide into two task: get the list which is fit all your conditions and get the name of it. From your array we can notice that at index 0 is the name and all others are at index 1
Code will be like this
func findTeachersSkills(_ teachersInfo: [[String]]) -> [String] {
// TODO: return all names that are profesional surfer
// get teacher info which is a profesional surfer
let listTeacherInfoIsProfesionalSurfer = teachersInfo.filter{$0[1].contains("profesional surfer")}
// get teacher info which is teacher and have available_for_new
let listTeacherInfoIsTeacher = listTeacherInfoIsProfesionalSurfer.filter{$0[1].contains("teacher") && $0[1].contains("available_for_new")}
// Bacause name in index 0 so return it
return listTeacherInfoIsTeacher.map{$0[0]}
}
We can combine all into one filter like this
func findTeachersSkills(_ teachersInfo: [[String]]) -> [String] {
// TODO: return all names that are profesional surfer
// get teacher info which is a profesional surfer
// get teacher info which is teacher and have available_for_new
let listTeacherInfoIsTeacher = teachersInfo.filter{$0[1].contains("profesional surfer") && $0[1].contains("teacher") && $0[1].contains("available_for_new")}
// Bacause name in index 0 so return it
return listTeacherInfoIsTeacher.map{$0[0]}
}
The result is
let finalResult = findTeachersSkills(teachersInfo) // ["Johan"]