Created
June 27, 2017 00:11
-
-
Save anonymous/c02d74eb75a51dce8dcbc027a3ba8704 to your computer and use it in GitHub Desktop.
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
// Put this in a Swift playground | |
import Foundation | |
class Appointment { | |
var start: Date | |
var end: Date | |
init(start: Date, end: Date) { | |
// Ensure that start is always before end | |
if start <= end { | |
self.start = start | |
self.end = end | |
} else { | |
self.end = start | |
self.start = end | |
} | |
} | |
func conflicts(with other: Appointment) -> Bool { | |
var appt1: Appointment | |
var appt2: Appointment | |
// Ensure that appt1 starts before or at the same time as appt2 | |
if self.start <= other.start { | |
appt1 = self | |
appt2 = other | |
} else { | |
appt1 = other | |
appt2 = self | |
} | |
if appt1.end > appt2.start { | |
return true | |
} | |
return false | |
} | |
var description: String { | |
return "\(self.start, self.end)" | |
} | |
} | |
let appt1 = Appointment(start: 2, end: 2); | |
appt1.description | |
let appt2 = Appointment(start: 2, end: 4); | |
appt2.description | |
print(appt1.conflicts(with: appt2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment