Skip to content

Instantly share code, notes, and snippets.

@sumerc
Last active August 29, 2015 14:00
Show Gist options
  • Save sumerc/11248600 to your computer and use it in GitHub Desktop.
Save sumerc/11248600 to your computer and use it in GitHub Desktop.
TopCoder Problems
# 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