From 90514e34bf6092020424039988b6ff3ce239c30b Mon Sep 17 00:00:00 2001 From: Matteo Matassoni Date: Tue, 24 Nov 2015 15:17:51 +0100 Subject: [PATCH 1/4] Assignment 1 - Selection Sort completed --- .../Generic.xcplaygroundpage/Contents.swift | 35 +++++++++++++++--- .../Contents.swift | 36 +++++++++++++++++-- .../Contents.swift | 31 +++++++++++++--- .../Contents.swift | 30 ++++++++++++++-- 4 files changed, 119 insertions(+), 13 deletions(-) diff --git a/Assignments/Sorting/SelectionSort.playground/Pages/Generic.xcplaygroundpage/Contents.swift b/Assignments/Sorting/SelectionSort.playground/Pages/Generic.xcplaygroundpage/Contents.swift index b80bb94..64c4bd7 100644 --- a/Assignments/Sorting/SelectionSort.playground/Pages/Generic.xcplaygroundpage/Contents.swift +++ b/Assignments/Sorting/SelectionSort.playground/Pages/Generic.xcplaygroundpage/Contents.swift @@ -8,10 +8,37 @@ For practicing purposes you may want to rewrite the function from scratch rather */ - - - -//assert(selectionSort(["c", "a", "b"]).isSorted()) +func selectionSort(var array: [T]) -> [T] { + func findMinIndex(array: [T], startingFrom start: Int = 0) -> Int { + var minIndex = start + for (index, value) in array[start.. 1 else { + return array + } + + for i in 0..(var array: [T], @noescape isOrderedBefore: (T, T) -> Bool) -> [T] { + func findMinIndex(array: [T], startingFrom start: Int = 0, @noescape isOrderedBefore: (T, T) -> Bool) -> Int { + var minIndex = start + for (index, value) in array[start.. 1 else { + return array + } + + for i in 0.. [Int] { - // You may declare array argument with var keyword so that it is copied. +func selectionSort(var array: [Int]) -> [Int] { + func findMinIndex(array: [Int], startingFrom start: Int = 0) -> Int { + var minIndex = start + for (index, value) in array[start.. 1 else { return array + } + + for i in 0.. Bool) -> [Generator.Element] { + func findMinIndex(array: [Generator.Element], startingFrom start: Int = 0, @noescape isOrderedBefore: (Generator.Element, Generator.Element) -> Bool) -> Int { + var minIndex = start + for (index, value) in array[start.. 1 else { + return array + } + + for i in 0.. $1.0}).isSorted({$0.0 > $1.0})) +assert([1, 3, 2].selectionSort(<).isSorted()) +assert([1: "b", 2: "a"].selectionSort({$0.0 > $1.0}).isSorted({$0.0 > $1.0})) +print([1: "b", 2: "a"].selectionSort({$0.0 > $1.0})) /*: [Table of Contents](Table%20of%20Contents) | [Previous](@previous) | [Next](@next) From 0a9b3551012b6345187fabba6c37e5f406e4e338 Mon Sep 17 00:00:00 2001 From: Matteo Matassoni Date: Tue, 24 Nov 2015 15:44:42 +0100 Subject: [PATCH 2/4] Improved inner function findMinIndex with guard --- .../Pages/Generic.xcplaygroundpage/Contents.swift | 13 +++++++++---- .../Pass a Closure.xcplaygroundpage/Contents.swift | 13 +++++++++---- .../Selection Sort.xcplaygroundpage/Contents.swift | 13 +++++++++---- .../Contents.swift | 14 +++++++++----- 4 files changed, 36 insertions(+), 17 deletions(-) diff --git a/Assignments/Sorting/SelectionSort.playground/Pages/Generic.xcplaygroundpage/Contents.swift b/Assignments/Sorting/SelectionSort.playground/Pages/Generic.xcplaygroundpage/Contents.swift index 64c4bd7..46eac4d 100644 --- a/Assignments/Sorting/SelectionSort.playground/Pages/Generic.xcplaygroundpage/Contents.swift +++ b/Assignments/Sorting/SelectionSort.playground/Pages/Generic.xcplaygroundpage/Contents.swift @@ -9,7 +9,11 @@ For practicing purposes you may want to rewrite the function from scratch rather func selectionSort(var array: [T]) -> [T] { - func findMinIndex(array: [T], startingFrom start: Int = 0) -> Int { + func findMinIndex(array: [T], startingFrom start: Int = 0) -> Int? { + guard 0..(var array: [T]) -> [T] { } for i in 0..(var array: [T], @noescape isOrderedBefore: (T, T) -> Bool) -> [T] { - func findMinIndex(array: [T], startingFrom start: Int = 0, @noescape isOrderedBefore: (T, T) -> Bool) -> Int { + func findMinIndex(array: [T], startingFrom start: Int = 0, @noescape isOrderedBefore: (T, T) -> Bool) -> Int? { + guard 0..(var array: [T], @noescape isOrderedBefore: (T, T) -> Bool) } for i in 0.. [Int] { - func findMinIndex(array: [Int], startingFrom start: Int = 0) -> Int { + func findMinIndex(array: [Int], startingFrom start: Int = 0) -> Int? { + guard 0.. [Int] { } for i in 0.. Bool) -> [Generator.Element] { - func findMinIndex(array: [Generator.Element], startingFrom start: Int = 0, @noescape isOrderedBefore: (Generator.Element, Generator.Element) -> Bool) -> Int { + func findMinIndex(array: [Generator.Element], startingFrom start: Int = 0, @noescape isOrderedBefore: (Generator.Element, Generator.Element) -> Bool) -> Int? { + guard 0.. $1.0}).isSorted({$0.0 > $1.0})) -print([1: "b", 2: "a"].selectionSort({$0.0 > $1.0})) /*: [Table of Contents](Table%20of%20Contents) | [Previous](@previous) | [Next](@next) From aa1b50c66793420303e1436145d333fe645ca190 Mon Sep 17 00:00:00 2001 From: Matteo Matassoni Date: Wed, 25 Nov 2015 09:11:35 +0100 Subject: [PATCH 3/4] turn inner findMinIndex function as method of Array via extension --- .../Contents.swift | 33 ++++++++++--------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/Assignments/Sorting/SelectionSort.playground/Pages/SequenceType Extension.xcplaygroundpage/Contents.swift b/Assignments/Sorting/SelectionSort.playground/Pages/SequenceType Extension.xcplaygroundpage/Contents.swift index 286e52a..e822a5f 100644 --- a/Assignments/Sorting/SelectionSort.playground/Pages/SequenceType Extension.xcplaygroundpage/Contents.swift +++ b/Assignments/Sorting/SelectionSort.playground/Pages/SequenceType Extension.xcplaygroundpage/Contents.swift @@ -7,28 +7,15 @@ Instead of only sorting arrays, modify your function to sort any type conforming extension SequenceType { func selectionSort(@noescape isOrderedBefore: (Generator.Element, Generator.Element) -> Bool) -> [Generator.Element] { - func findMinIndex(array: [Generator.Element], startingFrom start: Int = 0, @noescape isOrderedBefore: (Generator.Element, Generator.Element) -> Bool) -> Int? { - guard 0.. 1 else { return array } for i in 0.. Bool) -> Int? { + guard 0.. $1.0}).isSorted({$0.0 > $1.0})) From 1f13b336d74303f60ac767b716110a7d1a53f8b9 Mon Sep 17 00:00:00 2001 From: Matteo Matassoni Date: Mon, 30 Nov 2015 12:33:28 +0100 Subject: [PATCH 4/4] Assignment 2: Functional programming approach solution --- .../Contents.swift | 28 ++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/Assignments/Sorting/InsertionSort.playground/Pages/Insertion Sort.xcplaygroundpage/Contents.swift b/Assignments/Sorting/InsertionSort.playground/Pages/Insertion Sort.xcplaygroundpage/Contents.swift index 98fc641..6696730 100644 --- a/Assignments/Sorting/InsertionSort.playground/Pages/Insertion Sort.xcplaygroundpage/Contents.swift +++ b/Assignments/Sorting/InsertionSort.playground/Pages/Insertion Sort.xcplaygroundpage/Contents.swift @@ -6,10 +6,30 @@ Write an `insertionSort` function that takes an array and returns it as sorted b Note that there is already a `swap` function in the standard library. However, you can move the larger elements to the right rather than using the `swap` function. */ +extension Array { + func insertInOrder(newElement: Element, @noescape isOrderedBefore: (Element, Element) -> Bool) -> [Element] { + var array = self + guard !array.isEmpty else { + return [newElement] + } + + let firstElement = array.removeFirst() + if isOrderedBefore(newElement, firstElement) { + return [newElement, firstElement] + array + } else { + return [firstElement] + array.insertInOrder(newElement, isOrderedBefore: isOrderedBefore) + } + } +} func insertionSort(var array: [T], @noescape isOrderedBefore: (T, T) -> Bool) -> [T] { - - return array + guard !array.isEmpty else { + return [] + } + + let firstElement = array.removeFirst() // this removes the first element from the array mutating it + let subOrderedArray = insertionSort(array, isOrderedBefore: isOrderedBefore) // recursive call on the smaller array + return subOrderedArray.insertInOrder(firstElement, isOrderedBefore: isOrderedBefore) // insert the first element mainting the order in the subarray, which is ordered } @@ -22,8 +42,8 @@ assert(items == ["c", "d", "b"]) // double check that items does not change assert(insertionSort([1], isOrderedBefore: <).isSorted()) assert(insertionSort([1, 2, 3], isOrderedBefore: <).isSorted()) -//assert(insertionSort([1, 2, 3], isOrderedBefore: >).isSorted(>)) -//assert(insertionSort([3, 2, 1, 2, -1], isOrderedBefore: <).isSorted()) +assert(insertionSort([1, 2, 3], isOrderedBefore: >).isSorted(>)) +assert(insertionSort([3, 2, 1, 2, -1], isOrderedBefore: <).isSorted()) /*: [Table of Contents](Table%20of%20Contents) | [Previous](@previous) | [Next](@next)