Silver Challenge


#1

This is my solution to the Silver Challenge

import Cocoa

/* Union chapter */

var groceryBag = Set (["Apples", "Oranges", "Pineapple"])
let friendsGroceryBag = Set(["Bananas", "Cereal", "Milk", "Oranges"])

for grocery in friendsGroceryBag where !groceryBag.contains(grocery)  {
    groceryBag.insert(grocery)
}

print(groceryBag)

/* Intersect chapter */

var roommatesGroceryBag = Set(["Apples", "Bananas", "Cereal", "Toothpaste"])

for grocery in roommatesGroceryBag where !groceryBag.contains(grocery) {
    roommatesGroceryBag.remove(grocery)
}

print(roommatesGroceryBag)

#2

Here is my reworked code:

[code]import Cocoa

var groceryBag: Set = [“Apples”, “Oranges”, “Pineapple”]

for food in groceryBag {
print(food)
}
let hasBananas = groceryBag.contains(“Bananas”)
let friendsGroceryBag = Set([“Bananas”, “Cereal”, “Milk”, “Oranges”])
groceryBag.unionInPlace(friendsGroceryBag)

var roommatesGroceryBag = Set([“Apples”, “Bananas”, “Cereal”, “Toothpaste”])
roommatesGroceryBag.intersectInPlace(groceryBag)

let yourSecondBag = Set([“Berries”, “Yogurt”])
let roommatesSecondBag = Set([“Grapes”, “Honey”])
let disjoint = yourSecondBag.isDisjointWith(roommatesSecondBag)[/code]


#3

Here is my version.

/ Union chapter /

var groceryBag = Set ([“Apples”, “Oranges”, “Pineapple”])

let friendsGroceryBag = Set([“Bananas”, “Cereal”, “Milk”, “Oranges”])

groceryBag.formUnion(friendsGroceryBag)

print(groceryBag) //prints [“Bananas”, “Milk”, “Oranges”, “Pineapple”, “Apples”, “Cereal”]

/ Intersect chapter /

var roommatesGroceryBag = Set([“Apples”, “Bananas”, “Cereal”, “Toothpaste”])

roommatesGroceryBag.formIntersection(groceryBag)

print(roommatesGroceryBag) //prints [“Bananas”, “Apples”, “Cereal”]