Skip to content

Instantly share code, notes, and snippets.

@hendisantika
Created February 3, 2019 01:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hendisantika/4b8fc297f61241a4bfbc339ddab89510 to your computer and use it in GitHub Desktop.
Save hendisantika/4b8fc297f61241a4bfbc339ddab89510 to your computer and use it in GitHub Desktop.
def hill_and_vally(s):
d=[x1-x0 for x0,x1 in zip(s,s[1:]) if x1!=x0]
return 2+sum(d0*d1<0 for d0,d1 in zip(d,d[1:]))
#>>> hill_and_vally([1,0,0,0,1])
#3
#>>> hill_and_vally([0,1,0,1,0])
#5
#>>> hill_and_vally([0,2,2,1,1,0,0])
#3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment