Created
December 6, 2023 15:08
-
-
Save carlynorama/782f5a0f4699e99b2121f0a0874003cb to your computer and use it in GitHub Desktop.
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
public extension ClosedRange<Int> { | |
func crop(by other: ClosedRange<Int>) -> [ClosedRange<Int>] { | |
//print("cropping...") | |
guard self.overlaps(other) else { | |
//return [self] | |
fatalError("No overlap") | |
} | |
print("they overlap") | |
guard self != other else { | |
//return [] | |
fatalError("these arrays are the same.") | |
} | |
print("they are not the same") | |
//HANGS!!! Why??? | |
guard !other.contains(self) else { | |
print("other contains source entirely. Nothing left over.") | |
return [] | |
} | |
print("Does it hand in the playground?") | |
guard !other.fullyContains(self) else { | |
print("other contains source entirely. Nothing left over.") | |
return [] | |
} | |
//print("Other does not fully contain self") | |
guard self.upperBound != other.lowerBound else { | |
return [self.lowerBound...self.upperBound-1] | |
} | |
//print("self.upperBound != other.lowerBound") | |
guard other.upperBound != self.lowerBound else { | |
return [self.lowerBound+1...self.upperBound] | |
} | |
//print("other.upperBound != self.lowerBound") | |
guard self.lowerBound != other.lowerBound else { | |
return [(other.upperBound + 1)...self.upperBound] | |
} | |
//print("self.lowerBound != other.lowerBound") | |
guard self.upperBound != other.upperBound else { | |
return [self.lowerBound...(other.upperBound-1)] | |
} | |
//print("self.upperBound != other.upperBound") | |
guard !self.fullyContains(other) else { | |
return [(self.lowerBound...other.lowerBound-1), (other.upperBound+1...self.upperBound)] | |
} | |
//print("!self.fullyContains(other)") | |
if self.contains(other.lowerBound) { | |
//print("found lower") | |
return [self.lowerBound...other.lowerBound-1] | |
} else if self.contains(other.upperBound) { | |
//print("found upper") | |
return [other.upperBound+1...self.upperBound] | |
} else { | |
fatalError("Range divide:Forgot something...") | |
} | |
} | |
func fullyContains(_ other: ClosedRange<Int>) -> Bool { | |
self.lowerBound < other.lowerBound && self.upperBound > other.upperBound | |
} | |
} | |
let resultC = (2442...2679).crop(by:2321...2446) | |
print(resultC) | |
print(Int.max) | |
//This one Hangs. | |
let resultD = (2442763622...2679835230).crop(by:2321931404...2446354471) | |
//let result = (0...9).crop(by:3...8) | |
print(resultD) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment