https://codepen.io/gaearon/pen/gWWZgR?editors=0010
- Display the location for each move in the format (col, row) in the move history list.
function getColRow(i) {
function col(i) {
const one = new Set([0, 3, 6])
https://codepen.io/gaearon/pen/gWWZgR?editors=0010
function getColRow(i) {
function col(i) {
const one = new Set([0, 3, 6])
/* | |
FilterableProductTable | |
SearchBar | |
ProductTable | |
ProductCategoryRow | |
ProductRow | |
*/ | |
const PRODUCTS = [ | |
{category: 'Sporting Goods', price: '$49.99', stocked: true, name: 'Football'}, |
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) |
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)) |
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 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 |
class Node: Hashable { | |
var adjacentNodes = [Node]() | |
} | |
func breadthFirstSearch(from startingNode: Node{ | |
let queue = [startingNode] | |
var visited = Set<Node>() | |
while let next = queue.first { |
//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] | |
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 | |
} |
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) | |
} |