Swift: Create an array of numbers 1 to 100 and divide into odds and evens using partition()
// 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
Thanks to @AirspeedSwift for help with the partition algorithm
Related protips:
Written by Anthony Levings
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ios
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#