Skip to content

Instantly share code, notes, and snippets.

View jakehawken's full-sized avatar
🤖
Obsessed with learning Godot right now

Jake Hawken jakehawken

🤖
Obsessed with learning Godot right now
View GitHub Profile
@jakehawken
jakehawken / ValidParentheses.swift
Last active September 16, 2019 00:00
Leetcode #20: Takes in a string of braces (any mix of parentheses, square brackets, and curly braces) and returns whether they are validly delimited.
import Foundation
enum ParenthesisType: CaseIterable {
case round
case square
case curly
var open: Character {
switch self {
case .round:
@jakehawken
jakehawken / LongestCommonPrefix.swift
Last active September 16, 2019 00:01
Leetcode #14: Finds the longest common prefix between the strings in an array.
func longestCommonPrefix(_ strs: [String]) -> String {
var shortest = Int.max
let charArrays = strs.map { (string) -> [Character] in
let array = Array(string)
shortest = min(shortest, array.count)
return array
}
var outputChars = [Character]()
for i in 0..<shortest {
let ithChars = charArrays.map { $0[i] }
@jakehawken
jakehawken / RomanToInt.swift
Last active September 16, 2019 00:00
Leetcode #13: Converts a number in roman numerals to an Integer. Assumes a valid roman numeral.
let romanMapping: [String: Int] = [
"I" : 1,
"V" : 5,
"X" : 10,
"L" : 50,
"C" : 100,
"D" : 500,
"M" : 1000
]
@jakehawken
jakehawken / PickerHandler.swift
Created August 28, 2019 22:24
Simple, genericized Swift object for backing UIPickerViews. All you need is an array and a callback block and you're golden.
// PickerHandler.swift
// Created by Jake Hawken on 8/27/19.
import Foundation
import UIKit
class PickerHandler<T: CustomStringConvertible> {
typealias ItemSelectionCallback = (T)->()
private let implementation: PickerHandlerImplementation<T>
@jakehawken
jakehawken / ProbabilityTree.swift
Last active August 5, 2019 20:21
I've never actually researched how an actual Markov Chain works, but I'm attempting, by way of a lot of assumptions, to write something at least similar to one in Swift. It’s a fun mental exercise. My approach is to make something that takes in an array of strings, builds a predictive tree based off of that input, and then can generate sentences…
// ProbabilityTree.swift
// Created by Jake Hawken on 8/4/19.
// Copyright © 2019 Jake Hawken. All rights reserved.
import Foundation
// TODO: genericize so that this can be used for things other than strings
class ProbabilityTree {
private let rootNode = Node(word: "", parent: nil)
@jakehawken
jakehawken / PropertyBox.cs
Last active June 25, 2019 16:23
Getting that sweet, sweet `didSet` functionality in C#.
using System;
using System.Collections;
using System.Collections.Generic;
public class PropertyBox<T>
{
private T value;
public Action<T> didSet;
public Action<T,T> willSet;
import SwiftyJSON
extension JSON {
func parseTo<T: Codable>() -> T? {
guard let data = try? rawData(options: .prettyPrinted) else {
return nil
}
let decoder = JSONDecoder()
return try? decoder.decode(T.self, from: data)
}
@jakehawken
jakehawken / PrettyPrintable.swift
Last active June 23, 2023 15:53
Some handy tools for making things into more human-readable strings. Helpful for debugging!
import Foundation
protocol PrettyPrintable {
func prettyPrinted() -> String
func prettyPrintToConsole()
}
extension PrettyPrintable {
func prettyPrintToConsole() {
let pretty = prettyPrinted()
@jakehawken
jakehawken / SuperviewsAndSubviews.swift
Last active June 26, 2023 15:14
Some handy methods for view debugging in iOS.
extension UIView {
func firstSuperview<T: UIView>(ofType type: T.Type) -> T? {
guard let superview = superview else {
return nil
}
if let superViewAsT = superview as? T {
return superViewAsT
}
return superview.firstSuperview(ofType: type)
static func -=(lhs: inout String, rhs: String) {
guard lhs.hasSuffix(rhs) else {
return
}
guard let range = lhs.range(of: rhs, options: [.backwards], range: nil, locale: nil) else {
return
}
lhs = lhs.replacingCharacters(in: range, with: "")
}