This file contains 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 Foundation | |
final class Generator { | |
private var store = [[""]] | |
func genBalanced(_ n: Int) -> [String] { | |
if n < store.count { | |
return store[n] | |
} |
This file contains 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 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. |
This file contains 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
// 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 |
This file contains 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
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 { |
This file contains 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 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 |
This file contains 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
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 | |
} | |
} |
This file contains 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
// 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 | |
dvdNode.run(changeColor) | |
} |
This file contains 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
// ... | |
private var changeColor: SKAction { | |
return SKAction.colorize(with: UIColor.generateRandomColor(), colorBlendFactor: 1.0, duration: 0) | |
} | |
// ... | |
private func setup() { | |
addChild(dvdNode) |
This file contains 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 SpriteKit | |
public class DVDScene: SKScene { | |
private let dvdNode: SKNode | |
public init(node: SKNode, size: CGSize) { | |
self.dvdNode = node | |
super.init(size: size) | |
setup() |
This file contains 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 SpriteKit | |
import PlaygroundSupport | |
let viewFrame = CGRect(x: 0, y: 0, width: 400, height: 400) | |
let view = SKView(frame: viewFrame) | |
let dvdNode = SKSpriteNode(imageNamed: "dvd-logo.png") | |
dvdNode.size = CGSize(width: 100, height: 47) | |
let mainScene = SKScene(size: view.frame.size) |