You are given an array recipes
containing a list of cooking recipes. Find the recipes containing a key ingredient keyword
. Example: beef
, eggs
, etc.
Example
Input:
let recipes = ["Put eggs in a frying pan", "Cut the beef", "Boil the beef"]
let keyword = "beef"
Expected value/output:
["Cut the beef", "Boil the beef"]
[collapse]
Hint
Simply iterate through every word in every recipe and check if it matches the keyword.
[collapse]
Solution
let recipes = ["Put eggs in a frying pan", "Cut the beef", "Boil the beef"]
let keyword = "beef"
import Foundation
func searchRecipes(recipes:[String], keyword:String) -> [String] {
var foundRecipes = [String]()
for recipe in recipes {
if recipe.contains(keyword) == true {
foundRecipes.append(recipe)
}
}
return foundRecipes
}
var foundRecipes = searchRecipes(recipes: recipes, keyword: keyword)
[collapse]