Functions: Challenge Solutions


#1

Bronze Challenge

func greetByMiddleNameRefactor(fromFullName name: (first: String, middle: String?, last: String)) {
    guard let middleName = name.middle, middleName.characters.count < 4 else {
        print("Hello there")
        return
    }
    print("Hello, \(middleName)")
}

Silver Challenge

I lowercased the grocery item and searched for the singular version of the beans to account for other forms of spelling i.e. (‘Beans’, ‘beans’, ‘Bean’, ‘bean’, ‘BEAN’, etc). I also chose contains instead of the recommended hasSuffix in order to find all instances of the word. In long string’s this will be slower, but for this use case it’s negligible and a more forgiving search.

func siftBeans(fromGroceryList groceries: [String]) -> (beans: [String], otherGroceries: [String]) {
    var beans = [String]()
    var otherGroceries = [String]()
    for item in groceries {
        if item.lowercased().contains("bean") {
            beans.append(item)
        } else {
            otherGroceries.append(item)
        }
    }
    return (beans, otherGroceries)
}

#2

Hey! Thanks for providing your solutions. It helped me get a better understanding.

For those curious this is what it looks like with the hasSuffix(_: ) function:

//Silver Challenge

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

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

#3

The problem with using contains is that it will also match things like “bean salad” or “bean soup”. If you want to exclude those kinds of entries while still matching both bean and beans, you’d need to do two checks using hasSuffix.

Though I’m a bit curious why anyone would want to buy one single bean…


#4

Here’s what I have so far for the silver challenge. I haven’t looked at your solution yet because I don’t want to just copy it. Any help with mine would be greatly appreciated.

//Silver Challenge

import Cocoa

func siftBeans(fromGroceryList list: [String], hasSuffix: Bool) -> (beans: [String], otherGroceries: String {
var beans = String
var otherGroceries = String
for item in list {
if item.hasSuffix(“beans”) {
beans.append(item)
} else {
otherGroceries.append(item)
}
}
return (beans, otherGroceries)
}
let result = siftBeans(fromGroceryList: [“green beans”, “pinto beans”, “black beans”, “apples”, “milk”])
let groceryList = siftBeans(result)
print(“The items with a beans suffix are (groceryList.beans); the other grocery list items are: (groceryList.otherGroceries)”)


#5

***for my code above ^
var beans = String is supposed to have brackets around String and empty parentheses (to assign it to an empty array)…the same goes for otherGroceries.

Again I’d just like some help, not the solution


#6

Update from code above

//Silver Challenge
import Cocoa

func siftBeans(fromGroceryList list: [String]) -> (beans: [String], otherGroceries: [String]){

let list = ["green beans", "milk", "black beans", "pinto beans", "apples"]
var beans: [String] = []
var otherGroceries: [String] = []
for item in list {
if item.hasSuffix("beans") {
   beans.append(item)
} else {
    otherGroceries.append(item)
     }
}
return (beans, otherGroceries)

}

/* having trouble printing the results now…
let groceryList = [“green beans”, “milk”, “black beans”, “pinto beans”, “apples”]
siftBeans(fromGroceryList: list)
print(“The beans list is: (groceryList.beans); the other groceries are: (groceryList.otherGroceries)”)
*/

/* this prints but looks weird…
siftBeans(fromGroceryList: [“green beans”, “milk”, “black beans”, “pinto beans”, “apples”])
print(siftBeans(fromGroceryList: [“green beans”, “milk”, “black beans”, “pinto beans”,“apples”]))
*/


#7

Please post your code properly formatted, by enclosing it between a pair of three back ticks like this:

```
func fooBar () {
}

```

The above input would result in this:

func fooBar () {
}

And, this is how you would tackle the original problem. You must use string interpolation when printing.

func siftBeans (fromGroceryList list: [String]) -> (beans: [String], otherGroceries: [String]){
    
    let list = ["green beans", "milk", "black beans", "pinto beans", "apples"]
    var beans: [String] = []
    var otherGroceries: [String] = []
    for item in list {
        if item.hasSuffix ("beans") {
            beans.append(item)
        } else {
            otherGroceries.append (item)
        }
    }
    return (beans, otherGroceries)
}

 let groceryList = ["green beans", "milk", "black beans", "pinto beans", "apples"]

 // sifting the list
 let result = siftBeans (fromGroceryList: groceryList)

 // printing the result
 print ("beans: \(result.beans)\nothers: \(result.otherGroceries)")
beans: ["green beans", "black beans", "pinto beans"]
others: ["milk", "apples"]


#8

//Bronze Challenge
func greetByMidleName(bronzeChallenge name: (first: String, middle: String?, last: String)) {
if let midleName = name.middle { // first checking it`s exist or nil
guard midleName.count > 4 else {
print(“Hey Wahts Up BRO”)
return
}
print(“Hello MR.:(midleName)”)
}
}
greetByMidleName(bronzeChallenge: (“Andrew”,“Junior”,“Burlak”))

// Silver Challenge
func siftBeans(fromGroceryList list: [String]) -> (beans: [String], otherGroceries: [String]) {
var beans = String
var otherGroceries = String

for grocery in list {
    if grocery.contains("beans") {
        beans.append(grocery)
    } else {
        otherGroceries.append(grocery)
    }
}

return(beans, otherGroceries)
}

let result = siftBeans(fromGroceryList: [“green beans”,
“milk”,
“black beans”,
“pinto beans”,
“apples”])

result.beans == [“green beans”, “black beans”, “pinto beans”] // true
result.otherGroceries == [“milk”, “apples”] // true

print(result)