Skip to content

Commit

Permalink
Add examples on Swift 3.0.2 (egonSchiele#11)
Browse files Browse the repository at this point in the history
* 01_binary_search Swift 3.0.2

* 01_binary_search Swift 3.0.2

* add Chapter 2 - 01_selection_sort Swift 3.0.2 example

* 01_binary_search cosmetic note updates Swift 3.0.2

* 03_recursion Swift 3.0.2 examples

* 04_quicksort Swift 3.0.2 examples

* fix typo on quicksort example. Swift 3.0.2

* add  05_hash_tables examples on Swift 3.0.2

* add 01_breadth-first_search Swift 3.0.2 example

* 01_breadth-first_search fixing typo Swift 3.0.2

* add 01_dijkstras_algorithm on Swift 3.0.2

* add 08_greedy_algorithms Swift 3.0.2 example

* 01_longest_common_subsequence Swift 3.0.2 example
  • Loading branch information
AnthonyBY authored and egonSchiele committed Mar 15, 2017
1 parent 62ed616 commit 12265a8
Show file tree
Hide file tree
Showing 21 changed files with 467 additions and 0 deletions.
31 changes: 31 additions & 0 deletions 01_introduction_to_algorithms/swift/01_binary_search.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import Foundation

// Note: If you aren’t familiar with Comparable, please check out “Generics” chapter in Swift book
func binarySearch <T: Comparable>(_ list: [T], item: T) -> Int? {
// low and high keep track of which part of the list you'll search in.
var low = 0
var high = list.count - 1
// While you haven't narrowed it down to one element ...
while low <= high {
//... check the middle element
let mid = low + (high - low) / 2
let guess = list[mid]
// Found the item.
if guess == item {
return mid
}
// The guess was too high.
if guess > item {
high = mid - 1
} else {
low = mid + 1
}
}

return nil
}

let myList = [1, 3, 5, 7, 9]
print(binarySearch(myList, item: 3) ?? "Not Found") // => 1
print(binarySearch(myList, item: -1) ?? "Not Found") // => Not Found

38 changes: 38 additions & 0 deletions 02_selection_sort/swift/01_selection_sort.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import Foundation

// Finds the smallest value in an array
func findSmallestIndex <T: Comparable> (_ arr: [T]) -> Int {
// Stores the smallest value
var smallest = arr[0]
// We don't need any calculation if the array lenght is 1
if arr.count == 1 {
return 0
}
// Stores the index of the smallest value
var smallestIndex = 0
for i in 1...arr.count-1 {
if arr[i] < smallest {
smallest = arr[i]
smallestIndex = i
}
}
return smallestIndex
}

// Sort array
func selectionSort <T: Comparable> (arr: [T]) -> [T] {
var newArr: [T] = []
// We have to make mutableArray reference copy of original array, because Swift 3 doesn't allow to get var parameter
var mutableArr = arr
for _ in 0...mutableArr.count-1 {
//Finds the smallest element in the array and adds it to the new array
let smallestIndex = findSmallestIndex(mutableArr)
newArr.append(mutableArr[smallestIndex])
mutableArr.remove(at: smallestIndex)
}
return newArr
}

print(selectionSort(arr: [5, 3, 6, 2, 10])) // => [2, 3, 5, 6, 10]


16 changes: 16 additions & 0 deletions 03_recursion/swift/01_countdown.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Foundation

func countDown(i : Int) {
print(i)
// base case
if i <= 0 {
return
} else {
// recursive case
countDown(i: i-1)
}
}

countDown(i: 5)


18 changes: 18 additions & 0 deletions 03_recursion/swift/02_greet.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import Foundation

func greet2(name: String) {
print("how are you, \(name) ?")
}

func bye() {
print("ok bye!")
}

func greet(name: String ) {
print("hello, \(name)!")
greet2(name: name)
print("getting ready to say bye...")
bye()
}

greet(name: "adit")
12 changes: 12 additions & 0 deletions 03_recursion/swift/03_factorial.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Foundation

func fact(x: Int) -> Int {
if x == 1 {
return 1
} else {
return x*fact(x: x-1)
}
}


print(fact(x: 5)) // => 120
11 changes: 11 additions & 0 deletions 04_quicksort/swift/01_loop_sum.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Foundation

func sum (_ arr: [Int]) -> Int {
var total = 0
for x in arr {
total += x
}
return total
}

print(sum([1, 2, 3, 4])) // => 10
12 changes: 12 additions & 0 deletions 04_quicksort/swift/02_recursive_sum.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Foundation

func sum(list : [Int]) -> Int {
if list.count == 0 {
return 0
}
var tempArray = list
tempArray.remove(at: 0)
return list[0] + sum(list: tempArray)
}

print(sum(list: [1, 2, 3, 4])) // => 10
12 changes: 12 additions & 0 deletions 04_quicksort/swift/03_recursive_count.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Foundation

func count(list : [Int]) -> Int {
if list.count == 0 {
return 0
}
var tempArray = list
tempArray.remove(at: 0)
return 1 + count(list: tempArray)
}

print(count(list: [1, 2, 3, 4])) // => 4
13 changes: 13 additions & 0 deletions 04_quicksort/swift/04_recursive_max.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Foundation

func max(list : [Int]) -> Int {
if list.count == 2 {
return (list[0] > list[1]) ? list[0] : list[1]
}
var tempArray = list
tempArray.remove(at: 0)
let subMax = max(list: tempArray)
return (list[0] > subMax) ? list[0] : subMax
}

print(max(list: [1, 5, 10, 25, 16, 1])) // => 25
21 changes: 21 additions & 0 deletions 04_quicksort/swift/05_quicksort.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import Foundation

//The following implementation of quick sort is little more classic than described in the book, but we have two use this one because of some “slice” feature limitation with array on Swift 3. Main concept is the same
func quicksort <T : Comparable> (_ array : [T]) -> [T] {
if (array.count < 2) {
// base case, arrays with 0 or 1 element are already "sorted"
return array
} else {
// recursive case
let pivot = array[0]
// sub-array of all the elements less than the pivot
let less = array.filter { $0 < pivot }
// sub-array of all the elements equal to the pivot
let equal = array.filter { $0 == pivot }
// sub-array of all the elements greater than the pivot
let greater = array.filter { $0 > pivot }
return quicksort(less) + equal + quicksort(greater)
}
}

print(quicksort([1, 5, 10, 25, 16, 1])) // => [1, 1, 5, 10, 16, 25]
18 changes: 18 additions & 0 deletions 05_hash_tables/swift/01_price_of_groceries.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import Foundation

var book = [String: Double]()
// an apple costs 67 cents
book["apple"] = 0.67
//# milk costs $1.49
book["milk"] = 1.49
book["avacado"] = 1.49
print(book) // => ["avacado": 1.49, "apple": 0.67000000000000004, "milk": 1.49]

// Qustion: Why is "apple" is 0.67000000000000004 intsted of 0.67?
// Answer: Double cannot store the value 0.67 exactly. Swift uses (like many other languages) binary floating point numbers according to the IEEE 754 standard.
// This topic is not related with Algorithms, but you can play with .description and .debugDescription for making workarrounds
print(book["apple"]?.description ?? "Not exist") // => 0.67
print(book["apple"]?.debugDescription ?? "Not exist") // => 0.67000000000000004



18 changes: 18 additions & 0 deletions 05_hash_tables/swift/02_check_voter.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import Foundation

var voter = [String : Bool]()

func checkVoter(_ name : String) {
if voter[name] != nil {
print("kick them out!")
} else {
voter[name] = true
print("let them vote!")
}
}

checkVoter("tom")
checkVoter("mike")
checkVoter("mike")


93 changes: 93 additions & 0 deletions 06_breadth-first_search/swift/01_breadth-first_search.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import Foundation

// As I can see Swift doesn't have Queue default implementation, so we have to use custom on, Degue structure from Swift Algorithm Club
// https://github.com/raywenderlich/swift-algorithm-club/tree/master/Deque
public struct Deque<T> {
private var array = [T]()

public var isEmpty: Bool {
return array.isEmpty
}

public var count: Int {
return array.count
}

public mutating func enqueue(_ element: T) {
array.append(element)
}

public mutating func enqueueFront(_ element: T) {
array.insert(element, at: 0)
}

public mutating func dequeue() -> T? {
if isEmpty {
return nil
} else {
return array.removeFirst()
}
}

public mutating func dequeueBack() -> T? {
if isEmpty {
return nil
} else {
return array.removeLast()
}
}

public func peekFront() -> T? {
return array.first
}

public func peekBack() -> T? {
return array.last
}
}

func persionIsSeller(name: String) -> Bool {
return name.characters.last == "m"
}

var graph = [String : [String]]()
graph["you"] = ["alice", "bob", "claire"]
graph["bob"] = ["anuj", "peggy"]
graph["alice"] = ["peggy"]
graph["claire"] = ["thom", "jonny"]
graph["anuj"] = []
graph["peggy"] = []
graph["thom"] = []
graph["jonny"] = []

func search(name: String) -> Bool {
var searchQueue = Deque<String>()
//Swift Note: Our custom Deque doesn't have possibility to add new element as array so we have to add elements one by one (insted of +=graph["person"] in the book example)
for string in graph[name]! {
searchQueue.enqueue(string)
}
// This array is how you keep track of which people you've searched before.
var searched = [String]()
while !searchQueue.isEmpty {
let person = searchQueue.dequeue()
// Only search this person if you haven't already searched them
if !searched.contains(person!) {
if persionIsSeller(name: person!) {
print("\(person!) is a mango seller!")
return true
} else {
for string in graph[person!]! {
searchQueue.enqueue(string)
}
// Marks this person as searched
searched.append(person!)
}
}
}

return false
}

if search(name: "you") == false {
print("Mango seller Not Found!")
} // => thom is a mango seller!
Loading

0 comments on commit 12265a8

Please sign in to comment.