Skip to content

Instantly share code, notes, and snippets.

View BrianLitwin's full-sized avatar

Brian Litwin BrianLitwin

View GitHub Profile
@BrianLitwin
BrianLitwin / FetchRequestGenerator.swift
Created January 26, 2018 04:46
A Generic fetch request protocol that uses an Associated Type to send fetch requests through an Enum
import CoreData
let context = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
class Book: NSManagedObject {
var title: String?
var author: Author?
var date: Date?
}
func mergeSort(_ array: inout [Int], _ temp: inout [Int?], _ leftStart: Int, _ rightEnd: Int) {
guard leftStart < rightEnd else { return }
let middle = (leftStart + rightEnd) / 2
mergeSort(&array, &temp, leftStart, middle)
mergeSort(&array, &temp, middle + 1, rightEnd)
mergeHalves(&array, &temp, leftStart, rightEnd)
}
func isUnique(string: String) -> Bool {
var charSet = Set<Character>()
for character in string {
guard charSet.contains(character) == false else { return false }
charSet.insert(character)
}
return true
}
//First-in first-out
//concession-stand line
//i.e. enqueue(4), enqueue(43), enqueue(3), enqueue(2)
//-> dequeue() returns 4
//append items and take items from [0] or .first to get next item
//or: add items to beginning at index 0 and rettrieve items from the end
var numbers = [1,2,3]
class Node: Hashable {
var adjacentNodes = [Node]()
}
func breadthFirstSearch(from startingNode: Node{
let queue = [startingNode]
var visited = Set<Node>()
while let next = queue.first {
func nodesAtEachDepth(from startNode: BinaryTreeNode) -> [[BinaryTreeNode]] {
var nodes = [[BinaryTreeNode]]()
return findDepths(depth: 0, node: startNode, nodes: &nodes)
}
func findDepths(depth: Int, node: BinaryTreeNode?, nodes: inout [[BinaryTreeNode]]) -> [[BinaryTreeNode]] {
guard let node = node else { return nodes }
var startingNewDepth = nodes.count <= depth
//1
func oneAway(s1: String, s2: String) -> Bool {
guard abs(s1.count - s2.count) <= 1 else { return false }
var foundDifference = false
var index1 = 0
var index2 = 0
while index1 < s1.count && index2 < s2.count {
let c1 = s1[s1.index(s1.startIndex, offsetBy: index1)]
let c2 = s2[s2.index(s1.startIndex, offsetBy: index2)]
if c1 != c2 {
func sieve(numbers: [Int]) -> [Int] {
if numbers.isEmpty { return [] }
let p = numbers[0]
// assert(p > 1, "numbers must start at 2 or higher")
let rest = numbers[1..<numbers.count]
return [p] + sieve(rest.filter { $0 % p > 0 })
}
func primesUpTo(max: Int) -> [Int] {
return [1] + sieve(Array(2...max))
@BrianLitwin
BrianLitwin / Array -> Binary Search Tree.swift
Last active October 31, 2018 20:52
Array -> Binary Search Tree
class Solution {
func sortedArrayToBST(_ nums: [Int]) -> TreeNode? {
return makeBST(nums, 0, nums.count - 1)
}
func makeBST(_ nums: [Int], _ start: Int, _ end: Int) -> TreeNode? {
guard start <= end else { return nil }
let mid = (start + end) / 2
let node = TreeNode(nums[mid])
node.left = makeBST(nums, start, mid - 1)
/*
FilterableProductTable
SearchBar
ProductTable
ProductCategoryRow
ProductRow
*/
const PRODUCTS = [
{category: 'Sporting Goods', price: '$49.99', stocked: true, name: 'Football'},