Random Sampling[Source | Tests] Show Operations for randomly selecting k elements without replacement from a sequence or collection. Use these methods for sampling multiple elements from a collection, optionally maintaining the relative order of the elements. Each method has an overload that takes a RandomNumberGenerator as a parameter. var source = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
source.randomSample(count: 4)
// e.g. [30, 10, 70, 50]
source.randomStableSample(count: 4)
// e.g. [20, 30, 80, 100]
var rng = SplitMix64(seed: 0)
source.randomSample(count: 4, using: &rng)
What is the output of the following list operationaList = [10, 20, 30, 40, 50, 60, 70, 80]print(aList[2:5])print(aList[:4])print(aList[3:])The correct result doesn't make sense. Please explain why its correct.Here is the description of the challenge. Challenge Task 4 of 4 Great job so far! Clearly you know how to add and read items from the array so in this challenge let's try getting rid of a value. For this task, remove the 6th item from the array and assign the result to a constant named discardedValue. To remove an item, use the method removeAtIndex() and put the index number in between parentheses that you want to remove. MY ANSWER: var arrayOfInts = [10, 20, 30, 40, 50, 60, 70, 80]
arrayOfInts.removeAtIndex(5)
let discardedValue = arrayOfInts.removeAtIndex(5)
// discardedValue is 70
I'm removing 6th item from the array, which is 60 then when I'm assigning result to a constant named discardedValue and this is where things are getting tricky. I checked what "discardedValue" returns and its not 60 it's the following number in the array 70. Why is it the right answer? I'm suppose to assign result to a constant, which is suppose to be the same result of arrayOfInts.removeAtIndex(5) 60. I'm confused. |