Advertisement
KonstantyNil

Untitled

Aug 30th, 2017
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.55 KB | None | 0 0
  1. //«Write a function called siftBeans(fromGroceryList:) that takes a grocery list (as an array of strings) and “sifts out” //the beans from the other groceries. The function should take one argument that has a parameter name called list, and it //should return a named tuple of the type (beans: [String], otherGroceries: [String]).
  2. //Here is an example of how you should be able to call your function and what the result should be:
  3. //let result = siftBeans(fromGroceryList: ["green beans",
  4. //                                         "milk",
  5.  //                                        "black beans",
  6.  //                                        "pinto beans",
  7.   //                                       "apples"]
  8. //
  9. //result.beans == ["green beans", "black beans", "pinto beans"] // true
  10. //result.otherGroceries == ["milk", "apples"] // true
  11.  
  12. func siftBeans(fromGroceryList list: [String]) -> (beans: [String], otherGroceries: [String]) {
  13.     var beans = [String]()
  14.     var otherGroceries = [String]()
  15.    
  16.     for product in list {
  17.         if product.hasSuffix("beans") {
  18.             beans.append(product)
  19.         } else {
  20.             otherGroceries.append(product)
  21.         }
  22.     }
  23.    
  24.     return (beans, otherGroceries)
  25. }
  26.  
  27. let result = siftBeans(fromGroceryList: ["green beans",
  28.                                            "milk",
  29.                                            "black beans",
  30.                                            "pinto beans",
  31.                                            "apples"])
  32. print(result.beans)
  33. print(result.otherGroceries)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement