-
-
Notifications
You must be signed in to change notification settings - Fork 49.5k
Description
1.Write a function contracting(l) that takes as input a list of integer l and returns True if the absolute difference between each adjacent pair of elements strictly decreases.
- In a list of integers l, the neighbours of l[i] are l[i-1] and l[i+1]. l[i] is a hill if it is strictly greater than its neighbours and a valley if it is strictly less than its neighbours. Write a function counthv(l) that takes as input a list of integers l and returns a list [hc,vc] where hc is the number of hills in l and vc is the number of valleys in l.
3)square n×n matrix of integers can be written in Python as a list with n elements, where each element is in turn a list of n integers, representing a row of the matrix. For instance, the matrix 1 2 3 4 5 6 7 8 9 would be represented as [[1,2,3], [4,5,6], [7,8,9]]. Write a function leftrotate(m) that takes a list representation m of a square matrix as input, and returns the matrix obtained by rotating the original matrix counterclockwize by 90 degrees. For instance, if we rotate the matrix above, we get 3 6 9 2 5 8 1 4 7 Your function should not modify the argument m provided to the function rotate().
def contracting(l):
for i in range(len(l)-2):
if abs(l[i]-l[i+1])>abs(l[i+1]-l[i+2]):
continue
else:
return False
return True
†*****************************
def counthv(l):
hillcount=0
valleycount=0
for i in range (1,len(l)-1):
if l[i]>l[i-1] and l[i]>l[i+1]:
hillcount+=1
elif l[i]<l[i-1] and l[i]<l[i+1]:
valleycount+=1
return([hillcount,valleycount])
def leftrotate(m):
N = len(m[0])
for x in range(0, int(N / 2)):
for y in range(x, N-x-1):
temp = m[x][y]
m[x][y] = m[y][N-1-x]
m[y][N-1-x] = m[N-1-x][N-1-y]
m[N-1-x][N-1-y] = m[N-1-y][x]
m[N-1-y][x] = temp
return(m)