Last active
August 29, 2015 14:00
-
-
Save sumerc/11248600 to your computer and use it in GitHub Desktop.
TopCoder Problems
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
# Solution of Level 1 ZigZag Problem. (http://community.topcoder.com/stat?c=problem_statement&pm=1259&rd=4493) | |
def zigzag(a): | |
if len(a) <= 1: return 1 | |
result = 1 | |
prev_diff = 0 | |
for i in range(len(a)-1): | |
diff = a[i+1] - a[i] | |
if diff == 0: | |
continue | |
if (prev_diff >= 0 and diff < 0) or (prev_diff <= 0 and diff > 0): | |
result += 1 | |
prev_diff = diff | |
return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment