This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import CoreData | |
let context = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType) | |
class Book: NSManagedObject { | |
var title: String? | |
var author: Author? | |
var date: Date? | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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] | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Node: Hashable { | |
var adjacentNodes = [Node]() | |
} | |
func breadthFirstSearch(from startingNode: Node{ | |
let queue = [startingNode] | |
var visited = Set<Node>() | |
while let next = queue.first { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
FilterableProductTable | |
SearchBar | |
ProductTable | |
ProductCategoryRow | |
ProductRow | |
*/ | |
const PRODUCTS = [ | |
{category: 'Sporting Goods', price: '$49.99', stocked: true, name: 'Football'}, |
OlderNewer