Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Swift - iOS Xcode 9.4 Write a function that: - takes in an array of integers and

ID: 3904151 • Letter: S

Question

Swift - iOS Xcode 9.4

Write a function that:

- takes in an array of integers and separates each element of the array into odd and even numbers

- then splits the array into lists that are sorted in DESCENDING order (largest first, smallest last)

- finally, your function should return the sorted evens and odds as a tuple

- Each element in the array COULD be nil. So you will have to figure out how to represent that in the parameter's type.

- If an element in the array is nil, do nothing with it.

- Don't forget edge cases like an empty data or all nil values.

Notes:

- Research how to return multiple values (as a named tuple) from Swift functions

- DO NOT manually sort the integers; find a better way using Swift's libraries!

- Your function MUST work with the commented out function calls below!

//let sortedNumbers = sortedEvenOdd(numbers: [0, 9, 3, nil, 8, 15, 11, 2, 20, nil])

//print(sortedNumbers.evens) // [20, 8, 2, 0]

//print(sortedNumbers.odds) // [15, 11, 9, 3]

Explanation / Answer

// create an empty Int array
var arr = [Int]()
// extend the array using a Range  
arr += 1...100
// partition based on the use of odd or even numbers
let idx = partition(&arr, indices(arr)) { i, _ in i%2==0 }
// use the returned point of partition (index) to split the array into odd and even
arr[0..<idx] // even
arr[idx..<arr.endIndex] // odd
print(numbers.sort(>)) //Sorting in Descending order
let sortedNumbers = sortedEvenOdd(numbers: [0, 9, 3, nil, 8, 15, 11, 2, 20, nil])

print(sortedNumbers.evens) // [20, 8, 2, 0]

print(sortedNumbers.odds) // [15, 11, 9, 3]