Question d’entretien chez Meta

NSRange intersections

Réponses aux questions d'entretien

Utilisateur anonyme

11 avr. 2018

class IntersectionRange { let range1 : NSRange let range2 : NSRange init(first: NSRange, second: NSRange) { range1 = first range2 = second } func intersection() -> NSRange? { let min1 = range1.location let max1 = range1.location + range1.length let min2 = range2.location let max2 = range2.location + range2.length if max2 < min1 || max1 < min2 { return nil } else { let minRange = max(min1, min2) let maxRange = min(max1, max2) return NSRange(location: minRange, length: maxRange - minRange) } } } let intersectionRange = IntersectionRange(first: NSRange(location: 5, length: 17), second: NSRange(location: 10, length: 3)) let intersection = intersectionRange.intersection()

Utilisateur anonyme

20 févr. 2019

func rangeIntersections(_ range1: NSRange, _ range2: NSRange) -> NSRange { let maxLocation = max(range1.location, range2.location) let ranges: (maxLocation: NSRange, minLocation: NSRange) = maxLocation == range1.location ? (range1, range2) : (range2, range1) let point = ranges.minLocation.location + ranges.minLocation.length return NSMakeRange(maxLocation, point - maxLocation) }

Utilisateur anonyme

19 mars 2019

func rangeInterSection(r1: NSRange, r2: NSRange) -> Bool { return (r1.location >= r2.location && r1.location = r1.location && r2.location <= (r1.location + r1.length)) }