Computer Science, asked by balavelan, 8 months ago

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.

Answers

Answered by knomkarnag24
20

Answer:def contracting(l):

   x=0

   old_diff=abs(l[x]-l[x+1])

   for i in range(1,len(l)-1):

       diff=abs(l[i]-l[i+1])

       if diff<old_diff:

           old_diff=diff

       else:

           return False

   return True

Explanation:

Answered by qwstoke
0

def contracting(l):

   for i in range(1, len(l)):

       if abs(l[i] - l[i-1]) >= abs(l[i-1] - l[i-2]):

           return False

   return True

  • This function iterates through the list, starting with the second element (index 1), and compares the absolute difference between each element and its previous element to the absolute difference between the previous element and the element before it.
  • If at any point the absolute difference between an element and its previous element is greater than or equal to the absolute difference between the previous element and the element before it, the function returns False.
  • If the function makes it through the entire list without returning False, it returns True.

#SPJ3

Similar questions