Skip to content

Instantly share code, notes, and snippets.

// The contingency table contains the support counts for each item
// in comparision with one another.
struct ContingencyTable {
var bc: Int // B and C
var b_c: Int // B and not C
var _bc: Int // not B and C
var _b_c: Int // not B and not C
@rayfix
rayfix / eclat.swift
Created January 20, 2019 19:43
ECLAT Frequent Pattern Matching in Swift
var accumulatedItemSets = Dictionary(uniqueKeysWithValues: database.map {
(Set([$0.key]), Set($0.value)) })
var itemSetsToEvaluate = accumulatedItemSets
while !itemSetsToEvaluate.isEmpty {
var temp: [Set<String>: Set<Int>] = [:]
itemSetsToEvaluate.pairs().forEach { a, b in
let key = a.key.union(b.key)
let value = a.value.intersection(b.value)
if value.count >= absoluteSupport {
@rayfix
rayfix / GildedRose.swift
Created April 21, 2018 22:32
Gilded Rose
import Foundation
typealias Quality = Int
typealias SellIn = Int
typealias UpdateRule = (Item) -> Item
struct Item {
enum Kind: String {
case normal
extension StoryboardInitializable where Self: UIViewController {
static var storyboardName: String {
return "Main"
}
static var storyboardBundle: Bundle? {
return nil
}
static var storyboardIdentifier: String {
return String(describing: self)
struct BeginsWith {
let value: String
init(_ value: String) {
self.value = value
}
}
func ~=(pattern: BeginsWith, value: String) -> Bool {
return value.hasPrefix(pattern.value)
}
@rayfix
rayfix / zombie.swift
Created April 6, 2017 17:30
Create a zombie
class Person {}
unowned var zombie: Person
do {
let alive = Person()
zombie = alive
}
// a zombie is born
@rayfix
rayfix / LinkedList.swift
Created April 2, 2017 04:32
An alternate (slightly memory hungry) solution to the middle problem
public enum LinkedList<T> {
case end
indirect case node(value: T, next: LinkedList)
public func cons(_ value: T) -> LinkedList {
return .node(value: value, next: self)
}
}
let str = "Hello, Keita"
var index = str.characters.startIndex
while index < str.characters.endIndex {
print(str.characters[index])
str.characters.formIndex(after: &index)
}
let str = "acxz"
func isFunny(_ s: AnySequence<UInt8>) -> Bool {
let n1: [Int] = zip(s.dropFirst(), s).map { Int($0)-Int($1) }
let n2: [Int] = zip(s.reversed().dropFirst(), s.reversed()).map { Int($1)-Int($0) }
return n1 == n2
}
isFunny(AnySequence(str.utf8))
import Foundation
private enum RandomSource {
static private let file = fopen("/dev/urandom","r")!
static private let queue = DispatchQueue(label: "random")
static func get(count: Int) -> [Int8] {
let capacity = count + 1 // fgets adds null termination
var data = UnsafeMutablePointer<Int8>.allocate(capacity: capacity)
defer {
data.deallocate(capacity: capacity)