Created
August 22, 2018 21:13
-
-
Save thexande/56f66016f47a98a1b9c856ea997b13b8 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
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