Skip to content

Instantly share code, notes, and snippets.

View acrookston's full-sized avatar
👨‍💻
Coding!

Andrew Crookston acrookston

👨‍💻
Coding!
View GitHub Profile
@acrookston
acrookston / sorting_tree.rb
Last active April 29, 2017 19:29
Tree sorting in ruby
# Question: https://www.dropbox.com/s/4dlhs6x69cmj8uv/Screenshot%202017-04-29%2012.21.05.png?dl=0
# Time limit: 30min
# Finished in 28min
# It should be possible to load the tree in one pass. I just didn't have time to rewrite that part.
require "rubygems"
require "json"
locations = JSON.parse(STDIN.read)
class Node
@acrookston
acrookston / UIColorExtensions.swift
Created April 26, 2017 22:26
UIColor extension for creating UIColor with rgba() values
import UIKit
extension UIColor {
static func rgba(_ r: CGFloat, _ g: CGFloat, _ b: CGFloat, _ a: CGFloat) -> UIColor {
return UIColor(red: r / 255.0, green: g / 255.0, blue: b / 255.0, alpha: a)
}
static var lightBackground: UIColor { return .rgba(246, 246, 246, 1) }
static var text: UIColor { return .rgba(51, 51, 51, 1) }
}
@acrookston
acrookston / EnumExtensions.swift
Created April 26, 2017 22:24
Swift extension to make Enums enumerable
protocol EnumCollection : Hashable {}
extension EnumCollection {
static func enumerated() -> AnySequence<Self> {
typealias S = Self
return AnySequence { () -> AnyIterator<S> in
var raw = 0
return AnyIterator {
let current = withUnsafePointer(to: &raw) {
$0.withMemoryRebound(to: S.self, capacity: 1) { $0.pointee }
import Foundation
// Given some sorted dates find out if the set contains 5 dates within 30 days of eachother.
// I.e. Given a user's login dates, find out if they were logged in 5 or more times in 30 days.
extension Date {
static func parse(_ str: String) -> Date {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
return dateFormatter.date(from: str)!
class LinkedListNode<T> {
var next : LinkedListNode<T>?
var value: T
var count : Int {
return next == nil ? 1 : next!.count + 1
}
init(_ value: T, _ next: LinkedListNode<T>?=nil) {
class NavBarItem {
typealias Callback = (() -> ())
var title : String
var selected: Bool
var callback: Callback
init(title: String, selected: Bool, callback: @escaping Callback) {
protocol NavButtonProtocol: RawRepresentable {
var title : String { get }
}
//extension NavButtonProtocol where Self : RawRepresentable, Self.RawValue == String {
// static var count: Int {
// var max: Int = 0
// while let _ = self.init(rawValue: max) { max += 1 }
extension UICollectionView {
func indexPathsForCloseItems(offset: Int) -> [IndexPath] {
var indexes = indexPathsForVisibleItems.sorted(by: { $0.row < $1.row })
for x in indexes {
print("had: \(x)")
}
if let index = indexes.last {
@acrookston
acrookston / README.md
Last active January 26, 2022 11:05
Xcode pre-action to build custom Info.plist

Automatic build versions from git in Xcode (and other goodies)

Installation procedure for pre-build actions to automatically populate Xcode Info.plist with dynamic data.

1. Xcode Scheme pre-action

Edit Xcode Scheme and add a pre-action script. Copy the contents of preaction.sh into the pre-action script box.

module Enumerable
def normalize
magnitude = Math.sqrt(reduce(:+))
return self if magnitude.nan?
map { |x| x.to_f / magnitude }
end
def similarity(arr)
return 0.0 if arr.count != count
similarity = Math.sqrt(zip(arr).map { |d| d.first * d.second }.reduce(:+))