vogzvq
Last Updated: September 03, 2018
·
13.66K
· 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

30574

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(17)
rands.insert(29)
rands.insert(21)
rands.insert(11) https://rightmove.onl/
rands.insert(31)
let str1 = " ".join(map(rands) {"($0)"}

3 months ago ·