Skip to content

Instantly share code, notes, and snippets.

@junebash
Last active November 8, 2019 17:48
Show Gist options
  • Save junebash/f7ff51c9c5e051a41145d77753c6484b to your computer and use it in GitHub Desktop.
Save junebash/f7ff51c9c5e051a41145d77753c6484b to your computer and use it in GitHub Desktop.
Lambda School Unit 2 Sprint 1 Coding Challenge
import Foundation
/*
Given two arrays arr1 and arr2, the elements of arr2 are distinct, and all elements in arr2 are also in arr1.
Sort the elements of arr1 such that the relative ordering of items in arr1 are the same as in arr2. Elements that don't appear in arr2 should be placed at the end of arr1 in ascending order.
Example 1:
Input: arr1 = [2,3,1,3,2,4,6,7,9,2,19], arr2 = [2,1,4,3,9,6]
Output: [2,2,2,1,4,3,3,9,6,7,19]
Constraints:
arr1.length, arr2.length <= 1000
0 <= arr1[i], arr2[i] <= 1000
Each arr2[i] is distinct.
Each arr2[i] is in arr1.
*/
let arr1 = [2,3,1,3,2,4,6,7,9,2,19]
let arr2 = [2,1,4,3,9,6]
// Output: [2,2,2,1,4,3,3,9,6,7,19]
func referenceSort(input inputArray: [Int], from referenceArray: [Int]) -> [Int] {
let errorMessage = "Arrays do not follow laid-out rules; cannot sort. Returning empty array."
if inputArray.count > 1000 || referenceArray.count > 1000 {
print(errorMessage)
return []
}
var arrayToSort = inputArray
var arrayArray = [[Int]]()
for reference in referenceArray {
if reference < 0 || reference > 1000 {
print(errorMessage)
return []
}
var foundMatch = false
var referencedNumbers = [Int]()
var arrayCount = arrayToSort.count
var index = 0
while index < arrayCount {
if arrayToSort[index] < 0 || arrayToSort[index] > 1000 {
print(errorMessage)
return []
}
if arrayToSort[index] == reference {
referencedNumbers.append(arrayToSort.remove(at: index))
arrayCount -= 1
foundMatch = true
} else {
index += 1
}
}
if !foundMatch {
print(errorMessage)
return []
}
arrayArray.append(referencedNumbers)
}
arrayToSort.sort()
var arrayToReturn = [Int]()
for subArray in arrayArray {
arrayToReturn.append(contentsOf: subArray)
}
arrayToReturn.append(contentsOf: arrayToSort)
return arrayToReturn
}
let arr3 = referenceSort(input: arr1, from: arr2)
print(arr3)
let anotherArr = [1,7,9,20,38,3,1,6,7,9,20,57,1,2,3,4,39,20,1,3,4,2,2,2,20,10,10,9,7,587]
let refArr = [9,587,20,2,3,1,7,6]
let anotherSorted = referenceSort(input: anotherArr, from: refArr)
print(anotherSorted)
// Was previously wrong; now fixed and I think working!
// Not super efficient but I think it works to spec.
//-----------------------------------------------------------------------------------------
// alternate implementation (post-timeframe)
//-----------------------------------------------------------------------------------------
func referenceSort2(input inputArray: [Int], from referenceArray: [Int]) -> [Int] {
let errorMessage = "Arrays do not follow laid-out rules; cannot sort. Returning empty array."
if inputArray.count > 1000 || referenceArray.count > 1000 {
print(errorMessage)
return []
}
var arrayToSort = inputArray
var sortedArray = [Int]()
for reference in referenceArray {
if reference < 0 || reference > 1000 || !arrayToSort.contains(reference) {
print(errorMessage)
return []
}
while arrayToSort.contains(reference) {
guard let index = arrayToSort.firstIndex(of: reference) else {
print("UNEXPECTED ERROR")
return []
}
sortedArray.append(arrayToSort.remove(at: index))
}
}
arrayToSort.sort()
sortedArray.append(contentsOf: arrayToSort)
return sortedArray
}
@vicxruiz
Copy link

Awesome job, extremely efficient! Runtime was 16ms compared to rubric solution 12ms!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment