Skip to content

Commit 88ac573

Browse files
committed
small bits of cleanup
1 parent c2522c0 commit 88ac573

File tree

2 files changed

+29
-32
lines changed

2 files changed

+29
-32
lines changed

Classic Computer Science Problems in Swift.playground/Pages/Chapter 3.xcplaygroundpage/Contents.swift

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ open class Constraint <V: Hashable, D> {
6161
return true
6262
}
6363
/// The variables that make up the constraint.
64-
var vars: [V] {return []}
64+
var vars: [V] { return [] }
6565
}
6666

6767
/// the meat of the backtrack algorithm - a recursive depth first search
@@ -109,7 +109,7 @@ func isConsistent<V, D>(variable: V, value: D, assignment: Dictionary<V, D>, csp
109109
final class MapColoringConstraint: Constraint <String, String> {
110110
let place1: String
111111
let place2: String
112-
final override var vars: [String] {return [place1, place2]}
112+
final override var vars: [String] { return [place1, place2] }
113113

114114
init(place1: String, place2: String) {
115115
self.place1 = place1
@@ -127,8 +127,7 @@ final class MapColoringConstraint: Constraint <String, String> {
127127
}
128128
}
129129

130-
let variables: [String] = ["Western Australia", "Northern Territory",
131-
"South Australia", "Queensland", "New South Wales", "Victoria", "Tasmania"]
130+
let variables: [String] = ["Western Australia", "Northern Territory", "South Australia", "Queensland", "New South Wales", "Victoria", "Tasmania"]
132131
var domains = Dictionary<String, [String]>()
133132
for variable in variables {
134133
domains[variable] = ["r", "g", "b"]
@@ -154,7 +153,7 @@ if let solution = backtrackingSearch(csp: csp) {
154153

155154
final class QueensConstraint: Constraint <Int, Int> {
156155
let columns: [Int]
157-
final override var vars: [Int] {return columns}
156+
final override var vars: [Int] { return columns }
158157

159158
init(columns: [Int]) {
160159
self.columns = columns
@@ -264,7 +263,7 @@ func generateDomain(word: String, grid: Grid) -> [[GridLocation]] {
264263

265264
final class WordSearchConstraint: Constraint <String, [GridLocation]> {
266265
let words: [String]
267-
final override var vars: [String] {return words}
266+
final override var vars: [String] { return words }
268267

269268
init(words: [String]) {
270269
self.words = words
@@ -279,33 +278,34 @@ final class WordSearchConstraint: Constraint <String, [GridLocation]> {
279278
}
280279
}
281280

282-
// Commented out because it takes a long time to execute! Uncomment all of the following
281+
// May be commented out because it takes a long time to execute!
282+
// Uncomment all of the following
283283
// lines to see the word search in action.
284284

285-
//let words: [String] = ["MATTHEW", "JOE", "MARY", "SARAH", "SALLY"]
286-
//var locations = Dictionary<String, [[GridLocation]]>()
287-
//for word in words {
288-
// locations[word] = generateDomain(word: word, grid: grid)
289-
//}
290-
//
291-
//var wordsearch = CSP<String, [GridLocation]>(variables: words, domains: locations)
292-
//wordsearch.addConstraint(WordSearchConstraint(words: words))
293-
//if let solution = backtrackingSearch(csp: wordsearch) {
294-
// for (word, gridLocations) in solution {
295-
// let gridLocs = arc4random_uniform(2) > 0 ? gridLocations : gridLocations.reversed() // randomly reverse word half the time
296-
// for (index, letter) in word.characters.enumerated() {
297-
// let (row, col) = (gridLocs[index].row, gridLocations[index].col)
298-
// grid[row][col] = letter
299-
// }
300-
// }
301-
// printGrid(grid)
302-
//} else { print("Couldn't find solution!") }
285+
let words: [String] = ["MATTHEW", "JOE", "MARY", "SARAH", "SALLY"]
286+
var locations = Dictionary<String, [[GridLocation]]>()
287+
for word in words {
288+
locations[word] = generateDomain(word: word, grid: grid)
289+
}
290+
291+
var wordsearch = CSP<String, [GridLocation]>(variables: words, domains: locations)
292+
wordsearch.addConstraint(WordSearchConstraint(words: words))
293+
if let solution = backtrackingSearch(csp: wordsearch) {
294+
for (word, gridLocations) in solution {
295+
let gridLocs = arc4random_uniform(2) > 0 ? gridLocations : gridLocations.reversed() // randomly reverse word half the time
296+
for (index, letter) in word.characters.enumerated() {
297+
let (row, col) = (gridLocs[index].row, gridLocations[index].col)
298+
grid[row][col] = letter
299+
}
300+
}
301+
printGrid(grid)
302+
} else { print("Couldn't find solution!") }
303303

304304
/// ###SEND+MORE=MONEY
305305

306306
final class SendMoreMoneyConstraint: Constraint <Character, Int> {
307307
let letters: [Character]
308-
final override var vars: [Character] {return letters}
308+
final override var vars: [Character] { return letters }
309309
init(variables: [Character]) {
310310
letters = variables
311311
}
@@ -324,13 +324,10 @@ final class SendMoreMoneyConstraint: Constraint <Character, Int> {
324324
let more: Int = m * 1000 + o * 100 + r * 10 + e
325325
let money: Int = m * 10000 + o * 1000 + n * 100 + e * 10 + y
326326
if (send + more) == money {
327-
return true
328-
} else {
329-
return false
327+
return true // answer found
330328
}
331-
} else {
332-
return false
333329
}
330+
return false // this full assignment doesn't work
334331
}
335332

336333
// until we have all of the variables assigned, the assignment is valid

Classic Computer Science Problems in Swift.playground/Pages/Chapter 6.xcplaygroundpage/Contents.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ public final class KMeans<PointType: DataPoint> {
191191
}
192192
}
193193

194-
public func run(maxIterations: UInt = UInt.max) -> [Cluster] {
194+
public func run(maxIterations: UInt = 100) -> [Cluster] {
195195
for iteration in 0..<maxIterations {
196196
clusters.forEach{ $0.points.removeAll() } // clear all clusters
197197
assignClusters() // find clusters each is closest to - assign

0 commit comments

Comments
 (0)