Skip to content

Commit

Permalink
Add playgrounds where missing
Browse files Browse the repository at this point in the history
  • Loading branch information
hollance committed Jan 29, 2016
1 parent 988c0f1 commit e1198f8
Show file tree
Hide file tree
Showing 12 changed files with 159 additions and 0 deletions.
49 changes: 49 additions & 0 deletions Boyer-Moore/BoyerMoore.playground/Contents.swift
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
4 changes: 4 additions & 0 deletions Boyer-Moore/BoyerMoore.playground/contents.xcplayground
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>
6 changes: 6 additions & 0 deletions Boyer-Moore/BoyerMoore.playground/timeline.xctimeline
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>
19 changes: 19 additions & 0 deletions Insertion Sort/InsertionSort.playground/Contents.swift
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 Insertion Sort/InsertionSort.playground/contents.xcplayground
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>
6 changes: 6 additions & 0 deletions Insertion Sort/InsertionSort.playground/timeline.xctimeline
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>
22 changes: 22 additions & 0 deletions Selection Sort/SelectionSort.playground/Contents.swift
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 Selection Sort/SelectionSort.playground/contents.xcplayground
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>
6 changes: 6 additions & 0 deletions Selection Sort/SelectionSort.playground/timeline.xctimeline
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>
29 changes: 29 additions & 0 deletions Two-Sum Problem/2Sum.playground/Contents.swift
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
4 changes: 4 additions & 0 deletions Two-Sum Problem/2Sum.playground/contents.xcplayground
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>
6 changes: 6 additions & 0 deletions Two-Sum Problem/2Sum.playground/timeline.xctimeline
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>

0 comments on commit e1198f8

Please sign in to comment.