Skip to content

Instantly share code, notes, and snippets.

@thexande
Created August 22, 2018 21:13
Show Gist options
  • Save thexande/56f66016f47a98a1b9c856ea997b13b8 to your computer and use it in GitHub Desktop.
Save thexande/56f66016f47a98a1b9c856ea997b13b8 to your computer and use it in GitHub Desktop.
enum Trend {
case ascending
case decending
}
func trend(for sequence: [Int]) -> Trend? {
var trend: Trend?
for (index, item) in sequence.enumerated() {
guard index != 0 else {
continue
}
if item > sequence[index - 1] && (trend == .ascending || trend == nil) {
print("ascending")
trend = .ascending
} else if item < sequence[index - 1] && (trend == .decending || trend == nil) {
print(index - 1)
print(sequence[index - 1])
print(item, "\n")
trend = .decending
} else {
return nil
}
}
return trend
}
trend(for: [4, 6, 8])
trend(for: [3, 2, 1])
trend(for: [1, 2, 1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment