To find the standard deviation of all values in a map in Swift, you can use the following code:
main.swift536 chars15 lines
The above code extends the Dictionary
class to add a new computed property standardDeviation
which returns the standard deviation of all values in the dictionary. It first creates an array of all the values in the dictionary, then calculates the mean of those values. It then calculates the deviation of each value from the mean, squares it and adds it to deviations
. Finally, it calculates the variance by dividing deviations
by the number of values and returns the square root of the variance as the standard deviation.
gistlibby LogSnag