Skip to content

Instantly share code, notes, and snippets.

import Foundation
enum HTTPClientError {
case badRequest
case unauthorized
case paymentRequired
case forbidden
case notFound
case methodNotAllowed
case notAcceptable
@denpazakura
denpazakura / Repository.swift
Created November 24, 2023 13:44
An implementation of the Repository pattern in Swift that utilises associated types
import Foundation
protocol Repository {
associatedtype Item
func getItems() -> [Item]
func deleteItems()
}
@denpazakura
denpazakura / Median.swift
Last active December 5, 2023 10:34
Calculating median in Swift
# 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
@denpazakura
denpazakura / Modes.swift
Last active December 5, 2023 10:26
Calculating modes in Swift
# 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 }
@denpazakura
denpazakura / Variance.swift
Last active December 7, 2023 10:48
Calculating variance in Swift
# 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) }
@denpazakura
denpazakura / NormalPDF.swift
Created December 6, 2023 11:58
Calculates probability density, assuming normal distribution
# 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))
@denpazakura
denpazakura / NormalCDF.swift
Created December 7, 2023 10:47
Cumulative density function
# 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))
@denpazakura
denpazakura / normalPercentPointFunction
Created December 10, 2023 16:19
Calculating the quantile (inverted cdf)
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]
@denpazakura
denpazakura / PlusMinus.swift
Created December 13, 2023 15:30
PlusMinus implementation
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
@denpazakura
denpazakura / MinMaxSum.swift
Created December 14, 2023 16:11
Calculate the minimum and the maximum sum in an array (any number of elements)
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)