Skip to content

Instantly share code, notes, and snippets.

View jakehawken's full-sized avatar
🆒

Jake Hawken jakehawken

🆒
View GitHub Profile
@jakehawken
jakehawken / JakesSimpleSourdough.md
Last active January 4, 2021 20:58
My sourdough recipe

Jake's Simple Sourdough

The ingredients:

  • 500g water
  • 180g mature starter
    • I often put as much as 200g in, so anything in between should be fine.
  • 730g flour
    • When you're first making sourdough, just have all of your flour be bread flour. Play with mix ratios later.
  • 20g kosher salt
  • I recommend Diamond Crystal. It's finer and dissolves faster than Morton. And it's cheaper!
@jakehawken
jakehawken / ScreenStateHandler.swift
Last active June 30, 2023 16:08
ScreenStateHandler -- The glue to connect your views and presenters.
import Foundation
/*
I love the presenter pattern, but I hate connecting the presenter to the view using
delegation. Since protocol conformance can't be private in Swift, delegation always
leaks encapsulation because the conforming type has now announced to the world that
it has methods and properties that are irrelevant to everyone but the type doing the
delegating. ScreenStateHandler is a skinny little intermediary object that handles a
delegation-style 1:1 relationship between a presenter and a view but can be composed
into (rather than bolted onto) the types in question.
@jakehawken
jakehawken / AddBinary.swift
Last active October 1, 2019 00:34
Leetcode #67: “ Given two binary strings, return their sum (also a binary string). The input strings are both non-empty and contains only characters 1 or 0. For example, ‘11’ + ‘1’ = ‘100’ and ‘1010’ + ‘1011’ = ‘10101’.”
class Solution {
func addBinary(_ a: String, _ b: String) -> String {
let zeroChar: Character = "0"
let oneChar: Character = "1"
let aReversed = String(a.reversed())
let bReversed = String(b.reversed())
let bigger: String
let smaller: String
if a.count > b.count {
@jakehawken
jakehawken / PlusOne.swift
Created September 26, 2019 00:28
Leetcode #66: "Given a non-empty array of digits representing a non-negative integer, plus one to the integer. The digits are stored such that the most significant digit is at the head of the list, and each element in the array contain a single digit. You may assume the integer does not contain any leading zero, except the number 0 itself."
class Solution {
func plusOne(_ digits: [Int]) -> [Int] {
guard !digits.isEmpty else {
fatalError("But you promised!")
}
var digits = digits
let lastIndex = digits.count - 1
for i in 0...lastIndex {
let index = lastIndex - i
var digit = digits[index]
@jakehawken
jakehawken / MaximumSubarray.swift
Last active September 25, 2019 21:03
Leetcode #53: "Given an integer array, nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum."
class Solution {
func maxSubArray(_ nums: [Int]) -> Int {
guard let first = nums.first else {
return 0
}
guard nums.count > 1 else {
return first
}
var maxSingle = Int.min
@jakehawken
jakehawken / CountAndSay.swift
Last active September 23, 2019 17:31
Leetcode #38: "The count-and-say sequence is the sequence of integers with the first five terms as following: 1) '1' 2) '11' 3) '21' 4. '1211' 5. '111221' 1 is read off as 'one 1' or 11. 11 is read off as 'two 1s' or 21. 21 is read off as 'one 2, then one 1' or 1211. Given an integer n where 1 ≤ n ≤ 30, generate the nth term of the ... sequence."
class Solution {
func countAndSay(_ n: Int) -> String {
guard n > 0 else {
fatalError("Positive integers only!")
}
var last = "1"
if n == 1 {
return last
}
for _ in 2...n {
@jakehawken
jakehawken / NeedleInHaystack.swift
Created September 16, 2019 05:10
Leetcode #28: "Return the index of the first occurrence of `needle` in `haystack`, or -1 if `needle` is not part of `haystack`."
extension String {
func substring(start: Int, length: Int) -> String {
let startingIndex = self.index(self.startIndex, offsetBy: start)
let endingIndex = self.index(self.startIndex, offsetBy: start + length)
return String(self[startingIndex..<endingIndex])
}
}
func strStr(_ haystack: String, _ needle: String) -> Int {
if needle.count == 0 {
@jakehawken
jakehawken / RemoveElement.swift
Created September 16, 2019 01:14
Leetcode #27: "Given an array `nums` and a value `val`, remove all instances of that value in-place and return the new length."
func removeElement(_ nums: inout [Int], _ val: Int) -> Int {
var maxIndex = nums.count - 1
var currentIndex = 0
while currentIndex <= maxIndex {
if nums[currentIndex] == val {
nums.remove(at: currentIndex)
maxIndex -= 1
}
else {
currentIndex += 1
@jakehawken
jakehawken / RemoveDuplicates.swift
Created September 15, 2019 23:58
Leetcode #26: "Given a sorted array `nums`, remove the duplicates in-place such that each element appears only once, and return the new length."
func removeDuplicates(_ nums: inout [Int]) -> Int {
var maxIndex = nums.count - 1
var currentIndex = 0
while currentIndex < maxIndex {
if nums[currentIndex] == nums[currentIndex + 1] {
nums.remove(at: currentIndex)
maxIndex -= 1
}
else {
currentIndex += 1
@jakehawken
jakehawken / MergeTwoLists.swift
Last active September 15, 2019 23:59
Leetcode #21: Algorithm for merging two sorted linked lists into one sorted linked list.
public class ListNode: CustomDebugStringConvertible {
public var val: Int
public var next: ListNode?
public init(_ val: Int) {
self.val = val
self.next = nil
}
public var debugDescription: String {
let nextString = next?.debugDescription ?? "<end>"