Skip to content

Instantly share code, notes, and snippets.

@davidinga
davidinga / ShortestTransformationSequences.swift
Created January 4, 2023 17:06
Shortest Transformation Sequences
func get_all_shortest_transformation_sequences(start_word: String, target_word: String, words: [String]) -> [[String]] {
var result: [[String]] = []
var found = false
var level = 0
let words = Set(words)
var slate: [String] = []
var parent: [String: Set<String>] = [:]
var visited: Set<String> = []
var levels: [String: Int] = [:]
@davidinga
davidinga / mergeAccounts.swift
Last active December 22, 2021 07:41
Merge accounts with the same email address.
class Solution {
func accountsMerge(_ accounts: [[String]]) -> [[String]] {
var accounts: [Account] = accounts.map {
var account = Account(name: $0.first!)
for i in 1 ..< $0.count {
account.emails.insert($0[i])
}
return account
}
@davidinga
davidinga / BST.swift
Last active December 17, 2021 20:53
Simple BST in Swift
class Program {
class BST {
var value: Int
var left: BST?
var right: BST?
init(value: Int) {
self.value = value
left = nil
right = nil
@davidinga
davidinga / MoveZerosInPlace.swift
Last active December 6, 2021 17:24
Move zeros to end of array in place
/*
Move zeros to end of array in place.
Brute force
Loop through elements n times
When 0 found
Bubble up 0 to end, perform swaps until swap performed with last element
O(n^2) time | O(1) space
Loop through elements n times
@davidinga
davidinga / gist:d04d270adc01f0a5d4bf67b73591e941
Created January 25, 2021 16:44
SpriteKit Typecasting Question
//
// GameScene.swift
// project11
//
// Created by David Inga on 10/23/20.
//
import SpriteKit
class GameScene: SKScene {
@davidinga
davidinga / DepthFirstSearch.swift
Created August 27, 2019 01:24
Depth First Search (DFS) implementation in Swift. Uses my GraphOP and Stack objects.
func depthFirstSearch(from start: Vertex<String>,
to end: Vertex<String>,
in graph: GraphOP<String> ) -> Stack<Vertex<String>> {
var stack = Stack<Vertex<String>>()
var visited = Set<Vertex<String>>()
stack.push(start)
visited.insert(start)
@davidinga
davidinga / HashTable.swift
Created August 26, 2019 21:54
Simple Hash Table implementation in Swift. Includes Linked List, and Node structures. Basic methods.
struct HashTable<Key: Hashable, Value> {
private typealias Element = (key: Key, value: Value)
private typealias List = LinkedList<Element>
private var table: [List]
init(size: Int) {
table = Array<List>()
for _ in 0..<size {
@davidinga
davidinga / GraphAL.swift
Created August 26, 2019 06:42
Graph data structure in Swift using an Adjacency List.
public struct GraphAL<Element: Hashable> {
var adjList = [Element: GraphNode<Element>]()
public mutating func addVertex(_ name: Element) {
adjList[name] = GraphNode(name)
}
@discardableResult public mutating func addEdge(from source: Element, to destination: Element) -> Bool {
guard adjList[source] != nil, adjList[destination] != nil
else { return false }
@davidinga
davidinga / GraphAM.swift
Last active March 6, 2022 04:21
Graph data structure in Swift using an Adjacency Matrix.
public struct GraphAM {
private var adjMatrix = [[Int?]]()
private var numberOfVertices: Int {
return adjMatrix.count
}
init(numberOfVertices: Int) {
adjMatrix = Array(repeating: Array(repeating: nil, count: numberOfVertices), count: numberOfVertices)
}
@davidinga
davidinga / Edge.swift
Created August 25, 2019 03:04
Graph data structure in Swift using Objects and Pointers with an Adjacency List.
public enum EdgeType {
case directed, undirected
}
public struct Edge<Element: Hashable> {
var source: Vertex<Element>
var destination: Vertex<Element>
var weight: Double?
}