Computer Science, asked by vcingawale5813, 1 year ago

Write a function expanding(l) that takes as input a list of integer l and returns true if the absolute difference between each adjacent pair of elements strictly increases. Here are some examples of how your function should work. >>> expanding([1,3,7,2,9]) true explanation: differences between adjacent elements are 3-1 = 2, 7-3 = 4, 7-2 = 5, 9-2 = 7. >>> expanding([1,3,7,2,-3]) false explanation: differences between adjacent elements are 3-1 = 2, 7-3 = 4, 7-2 = 5, 2-(-3) = 5, so not strictly increasing. >>> expanding([1,3,7,10]) false explanation: differences between adjacent elements are 3-1 = 2, 7-3 = 4, 10-7 = 3, so not (strictly) increasing. Write a function accordian(l) that takes as input a list of integer l and returns true if the absolute difference between each adjacent pair of elements alternates between increasing strictly and decreasing strictly. Here are some examples of how your function should work. >>> accordian([1,5,1]) false explanation: differences between adjacent elements are 5-1 = 4, 5-1 = 4, which are equal. >>> accordian([1,5,2,8,3]) true explanation: differences between adjacent elements are 5-1 = 4, 5-2 = 3, 8-2 = 6, 8-3 = 5, so the differences decrease, increase and then decrease. >>> accordian([-2,1,5,2,8,3]) true explanation: differences between adjacent elements are 1-(-2) = 3, 5-1 = 4, 5-2 = 3, 8-2 = 6, 8-3 = 5, so the differences increase, decrease, increase and then decrease. >>> accordian([1,5,2,8,1]) false explanation: differences between adjacent elements are 1-(-2) = 3, 5-1 = 4, 5-2 = 3, 8-2 = 6, 8-1 = 7, so the differences increase, decrease, increase and then increase again. A square nn 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 rotate(m) that takes a list re m of a square matrix as input, and returns the matrix obtained by rotating the original matrix clockwize by 90 degrees. For instance, if we rotate the matrix above, we get 7 4 1 8 5 2 9 6 3 your function should not modify the argument m provided to the function rotate(). Here are some examples of how your function should work. >>> rotate([[1,2],[3,4]]) [[3, 1], [4, 2]]

Answers

Answered by vcharithacherry
5

Answer:

def expanding(lst):

diff=[]

for i in range(len(lst)):

 if(i<len(lst)-1):

  x=lst[i]-lst[i+1]

  if(x<0):

   x=-x

  diff.append(x)

for i in range(len(diff)):

 if(i<len(diff)-1):

  if(diff[i+1]>diff[i]):

   f=0

  else:

   f=1

   break

if(f==1):

  return False

else:

  return True

Explanation:

Answered by balavelan
3

Answer:

def contracting(l):

   c=0

   h=["","","","","","","","","","","","","","",""]

   c1=abs(l[0]-l[1])

   for j in range(2,len(l)):

       c2=abs(l[j]-l[j-1])

       if c2 < c1:     ##for strictly increasing use > insted of <

           c=0

           h[j]=c      

       else:

             c=c+1

             h[j]=c;          

   if 1 in h:

       print(False)

   else:

       print(True)

contracting([9,2,7,3,1])

contracting([-2,3,7,2,-1])

contracting([10,7,4,1])

Explanation:

output:

True

False

False

for the question

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.

Similar questions