擅长:python、mysql、java
<p>你想得太多了。一个“转折点”要么高于两边的点,要么低于两边的点。</p>
<pre><code>def turningpoints(x):
N=0
for i in range(1, len(x)-1):
if ((x[i-1] < x[i] and x[i+1] < x[i])
or (x[i-1] > x[i] and x[i+1] > x[i])):
N += 1
return N
>>> turningpoints([0,2,3,4,5,2,1,2,3,4,5,6,7,8,7,6,5,4,5,6])
4
</code></pre>