Silver Challenge ch. 12

My solution:

func siftBeans(fromGroceryList list: [String]) -> (beans: [String], otherGroceries: [String]) {
  var beans = [String]()
  var otherGroceries = [String]()
  
  for product in list {
    if product.hasSuffix("beans") {
      beans.append(product)
    } else {
      otherGroceries.append(product)
    }
  }
  
  return (beans, otherGroceries)
}

let result2 = siftBeans(fromGroceryList: ["green beans",
                                         "milk",
                                         "black beans",
                                         "pinto beans",
                                         "apples"])


result2.beans == ["green beans", "black beans", "pinto beans"]
result2.otherGroceries == ["milk", "apples"]
2 Likes

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)

Welcome aboard and thank you for sharing your version.

However, a close inspection reveals two problems.

  1. Creation of string arrays

var beans = String
var notBeans = String

  1. Incorrect use of the lowercased function

if checked.lowercased().hasSuffix (“beans”) {
beans.append ((checked))
} else {
notBeans.append (checked)
}

Check the String documentation:

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)
1 Like
checked.lowercased().hasSuffix(“beans”) 

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.

1 Like

Thanks for the clarification.