My solution was very similar to yours. The only real difference is a step to check the text case.
// Silver Challenge
func siftBeans(fromGroceryList: [String]) → (beans: [String], notBeans: [String]) {
var beans = String
var notBeans = String
for checked in fromGroceryList {
if checked.lowercased().hasSuffix(“beans”) {
beans.append((checked))
} else {
notBeans.append(checked)
}
}
return (beans, notBeans)
}
let result = siftBeans(fromGroceryList: [“green Beans”, “milk”, “black bEans”, “pinto beAns”, “bananas”, “beer beaNs”, “butter beanS”])
result.beans == [“green beans”, “black beans”, “pinto beans”, “beer beans”] //expected result is true
result.notBeans == [“milk”, “bananas”] //expected result is true
print(result.beans)
print(result.notBeans)
func lowercased() -> String
// Returns a lowercase version of the string.
func uppercased() -> String
// Returns an uppercase version of the string.
Here is the edited version (featuring the assert macro):
// Silver Challenge
func siftBeans (fromGroceryList list: [String]) -> (beans: [String], notBeans: [String]) {
var beans = [String] ()
var notBeans = [String] ()
for item in list {
// rather than inventing a new variable name, shadowing the variable in enclosing scope!
let item = item.lowercased ()
if item.hasSuffix ("beans") {
beans.append (item)
} else {
notBeans.append (item)
}
}
return (beans, notBeans)
}
let result = siftBeans (fromGroceryList: ["green Beans", "milk", "black bEans", "pinto beAns", "bananas", "beer beaNs", "butter beanS"])
// Check the expected results with the assert macro
// The assert macro will fire if the condition does not hold
assert (result.beans == ["green beans", "black beans", "pinto beans", "beer beans", "butter beans"])
assert (result.notBeans == ["milk", "bananas"])
print (result.beans)
print (result.notBeans)
is valid syntax, you don’t have to create a variable for the lowercased string if you’re not going to use it again.
Edit: OK, I see what you were getting at. The code is expecting the contents of beans and notBeans to be all lower case but it was putting the original mixed case strings into those arrays instead of the lowercased string.