Skip to content

Commit a362c9b

Browse files
committed
Completed Swift 2.2 updates..
- updates to sorting algorithms - updates to math related sequences - minor update to graph algorithm helper function
1 parent b580133 commit a362c9b

File tree

8 files changed

+89
-46
lines changed

8 files changed

+89
-46
lines changed

Source/Factories/Graph.swift

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,18 @@ public class SwiftGraph {
8585
/* reverse the sequence of paths given the shortest path.
8686
process analagous to reversing a linked list. */
8787

88-
func reversePath(var head: Path!, source: Vertex) -> Path! {
88+
func reversePath(head: Path!, source: Vertex) -> Path! {
8989

90-
if head == nil {
91-
return nil
90+
91+
guard head != nil else {
92+
return head
9293
}
9394

95+
//mutated copy
96+
var output = head
97+
9498

95-
var current: Path! = head
99+
var current: Path! = output
96100
var prev: Path!
97101
var next: Path!
98102

@@ -112,10 +116,10 @@ public class SwiftGraph {
112116
sourcePath.previous = prev
113117
sourcePath.total = nil
114118

115-
head = sourcePath
119+
output = sourcePath
116120

117121

118-
return head
122+
return output
119123

120124
}
121125

Source/Factories/Math.swift

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ class Math {
1919
func fib(n: Int) -> Array<Int>! {
2020

2121

22-
if n < 2 {
22+
//check trivial condition
23+
guard n > 2 else {
2324
return nil
2425
}
2526

27+
2628
//initialize the sequence
2729
var sequence: Array<Int> = [0, 1]
2830

@@ -41,52 +43,36 @@ class Math {
4143
return sequence
4244

4345
}
44-
45-
46-
47-
func someFunction(parameterWithDefault: Int = 12) {
48-
// function body goes here
49-
// if no arguments are passed to the function call,
50-
// value of parameterWithDefault is 12
51-
52-
someFunction(9)
53-
}
5446

5547

5648

57-
/*
58-
TODO: Recursive functions with parameters are still allowed in Swift. It's
59-
only the mutation of the variable within the method scope that isn't allowed.
60-
Just fix by providing a secondary "mutated" output variable..
61-
*/
62-
63-
6449
//build fibonacci sequence to a specified position - recursive
65-
func fib(n: Int, var sequence: Array<Int> = [0, 1]) {
66-
67-
someFunction()
68-
someFunction(6)
50+
func fib(n: Int, sequence: Array<Int> = [0, 1]) {
6951

70-
//initialize sequence
71-
if n < 2 {
52+
53+
//check trivial condition
54+
guard n > 2 else {
7255
return
7356
}
7457

58+
59+
//mutated copy
60+
var output = sequence
7561

76-
let i: Int = sequence.count
62+
let i: Int = output.count
7763

7864

7965
//set base condition
8066
if i == n {
8167
return
8268
}
8369

84-
let results: Int = sequence[i - 1] + sequence[i - 2]
85-
sequence.append(results)
70+
let results: Int = output[i - 1] + output[i - 2]
71+
output.append(results)
8672

8773

8874
//set iteration
89-
fib(n, sequence: sequence)
75+
fib(n, sequence: output)
9076

9177
}
9278

@@ -95,7 +81,9 @@ class Math {
9581
//build fibonacci sequence to a specified position - trailing closure
9682
func fib(n: Int, formula: Array<Int> -> Int) -> Array<Int>! {
9783

98-
if n < 2 {
84+
85+
//check trivial condition
86+
guard n > 2 else {
9987
return nil
10088
}
10189

Source/Factories/Sorting.swift

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,12 @@ public class Sorting {
110110

111111
for primaryindex in 0..<output.count {
112112

113+
113114
let key = output[primaryindex]
115+
114116
var secondaryindex = primaryindex
115117

118+
116119
while secondaryindex > -1 {
117120

118121
print("comparing \(key) and \(output[secondaryindex])")
@@ -241,8 +244,9 @@ public class Sorting {
241244
*/
242245

243246
func bubbleSortG<T: Comparable>(sequence: [T]) -> [T] {
247+
244248

245-
// immediately return the trivial cases
249+
//return trvial case
246250
guard sequence.count > 1 else {
247251
return sequence
248252
}
@@ -341,7 +345,8 @@ public class Sorting {
341345

342346
func selectionSortG<T: Comparable>(sequence: [T]) -> [T] {
343347

344-
// immediately return the trivial cases
348+
349+
//check for trivial case
345350
guard sequence.count > 1 else {
346351
return sequence
347352
}
@@ -352,8 +357,9 @@ public class Sorting {
352357

353358

354359
for primaryindex in 0..<output.count {
360+
355361

356-
362+
//set indicies
357363
var minimum = primaryindex
358364
var secondaryindex = primaryindex + 1
359365

@@ -384,7 +390,7 @@ public class Sorting {
384390
}
385391

386392

387-
//MARK: - Other Sorting Algorithms
393+
//MARK: - Other Sorting Algorithms - to be refactored
388394

389395

390396

@@ -393,6 +399,8 @@ public class Sorting {
393399
// and moves values to the left or right of the pivot based on their value
394400
// it works recursively so that either side will be eventually sorted back to the top
395401

402+
/*
403+
396404
func quickSort(sequence:[Int]) -> [Int] {
397405

398406
// immediately return the trivial cases
@@ -478,14 +486,22 @@ public class Sorting {
478486
*/
479487

480488
if (leftCount < left.count && (rightCount >= right.count || left[leftCount] <= right[rightCount])) {
489+
481490
sortedArray.append(left[leftCount++])
491+
482492
} else if (rightCount < right.count && (leftCount >= left.count || right[rightCount] < left[leftCount])) {
493+
483494
sortedArray.append(right[rightCount++])
495+
484496
}
485497
}
486498

487499
return sortedArray
488500
}
489-
501+
502+
*/
490503

504+
491505
}
506+
507+

SwiftStructures.xcodeproj/xcuserdata/waynebishop.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,14 +154,46 @@
154154
ignoreCount = "0"
155155
continueAfterRunningActions = "No"
156156
filePath = "Source/Factories/Sorting.swift"
157-
timestampString = "481680628.179238"
157+
timestampString = "481755775.341086"
158158
startingColumnNumber = "9223372036854775807"
159159
endingColumnNumber = "9223372036854775807"
160-
startingLineNumber = "135"
161-
endingLineNumber = "135"
160+
startingLineNumber = "138"
161+
endingLineNumber = "138"
162162
landmarkName = "insertionSort(_:)"
163163
landmarkType = "5">
164164
</BreakpointContent>
165165
</BreakpointProxy>
166+
<BreakpointProxy
167+
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
168+
<BreakpointContent
169+
shouldBeEnabled = "No"
170+
ignoreCount = "0"
171+
continueAfterRunningActions = "No"
172+
filePath = "SwiftTests/MathTest.swift"
173+
timestampString = "481754655.821472"
174+
startingColumnNumber = "9223372036854775807"
175+
endingColumnNumber = "9223372036854775807"
176+
startingLineNumber = "43"
177+
endingLineNumber = "43"
178+
landmarkName = "testFibRecursive()"
179+
landmarkType = "5">
180+
</BreakpointContent>
181+
</BreakpointProxy>
182+
<BreakpointProxy
183+
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
184+
<BreakpointContent
185+
shouldBeEnabled = "No"
186+
ignoreCount = "0"
187+
continueAfterRunningActions = "No"
188+
filePath = "Source/Factories/Sorting.swift"
189+
timestampString = "481756359.333058"
190+
startingColumnNumber = "9223372036854775807"
191+
endingColumnNumber = "9223372036854775807"
192+
startingLineNumber = "489"
193+
endingLineNumber = "489"
194+
landmarkName = "Sorting"
195+
landmarkType = "3">
196+
</BreakpointContent>
197+
</BreakpointProxy>
166198
</Breakpoints>
167199
</Bucket>

SwiftTests/BloomTest.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,4 @@ class BloomTest: XCTestCase {
175175

176176

177177

178-
179-
180178
}

SwiftTests/MathTest.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class MathTest: XCTestCase {
4040
let positions: Int = 9
4141

4242
//set the number of iterations
43-
math.fib(positions)
43+
math.fib(positions, sequence: [0, 1])
4444

4545

4646
}

SwiftTests/SortingTest.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,10 @@ class SortingTest: XCTestCase {
145145
}
146146

147147

148+
//MARK: - Other sorting algorithms - to be refactored
148149

150+
151+
/*
149152
func testQuickSort() {
150153

151154
let resultList: Array<Int> = sortTest.quickSort(numberList)
@@ -174,6 +177,8 @@ class SortingTest: XCTestCase {
174177

175178
}
176179

180+
*/
181+
177182

178183

179184
//MARK: Helper Function

0 commit comments

Comments
 (0)