Skip to content

Commit 2f339ad

Browse files
authored
Merge branch 'master' into Swift4-updates
2 parents db1fc17 + 721ef3c commit 2f339ad

File tree

24 files changed

+221
-155
lines changed

24 files changed

+221
-155
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
language: objective-c
2-
osx_image: xcode8.3
2+
osx_image: xcode9
33
# sudo: false
44

55
install:

Array2D/Array2D.playground/Contents.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
// last checked with Xcode 9.0b4
2+
#if swift(>=4.0)
3+
print("Hello, Swift 4!")
4+
#endif
5+
16
/*
27
Two-dimensional array with a fixed number of rows and columns.
38
This is mostly handy for games that are played on a grid, such as chess.

Breadth-First Search/BreadthFirstSearch.playground/Pages/Simple example.xcplaygroundpage/Contents.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
// last checked with Xcode 9.0b4
2+
#if swift(>=4.0)
3+
print("Hello, Swift 4!")
4+
#endif
5+
16
func breadthFirstSearch(_ graph: Graph, source: Node) -> [String] {
27
var queue = Queue<Node>()
38
queue.enqueue(source)

Depth-First Search/DepthFirstSearch.playground/Pages/Simple Example.xcplaygroundpage/Contents.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
// last checked with Xcode 9.0b4
2+
#if swift(>=4.0)
3+
print("Hello, Swift 4!")
4+
#endif
5+
16
func depthFirstSearch(_ graph: Graph, source: Node) -> [String] {
27
var nodesExplored = [source.label]
38
source.visited = true

Linear Search/LinearSearch.playground/Contents.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ print("Hello, Swift 4!")
66
#endif
77

88
func linearSearch<T: Equatable>(_ array: [T], _ object: T) -> Int? {
9-
for (index, obj) in array.enumerated() where obj == object {
10-
return index
11-
}
12-
return nil
9+
for (index, obj) in array.enumerated() where obj == object {
10+
return index
11+
}
12+
return nil
1313
}
1414

1515
let array = [5, 2, 4, 7]

Selection Sort/SelectionSort.playground/Contents.swift

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,9 @@
11
//: Playground - noun: a place where people can play
22

3-
func selectionSort<T>(_ array: [T], _ isOrderedBefore: (T, T) -> Bool) -> [T] {
4-
guard array.count > 1 else { return array }
5-
var a = array
6-
for x in 0 ..< a.count - 1 {
7-
var lowest = x
8-
for y in x + 1 ..< a.count {
9-
if isOrderedBefore(a[y], a[lowest]) {
10-
lowest = y
11-
}
12-
}
13-
if x != lowest {
14-
swap(&a[x], &a[lowest])
15-
}
16-
}
17-
return a
18-
}
3+
// last checked with Xcode 9.0b4
4+
#if swift(>=4.0)
5+
print("Hello, Swift 4!")
6+
#endif
197

208
let list = [ 10, -1, 3, 9, 2, 27, 8, 5, 1, 3, 0, 26 ]
219
selectionSort(list, <)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
public func selectionSort<T: Comparable>(_ array: [T], _ isOrderedBefore: (T, T) -> Bool) -> [T] {
2+
guard array.count > 1 else { return array }
3+
4+
var a = array
5+
for x in 0 ..< a.count - 1 {
6+
7+
// Find the lowest value in the rest of the array.
8+
var lowest = x
9+
for y in x + 1 ..< a.count {
10+
if isOrderedBefore(a[y], a[lowest]) {
11+
lowest = y
12+
}
13+
}
14+
15+
// Swap the lowest value with the current array index.
16+
if x != lowest {
17+
a.swapAt(x, lowest)
18+
}
19+
}
20+
return a
21+
}

Selection Sort/SelectionSort.playground/timeline.xctimeline

Lines changed: 0 additions & 6 deletions
This file was deleted.

Selection Sort/SelectionSort.swift

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
func selectionSort<T>(_ array: [T], _ isOrderedBefore: (T, T) -> Bool) -> [T] {
2-
guard array.count > 1 else { return array }
1+
public func selectionSort<T: Comparable>(_ array: [T], _ isOrderedBefore: (T, T) -> Bool) -> [T] {
2+
guard array.count > 1 else { return array }
33

4-
var a = array
5-
for x in 0 ..< a.count - 1 {
4+
var a = array
5+
for x in 0 ..< a.count - 1 {
66

7-
// Find the lowest value in the rest of the array.
8-
var lowest = x
9-
for y in x + 1 ..< a.count {
10-
if isOrderedBefore(a[y], a[lowest]) {
11-
lowest = y
12-
}
13-
}
7+
// Find the lowest value in the rest of the array.
8+
var lowest = x
9+
for y in x + 1 ..< a.count {
10+
if isOrderedBefore(a[y], a[lowest]) {
11+
lowest = y
12+
}
13+
}
1414

15-
// Swap the lowest value with the current array index.
16-
if x != lowest {
17-
swap(&a[x], &a[lowest])
15+
// Swap the lowest value with the current array index.
16+
if x != lowest {
17+
a.swapAt(x, lowest)
18+
}
1819
}
19-
}
20-
return a
20+
return a
2121
}
Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,15 @@
11
//: Playground - noun: a place where people can play
22

3-
import Foundation
4-
5-
/* Returns a random integer between 0 and n-1. */
6-
public func random(_ n: Int) -> Int {
7-
return Int(arc4random_uniform(UInt32(n)))
8-
}
3+
// last checked with Xcode 9.0b4
4+
#if swift(>=4.0)
5+
print("Hello, Swift 4!")
6+
#endif
97

10-
/* Fisher-Yates / Knuth shuffle */
11-
extension Array {
12-
public mutating func shuffle() {
13-
for i in stride(from: count - 1, through: 1, by: -1) {
14-
let j = random(i + 1)
15-
if i != j {
16-
swap(&self[i], &self[j])
17-
}
18-
}
19-
}
20-
}
8+
import Foundation
219

2210
var list = [ "a", "b", "c", "d", "e", "f", "g" ]
2311
list.shuffle()
2412
list.shuffle()
2513
list.shuffle()
2614

27-
/* Create a new array of numbers that is already shuffled. */
28-
public func shuffledArray(_ n: Int) -> [Int] {
29-
var a = [Int](repeating: 0, count: n)
30-
for i in 0..<n {
31-
let j = random(i + 1)
32-
if i != j {
33-
a[i] = a[j]
34-
}
35-
a[j] = i
36-
}
37-
return a
38-
}
39-
4015
let numbers = shuffledArray(10)

0 commit comments

Comments
 (0)