From 4c48068782d4200fdeeee08b97bd6491da0847e7 Mon Sep 17 00:00:00 2001 From: Paul Yi Date: Tue, 26 Feb 2019 15:53:28 -0800 Subject: [PATCH 1/3] Initial commit --- .../GenericsChallenge.playground/Contents.swift | 3 +++ .../GenericsChallenge.playground/contents.xcplayground | 4 ++++ .../GenericsChallenge.playground/timeline.xctimeline | 6 ++++++ 3 files changed, 13 insertions(+) create mode 100644 Sprint 7/Generics (7.2)/GenericsChallenge.playground/Contents.swift create mode 100644 Sprint 7/Generics (7.2)/GenericsChallenge.playground/contents.xcplayground create mode 100644 Sprint 7/Generics (7.2)/GenericsChallenge.playground/timeline.xctimeline diff --git a/Sprint 7/Generics (7.2)/GenericsChallenge.playground/Contents.swift b/Sprint 7/Generics (7.2)/GenericsChallenge.playground/Contents.swift new file mode 100644 index 00000000..49c1ff6d --- /dev/null +++ b/Sprint 7/Generics (7.2)/GenericsChallenge.playground/Contents.swift @@ -0,0 +1,3 @@ +import UIKit + +var str = "Hello, playground" diff --git a/Sprint 7/Generics (7.2)/GenericsChallenge.playground/contents.xcplayground b/Sprint 7/Generics (7.2)/GenericsChallenge.playground/contents.xcplayground new file mode 100644 index 00000000..9f5f2f40 --- /dev/null +++ b/Sprint 7/Generics (7.2)/GenericsChallenge.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/Sprint 7/Generics (7.2)/GenericsChallenge.playground/timeline.xctimeline b/Sprint 7/Generics (7.2)/GenericsChallenge.playground/timeline.xctimeline new file mode 100644 index 00000000..bf468afe --- /dev/null +++ b/Sprint 7/Generics (7.2)/GenericsChallenge.playground/timeline.xctimeline @@ -0,0 +1,6 @@ + + + + + From 376aa632c2f0dbdc39552d0232735fcd9e00745e Mon Sep 17 00:00:00 2001 From: Paul Yi Date: Tue, 26 Feb 2019 17:27:06 -0800 Subject: [PATCH 2/3] Create a generic CountedSet struct --- .../Contents.swift | 79 ++++++++++++++++++- .../timeline.xctimeline | 6 -- 2 files changed, 78 insertions(+), 7 deletions(-) delete mode 100644 Sprint 7/Generics (7.2)/GenericsChallenge.playground/timeline.xctimeline diff --git a/Sprint 7/Generics (7.2)/GenericsChallenge.playground/Contents.swift b/Sprint 7/Generics (7.2)/GenericsChallenge.playground/Contents.swift index 49c1ff6d..2ad94128 100644 --- a/Sprint 7/Generics (7.2)/GenericsChallenge.playground/Contents.swift +++ b/Sprint 7/Generics (7.2)/GenericsChallenge.playground/Contents.swift @@ -1,3 +1,80 @@ import UIKit -var str = "Hello, playground" +/// An unordered collection of unique elements that +/// may appear more than once in the collection. +public struct CountedSet { + /// Inserts the given element to the set, with + /// a count of 1 if the element is not yet present + /// or adding 1 to the count if it is. + @discardableResult + public mutating func insert(_ member: Element) -> Int { + _counts[member, default: 0] += 1 + return _counts[member]! + } + + /// Removes one instance of the given element from + /// the set. If the count goes to zero, the element is + /// removed from the set. If the element is not present, + /// the request is ignored. + @discardableResult + public mutating func remove(_ member: Element) -> Int { + + let memberCount = _counts[member, default: 0] + guard memberCount > 0 else { return 0 } + + guard memberCount > 1 else { _counts.removeValue(forKey: member); return 0 } + let newCount = _counts[member]! - 1 + _counts[member] = newCount + + return newCount + } + + /// Access an element count, returning 0 for any + /// element that does not appear in the counted set + public subscript(_ member: Element) -> Int { + return _counts[member, default: 0] + } + + /// Return the number of unique elements in the counted set + public var count: Int { return _counts.keys.count } + + /// A Boolean value that indicates whether the counted set is empty. + public var isEmpty: Bool { + return count == 0 + } + + internal var _counts: [Element: Int] +} + +extension CountedSet: ExpressibleByArrayLiteral { + // + // `ExpressibleByArrayLiteral` conformance + // + + /// Creates a counted set containing the elements of the given array literal. + /// + /// Do not call this initializer directly. It is used by the compiler when + /// you use an array literal. Instead, create a new counted set using an array + /// literal as its value by enclosing a comma-separated list of values in + /// square brackets. You can use an array literal anywhere a set is expected + /// by the type context. + public init(arrayLiteral elements: Element...) { + _counts = [:] + for element in elements { + _counts[element] = _counts[element, default: 0] + 1 + } + } +} + +enum Arrow { case iron, wooden, elven, dwarvish, magic, silver } +var aCountedSet = CountedSet() +aCountedSet[.iron] // 0 +var myCountedSet: CountedSet = [.iron, .magic, .iron, .silver, .iron, .iron] +myCountedSet[.iron] // 4 +myCountedSet.remove(.iron) // 3 +myCountedSet.remove(.dwarvish) // 0 +myCountedSet.remove(.magic) // 0 + + + + diff --git a/Sprint 7/Generics (7.2)/GenericsChallenge.playground/timeline.xctimeline b/Sprint 7/Generics (7.2)/GenericsChallenge.playground/timeline.xctimeline deleted file mode 100644 index bf468afe..00000000 --- a/Sprint 7/Generics (7.2)/GenericsChallenge.playground/timeline.xctimeline +++ /dev/null @@ -1,6 +0,0 @@ - - - - - From 5f0f124b8161b89f9c59cd23299c868f1f56f135 Mon Sep 17 00:00:00 2001 From: Paul Yi Date: Wed, 27 Feb 2019 19:46:40 -0800 Subject: [PATCH 3/3] Write solution --- .../Contents.swift | 88 +++++++++++++++++++ .../contents.xcplayground | 4 + .../timeline.xctimeline | 11 +++ 3 files changed, 103 insertions(+) create mode 100644 Sprint 7/Concurrency II (7.3)/HungryDevelopers.playground/Contents.swift create mode 100644 Sprint 7/Concurrency II (7.3)/HungryDevelopers.playground/contents.xcplayground create mode 100644 Sprint 7/Concurrency II (7.3)/HungryDevelopers.playground/timeline.xctimeline diff --git a/Sprint 7/Concurrency II (7.3)/HungryDevelopers.playground/Contents.swift b/Sprint 7/Concurrency II (7.3)/HungryDevelopers.playground/Contents.swift new file mode 100644 index 00000000..eaac43ef --- /dev/null +++ b/Sprint 7/Concurrency II (7.3)/HungryDevelopers.playground/Contents.swift @@ -0,0 +1,88 @@ +import UIKit + +import Foundation + +// 1 +class Spoon { + // 2 + func pickUp() { + while isPickedUp { continue } + lock.lock() + isPickedUp = true + lock.unlock() + } + + func putDown() { + lock.lock() + isPickedUp = false + lock.unlock() + } + + private let lock = NSLock() + private var isPickedUp: Bool = false +} +// 3 +class Developer { + + init(leftSpoon: Spoon, rightSpoon: Spoon, developer: String) { + self.leftSpoon = leftSpoon + self.rightSpoon = rightSpoon + self.developer = developer + } + // 5 + func think() { + print("\(developer) is thinking.") + usleep(useconds_t(Int.random(in: 1...1000))) + + leftSpoon.pickUp() + print("\(developer) picked up left spoon.") + + rightSpoon.pickUp() + print("\(developer) picked up right spoon.") + + return + } + // 6 + func eat() { + print("\(developer) is eating.") + usleep(useconds_t(Int.random(in: 1...1000))) + + leftSpoon.putDown() + print("\(developer) put down left spoon.") + + rightSpoon.putDown() + print("\(developer) put down right spoon.") + } + + func run() { + while true { + think() + eat() + } + } + + var leftSpoon: Spoon + var rightSpoon: Spoon + let developer: String +} + +// 7 +var spoon1 = Spoon() +var spoon2 = Spoon() +var spoon3 = Spoon() +var spoon4 = Spoon() +var spoon5 = Spoon() + +var developer1 = Developer(leftSpoon: spoon1, rightSpoon: spoon2, developer: "Paul") +var developer2 = Developer(leftSpoon: spoon2, rightSpoon: spoon3, developer: "Nate") +var developer3 = Developer(leftSpoon: spoon3, rightSpoon: spoon4, developer: "Jaspal") +var developer4 = Developer(leftSpoon: spoon4, rightSpoon: spoon5, developer: "Frulwinn") +var developer5 = Developer(leftSpoon: spoon5, rightSpoon: spoon1, developer: "Julian") + +let developers = [developer1, developer2, developer3, developer4, developer5] + +DispatchQueue.concurrentPerform(iterations: 5) { + // 8 + developers[$0].run() + +} diff --git a/Sprint 7/Concurrency II (7.3)/HungryDevelopers.playground/contents.xcplayground b/Sprint 7/Concurrency II (7.3)/HungryDevelopers.playground/contents.xcplayground new file mode 100644 index 00000000..9f5f2f40 --- /dev/null +++ b/Sprint 7/Concurrency II (7.3)/HungryDevelopers.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/Sprint 7/Concurrency II (7.3)/HungryDevelopers.playground/timeline.xctimeline b/Sprint 7/Concurrency II (7.3)/HungryDevelopers.playground/timeline.xctimeline new file mode 100644 index 00000000..c0a6b326 --- /dev/null +++ b/Sprint 7/Concurrency II (7.3)/HungryDevelopers.playground/timeline.xctimeline @@ -0,0 +1,11 @@ + + + + + + +