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 reverseKGroup(_ head: ListNode?, _ k: Int) -> ListNode? { | |
| var count = 0 | |
| var current = head | |
| var previous: ListNode? | |
| var newHead: ListNode? | |
| while current != nil { | |
| count += 1 | |
| current = current?.next | |
| } |
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 knapSack(capacity: Int, items: [(weight: Int, value: Int)]) -> Int { | |
| var grid = Array(repeatElement(Array(repeatElement(0, count: capacity)), count: items.count)) | |
| for row in 0 ..< grid.count { | |
| for column in 0 ..< capacity { | |
| let item = items[row] | |
| let maxWeight = column + 1 | |
| let previousMax = row >= 1 ? grid[row - 1][column] : 0 | |
| if item.weight <= maxWeight { |
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
| extension UIView { | |
| func reimplementedHitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? { | |
| guard isUserInteractionEnabled && !isHidden && alpha >= 0.01 else { | |
| return nil | |
| } | |
| if point(inside: point, with: event) { | |
| for subview in subviews { | |
| let convertedPoint = subview.convert(point, from: self) | |
| if let view = subview.reimplementedHitTest(convertedPoint, with: event) { |
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
| struct Scale { | |
| let notes = ["C", "D", "E", "F", "G", "A", "B"] | |
| } | |
| struct ScaleIterator: IteratorProtocol { | |
| private let scale: Scale | |
| private var index = 0 | |
| init(_ scale: Scale) { | |
| self.scale = scale |
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
| struct EnumDictionary<Key: Hashable & CaseIterable, Value> { | |
| private let dictionary: [Key: Value] | |
| init(_ dictionary: [Key: Value]) { | |
| for key in Key.allCases { | |
| assert(dictionary[key] != nil, "All enum cases must be assigned a value.") | |
| } | |
| self.dictionary = dictionary | |
| } |
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
| enum NoteLetter: CaseIterable { | |
| case c | |
| case d | |
| case e | |
| case f | |
| case g | |
| case a | |
| case b | |
| } |
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
| protocol CircularEnum: CaseIterable where Self.AllCases.Element: Equatable { | |
| var currentIndex: Self.AllCases.Index { get } | |
| func next() -> Self.AllCases.Element | |
| func previous() -> Self.AllCases.Element | |
| } | |
| extension CircularEnum { | |
| var currentIndex: Self.AllCases.Index { | |
| return Self.allCases.firstIndex(of: self)! | |
| } |
OlderNewer