This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
enum HTTPClientError { | |
case badRequest | |
case unauthorized | |
case paymentRequired | |
case forbidden | |
case notFound | |
case methodNotAllowed | |
case notAcceptable |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
protocol Repository { | |
associatedtype Item | |
func getItems() -> [Item] | |
func deleteItems() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Calculating median in Swift: | |
import Foundation | |
let sample = [0, 1, 5, 7, 10, 14] | |
func median<T: BinaryFloatingPoint & Comparable>(sample: Array<T>) -> T? { | |
let orderedSample = sample.sorted() | |
let count = orderedSample.count | |
let length_is_even = orderedSample.count % 2 == 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Calculating modes in Swift | |
func modes<T: BinaryFloatingPoint & Hashable>(sample: Array<T>) -> Array<T> { | |
let mapped = sample.map { ($0, 1) } | |
let counts = Dictionary(mapped, uniquingKeysWith: +) | |
let max_count = counts.values.max() ?? 0 | |
let modes = counts.filter { $0.value == max_count }.map { $0.key } | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Calculating variance in Swift | |
let data = [0, 1, 5, 7, 9, 10, 14] | |
func variance<T: BinaryFloatingPoint>(input: Array<T>) -> T { | |
let sum = input.reduce(0, +) | |
let mean = sum / T(input.count) | |
let sumOfSquaredDifferences = input.reduce(0) { $0 + (mean - $1) * (mean - $1) } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The normal probability density function (PDF) is used in statistics to calculate the probability of a specific value occurring in a normal distribution. | |
func normalProbabilityDensityFunction(x: Double, mean: Double, stdDev: Double) -> Double { | |
let power = pow((x - mean), 2) | |
let numerator = exp(-1.0 * power / (2.0 * stdDev * stdDev)) | |
let denominator = 2.0 * Double.pi * stdDev * stdDev | |
return 1.0 / sqrt(denominator) * numerator | |
} | |
print(normalProbabilityDensityFunction(x: 60, mean: 64.43, stdDev: 2.99)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Calculating cdf using erf (error function) provided by Darwin.C.math | |
# TODO: write an explanation in comments | |
func normalCumulativeDensityFunction(x: Double, mean: Double, stdDev: Double) -> Double { | |
return 0.5 * (1.0 + erf((x - mean) / (stdDev * sqrt(2.0)))) | |
} | |
print(normalCumulativeDensityFunction(x: 64.43, mean: 64.43, stdDev: 2.99)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let s2pi = sqrt(2.0 * .pi) | |
let P0 = [ -1.26551223, 1.00002368, 0.37409196, 0.09678418, -0.18628806, 0.27886807, -1.13520398, 1.48851587, -0.82215223, 0.17087277 ] | |
let Q0 = [ -0.15172908, 1.84759012, -3.68455089, 3.31335053, -1.01539585, 0.13295073, -0.02065141, -0.00900744, 0.00711589, 0.00392377 ] | |
let P1 = [ -0.73700162, 1.17160096, 0.63439328, 0.29963877, 0.21951691, 0.23392674, 0.31539329, 0.72463255 ] | |
let Q1 = [ -0.12712856, 1.99197325, -5.63224284, 7.49868308, -5.18611478, 0.55327544, 1.10514223, 0.33659149 ] | |
let P2 = [ -0.80082143, 1.81891395, 1.84631832, 1.19352598, 0.91109542, 0.06219856, -0.76820489, 0.56827526 ] | |
let Q2 = [ -0.10846517, 2.11237046, -6.72875739, 8.09336846, -5.09373263, 1.15688468, 3.89290431, 1.82524606 ] | |
func polevl(_ x: Double, _ coef: [Double]) -> Double { | |
var ans = coef[0] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func plusMinus(input: [Int]) { | |
let weight = 1 / Double(input.count) | |
let positives = input.filter( { $0 > 0 } ) | |
let negatives = input.filter( { $0 < 0 } ) | |
let zeroes = input.filter( { $0 == 0 } ) | |
let positiveRat = Double(positives.count) * weight | |
let negativeRat = Double(negatives.count) * weight | |
let zeroRat = Double(zeroes.count) * weight |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func minMaxSum(input: [Int]) { | |
let sum = input.reduce(0, +) | |
let min = input.min() ?? 0 | |
let max = input.max() ?? 0 | |
let minSum = sum - max | |
let maxSum = sum - min | |
print(minSum, maxSum) |
OlderNewer