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. Here are some examples of how your function should work. >>> contracting([9,2,7,3,1]) True >>> contracting([-2,3,7,2,-1]) False >>> contracting([10,7,4,1]) False
Answers
num_lists = [
[9, 2, 7, 3, 1],
[-2, 3, 7, 2, -1],
[10, 7, 4, 1]
]
def contracting(nums):
sums = []
for i, v in enumerate(nums):
if i < len(nums) - 1:
sums.append(abs(v - nums[i + 1]))
for i in range(len(sums) - 1):
if sums[i] <= sums[i + 1]:
return False
return True
for l in num_lists:
print(f"{l}: {contracting(l)}")
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.
>>> contracting([9,2,7,3,1])
True
>>> contracting([-2,3,7,2,-1])
False
>>> contracting([10,7,4,1])
False
PYTHON CODE
def contracting(l):
for z in range(0,len(l)-3):
A=abs(l[z+2]-l[z+1])
B=abs(l[z+1]-l[z])
if A<B:
continue
else:
return (False)
return (True)
def counthv(l):
hill_count,valley_count=0,0
if len(l)>2:
for i in range(1,len(l)-1):
if l[i]>l[i-1] and l[i]>l[i+1]:
hill_count+=1
elif l[i]<l[i-1] and l[i]<l[i+1]:
valley_count+=1
return ([hill_count,valley_count])
def leftrotate(m):
ans=list()
for a in range(len(m)):
ans.append([])
for b in range(len(m)):
ans[a].append(m[b][len(m)-a-1])
return(ans)
#SPJ2