Skip to content

Instantly share code, notes, and snippets.

Alex Persian alexpersian

Block or report user

Report or block alexpersian

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
alexpersian / BoundingBox.swift
Last active Jan 15, 2020
ViewController class that will render a shape and then on-tap will draw a bounding box around that shape.
View BoundingBox.swift
// ViewController with a single shape drawn inside it.
// Tapping on the shape will cause a red minimum-bounding box to be drawn
// around the shape's bounds.
// Included as a single monofile for the sake of including it all within a single gist.
// Core logic centers around the Flood Fill algorithm with an interative, BFS approach used.
import UIKit
alexpersian / GenerateParentheses.swift
Created Jan 12, 2020
Much simpler solution for generating a string of balanced parentheses of length (n * 2).
View GenerateParentheses.swift
import Foundation
final class Generator {
private var store = [[""]]
func genBalanced(_ n: Int) -> [String] {
if n < store.count {
return store[n]
alexpersian / BalancedParanthesesGenerator.swift
Last active Jan 11, 2020
Generate balanced parentheses using Heap's Algorithm
View BalancedParanthesesGenerator.swift
import Foundation
final class Solution {
// Using a Set for result storage prevents unintended duplicates from being added.
private var result: Set<[Character]> = Set()
func generateParans(_ num: Int) {
// Build the seed array for sending into Heap's Algorithm gen function.
// We create an array of `num` opening parans and combine it with an
// array of `num` closing parans.
View TruncatedBounds.swift
// It's possible that the text is truncated not because of the line break mode,
// but because the text is outside the drawable bounds
if isTruncating == false {
// This gives us the range of all the characters that have been already laid out
// into the text container by the layout manager.
let glyphRange = layoutManager.glyphRange(for: textContainer)
// This provides the range of glyphs (not characters) that have been generated for the text container.
// Since a glyph is a graphical representation of a character, and more than one glyph can be used
// to represent a single character, this should always be equal or greater than the number of
View Truncated.swift
var isTruncating = false
// Make a range that encompases the entire text length.
let maxRange = NSRange(location: 0, length: Int.max)
// Use the layout manager to go through all of its line fragments within the range (entire text because of max range).
// More in-depth information is available within the NSLayoutManager docs.
layoutManager.enumerateLineFragments(forGlyphRange: maxRange) { _, _, _, glyphRange, stop in
// Ask the layout manager to find the range of the truncation glyph (...) within this line fragment
let truncatedRange = self.layoutManager.truncatedGlyphRange(inLineFragmentForGlyphAt: glyphRange.lowerBound)
// If the truncatedRange has a valid location that means the layout manager has detected the truncation glyph.
if truncatedRange.location != NSNotFound {
alexpersian / UITextView+Truncated.swift
Last active Dec 10, 2019
Extension on UITextView that allows for the detection of truncation.
View UITextView+Truncated.swift
extension UITextView {
// Massive credit to Dave Delong for his extensive help with this solution.
/// Returns whether or not the `UITextView` is displaying truncated text. This includes text
/// that is visually truncated with an ellipsis (...), and text that is simply cut off through
/// word wrapping.
/// - Important:
/// This only works properly when the `NSLineBreakMode` is set to `.byTruncatingTail` or `.byWordWrapping`.
/// - Remark:
View DepthFirstSearch.swift
import Foundation
final class DepthFirstSearch<T: Hashable> {
private let graph: Graph<T>
private var searchStack: [T] = []
private var visited: Set<T> = Set()
init(graph: Graph<T>) {
self.graph = graph
View DVDScene3.swift
// Update is called once per frame
public override func update(_ currentTime: TimeInterval) {
// Collect a reference frame for the node's current position
let currentFrame = dvdNode.calculateAccumulatedFrame()
// Top bound
if currentFrame.maxY >= self.frame.maxY {
moveTransform.ty = -1.0
View DVDScene2.swift
// ...
private var changeColor: SKAction {
return SKAction.colorize(with: UIColor.generateRandomColor(), colorBlendFactor: 1.0, duration: 0)
// ...
private func setup() {
View UIColor+Random.swift
extension UIColor {
static func generateRandomColor() -> UIColor {
let hue = CGFloat(arc4random() % 256) / 256 // use 256 to get full range from 0.0 to 1.0
return UIColor(hue: hue, saturation: 1, brightness: 1, alpha: 1) // Max saturation, max brightness
You can’t perform that action at this time.