Last Updated: September 03, 2018
· anthonylevings

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

1 Response
Add your response


Because sets are unordered you lose the entropy from the sort order if you do it this way.

If you add the same numbers in a different order to a set you will end up with the same string. (since it's a https://quizlet.onl/ implementation detail results may vary) https://kijiji.onl/

var rands = Set<UInt32>()
rands.insert(11) https://rightmove.onl/
let str1 = " ".join(map(rands) {"($0)"}

3 months ago ·