Last active
January 12, 2022 12:53
-
-
Save Parables/f1f415dbd1ef708ee97136c404d1062e to your computer and use it in GitHub Desktop.
Zizag triples in Dart
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
// Author: Parables Boltnoel | |
void main() { | |
var numbers = [1, 2, 1, 3, 4]; | |
findZigZagTriples(numbers); | |
} | |
// | |
// This function determines whether three numbers a, b, and c is a zigzag triples | |
bool isZigzag(num a, num b, num c) { | |
return (a > b && b < c) == true || (a < b && b > c) == true; | |
} | |
List<num> findZigZagTriples(List<num> numbers) { | |
num a, b, c; | |
List<num> result = []; | |
for (int i = 0; i < numbers.length - 2; i++) { | |
a = numbers[i]; | |
b = numbers[i + 1]; | |
c = numbers[i + 2]; | |
int intValue = isZigzag(a, b, c) ? 1 : 0; | |
result.add(intValue); | |
} | |
print(result); | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment