python

              PYTHON NPTEL ASSIGNMENT-1


  1. def alternating(l):
  2. if(len(l)>1):
  3. if(l[0]>l[1]):
  4. for i in range(len(l)-1):
  5. if(l[i]==l[i-1]):
  6. return False
  7. if(i%2==0):
  8. if(l[i]<l[i+1]): return="" false="" if(l[0]<l[1]):="" for="" i="" in="" range(len(l)-1):="" if(i%2="=0):" if(l[i]="">l[i+1]):
  9. return False
  10. return True


  11. def ascending(lst):
  12. for i in range(0, len(lst) - 1):
  13. if lst[i] > lst[i+1]:
  14. return False
  15. return True
  16. def matmult(X,Y):
  17. col= len(Y[0])
  18. row= len(X)
  19. result = [[0 for i in range(col)] for j in range(row)]

  20. #print(result)
  21. #print(result)
  22. for i in range(len(X)):
  23. for j in range(len(Y[0])):
  24. for k in range(len(Y)):
  25. result[i][j] += X[i][k] * Y[k][j]
  26. return result
  27. </l[i+1]):>
Can be tested with the following code (Test Cases) :-

                       QUESTION NUM 2

  1. >>> ascending([])
  2. True

  3. >>> ascending([3,3,4])
  4. True

  5. >>> ascending([7,18,17,19])
  6. False

  7. >>> alternating([])
  8. True

  9. >>> alternating([1,3,2,3,1,5])
  10. True

  11. >>> alternating([3,2,3,1,5])
  12. True

  13. >>> alternating([3,2,2,1,5])
  14. False

  15. >>> alternating([3,2,1,3,5])
  16. False

  17. >>> matmult([[1,2],[3,4]],[[1,0],[0,1]])
  18. [[1,2],[3,4]]

  19. >>> matmult([[1,2,3],[4,5,6]],[[1,4],[2,5],[3,6]])
  20. [[14, 32], [32, 77]]

No comments:

Post a Comment

Pages