-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
//: Playground - noun: a place where people can play | ||
|
||
extension String { | ||
func indexOf(pattern: String) -> String.Index? { | ||
let patternLength = pattern.characters.count | ||
assert(patternLength > 0) | ||
assert(patternLength <= self.characters.count) | ||
|
||
var skipTable = [Character: Int]() | ||
for (i, c) in pattern.characters.enumerate() { | ||
skipTable[c] = patternLength - i - 1 | ||
} | ||
|
||
let p = pattern.endIndex.predecessor() | ||
let lastChar = pattern[p] | ||
var i = self.startIndex.advancedBy(patternLength - 1) | ||
|
||
func backwards() -> String.Index? { | ||
var q = p | ||
var j = i | ||
while q > pattern.startIndex { | ||
j = j.predecessor() | ||
q = q.predecessor() | ||
if self[j] != pattern[q] { return nil } | ||
} | ||
return j | ||
} | ||
|
||
while i < self.endIndex { | ||
let c = self[i] | ||
if c == lastChar { | ||
if let k = backwards() { return k } | ||
i = i.successor() | ||
} else { | ||
i = i.advancedBy(skipTable[c] ?? patternLength) | ||
} | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
// A few simple tests | ||
|
||
let s = "Hello, World" | ||
s.indexOf("World") // 7 | ||
|
||
// Input: | ||
let animals = "🐶🐔🐷🐮🐱" | ||
animals.indexOf("🐮") // 6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
<playground version='5.0' target-platform='osx'> | ||
<timeline fileName='timeline.xctimeline'/> | ||
</playground> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<Timeline | ||
version = "3.0"> | ||
<TimelineItems> | ||
</TimelineItems> | ||
</Timeline> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
//: Playground - noun: a place where people can play | ||
|
||
func insertionSort<T>(array: [T], _ isOrderedBefore: (T, T) -> Bool) -> [T] { | ||
var a = array | ||
for x in 1..<a.count { | ||
var y = x | ||
let temp = a[y] | ||
while y > 0 && isOrderedBefore(temp, a[y - 1]) { | ||
a[y] = a[y - 1] | ||
y -= 1 | ||
} | ||
a[y] = temp | ||
} | ||
return a | ||
} | ||
|
||
let list = [ 10, -1, 3, 9, 2, 27, 8, 5, 1, 3, 0, 26 ] | ||
insertionSort(list, <) | ||
insertionSort(list, >) |
4 changes: 4 additions & 0 deletions
4
Insertion Sort/InsertionSort.playground/contents.xcplayground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
<playground version='5.0' target-platform='osx'> | ||
<timeline fileName='timeline.xctimeline'/> | ||
</playground> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<Timeline | ||
version = "3.0"> | ||
<TimelineItems> | ||
</TimelineItems> | ||
</Timeline> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
//: Playground - noun: a place where people can play | ||
|
||
func selectionSort<T>(array: [T], _ isOrderedBefore: (T, T) -> Bool) -> [T] { | ||
guard array.count > 1 else { return array } | ||
var a = array | ||
for x in 0 ..< a.count - 1 { | ||
var lowest = x | ||
for y in x + 1 ..< a.count { | ||
if isOrderedBefore(a[y], a[lowest]) { | ||
lowest = y | ||
} | ||
} | ||
if x != lowest { | ||
swap(&a[x], &a[lowest]) | ||
} | ||
} | ||
return a | ||
} | ||
|
||
let list = [ 10, -1, 3, 9, 2, 27, 8, 5, 1, 3, 0, 26 ] | ||
selectionSort(list, <) | ||
selectionSort(list, >) |
4 changes: 4 additions & 0 deletions
4
Selection Sort/SelectionSort.playground/contents.xcplayground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
<playground version='5.0' target-platform='osx'> | ||
<timeline fileName='timeline.xctimeline'/> | ||
</playground> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<Timeline | ||
version = "3.0"> | ||
<TimelineItems> | ||
</TimelineItems> | ||
</Timeline> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
//: Playground - noun: a place where people can play | ||
|
||
func twoSumProblem(a: [Int], k: Int) -> ((Int, Int))? { | ||
var i = 0 | ||
var j = a.count - 1 | ||
|
||
while i < j { | ||
let sum = a[i] + a[j] | ||
if sum == k { | ||
return (i, j) | ||
} else if sum < k { | ||
++i | ||
} else { | ||
--j | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
let a = [2, 3, 4, 4, 7, 8, 9, 10, 12, 14, 21, 22, 100] | ||
if let (i, j) = twoSumProblem(a, k: 33) { | ||
i // 8 | ||
a[i] // 12 | ||
j // 10 | ||
a[j] // 21 | ||
a[i] + a[j] // 33 | ||
} | ||
|
||
twoSumProblem(a, k: 37) // nil |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
<playground version='5.0' target-platform='osx'> | ||
<timeline fileName='timeline.xctimeline'/> | ||
</playground> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<Timeline | ||
version = "3.0"> | ||
<TimelineItems> | ||
</TimelineItems> | ||
</Timeline> |