Skip to content

Commit e1198f8

Browse files
committed
Add playgrounds where missing
1 parent 988c0f1 commit e1198f8

File tree

12 files changed

+159
-0
lines changed

12 files changed

+159
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//: Playground - noun: a place where people can play
2+
3+
extension String {
4+
func indexOf(pattern: String) -> String.Index? {
5+
let patternLength = pattern.characters.count
6+
assert(patternLength > 0)
7+
assert(patternLength <= self.characters.count)
8+
9+
var skipTable = [Character: Int]()
10+
for (i, c) in pattern.characters.enumerate() {
11+
skipTable[c] = patternLength - i - 1
12+
}
13+
14+
let p = pattern.endIndex.predecessor()
15+
let lastChar = pattern[p]
16+
var i = self.startIndex.advancedBy(patternLength - 1)
17+
18+
func backwards() -> String.Index? {
19+
var q = p
20+
var j = i
21+
while q > pattern.startIndex {
22+
j = j.predecessor()
23+
q = q.predecessor()
24+
if self[j] != pattern[q] { return nil }
25+
}
26+
return j
27+
}
28+
29+
while i < self.endIndex {
30+
let c = self[i]
31+
if c == lastChar {
32+
if let k = backwards() { return k }
33+
i = i.successor()
34+
} else {
35+
i = i.advancedBy(skipTable[c] ?? patternLength)
36+
}
37+
}
38+
return nil
39+
}
40+
}
41+
42+
// A few simple tests
43+
44+
let s = "Hello, World"
45+
s.indexOf("World") // 7
46+
47+
// Input:
48+
let animals = "🐶🐔🐷🐮🐱"
49+
animals.indexOf("🐮") // 6
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='osx'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Timeline
3+
version = "3.0">
4+
<TimelineItems>
5+
</TimelineItems>
6+
</Timeline>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//: Playground - noun: a place where people can play
2+
3+
func insertionSort<T>(array: [T], _ isOrderedBefore: (T, T) -> Bool) -> [T] {
4+
var a = array
5+
for x in 1..<a.count {
6+
var y = x
7+
let temp = a[y]
8+
while y > 0 && isOrderedBefore(temp, a[y - 1]) {
9+
a[y] = a[y - 1]
10+
y -= 1
11+
}
12+
a[y] = temp
13+
}
14+
return a
15+
}
16+
17+
let list = [ 10, -1, 3, 9, 2, 27, 8, 5, 1, 3, 0, 26 ]
18+
insertionSort(list, <)
19+
insertionSort(list, >)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='osx'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Timeline
3+
version = "3.0">
4+
<TimelineItems>
5+
</TimelineItems>
6+
</Timeline>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//: Playground - noun: a place where people can play
2+
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+
}
19+
20+
let list = [ 10, -1, 3, 9, 2, 27, 8, 5, 1, 3, 0, 26 ]
21+
selectionSort(list, <)
22+
selectionSort(list, >)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='osx'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Timeline
3+
version = "3.0">
4+
<TimelineItems>
5+
</TimelineItems>
6+
</Timeline>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//: Playground - noun: a place where people can play
2+
3+
func twoSumProblem(a: [Int], k: Int) -> ((Int, Int))? {
4+
var i = 0
5+
var j = a.count - 1
6+
7+
while i < j {
8+
let sum = a[i] + a[j]
9+
if sum == k {
10+
return (i, j)
11+
} else if sum < k {
12+
++i
13+
} else {
14+
--j
15+
}
16+
}
17+
return nil
18+
}
19+
20+
let a = [2, 3, 4, 4, 7, 8, 9, 10, 12, 14, 21, 22, 100]
21+
if let (i, j) = twoSumProblem(a, k: 33) {
22+
i // 8
23+
a[i] // 12
24+
j // 10
25+
a[j] // 21
26+
a[i] + a[j] // 33
27+
}
28+
29+
twoSumProblem(a, k: 37) // nil
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='osx'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Timeline
3+
version = "3.0">
4+
<TimelineItems>
5+
</TimelineItems>
6+
</Timeline>

0 commit comments

Comments
 (0)