Question :-

  1. Define a Python function ascending(l) that returns True if each element in its input list is at least as big as the one before it.
    Here are some examples to show how your function should work.

    >>> ascending([])
    True
    >>> ascending([3,3,4])
    True
    >>> ascending([7,18,17,19])
    False
    

  2. A list of integers is said to be a valley if it consists of a sequence of strictly decreasing values followed by a sequence of strictlyincreasing values. The decreasing and increasing sequences must be of length at least 2. The last value of the decreasing sequence is the first value of the increasing sequence.
    Write a Python function valley(l) that takes a list of integers and returns True if l is a valley and False otherwise.
    Here are some examples to show how your function should work.

    >>> valley([3,2,1,2,3])
    True
    >>> valley([3,2,1])
    False
    >>> valley([3,3,2,1,2])
    False
    

  3. A two dimensional matrix can be represented in Python row-wise, as a list of lists: each inner list represents one row of the matrix. For instance, the matrix
    1  2  3
    4  5  6 
    

    would be represented as [[1,2,3],[4,5,6]].

    The transpose of a matrix makes each row into a column. For instance, the transpose of the matrix above is

    1  4  
    2  5
    3  6
    

    Write a Python function transpose(m) that takes as input a two dimensional matrix using this row-wise representation and returns the transpose of the matrix using the same representation.

    Here are some examples to show how your function should work. You may assume that the input to the function is always a non-empty matrix.

    >>> transpose([[1,4,9]])
    [[1], [4], [9]]
    
    >>> transpose([[1,3,5],[2,4,6]])
    [[1,2], [3,4], [5,6]]
    0 
    >>> transpose([[1,1,1],[2,2,2],[3,3,3]])
    [[1,2,3], [1,2,3], [1,2,3]]

Solution :-

Download Python File :- Download

def ascending(lst):
    for i in range(0, len(lst) - 1):
        if lst[i] > lst[i+1]:
            return False
    return True

def valley(lst):
    flag = False
    for i in range(0, len(lst) - 1):
        if lst[i] == lst[i+1]:
            return False
        if not flag:
            if lst[i] > lst[i+1]:
                continue
            if lst[i] < lst[i+1]:
                flag = True
        else:
            if lst[i] > lst[i+1]:
                return False
            elif lst[i] < lst[i+1]:
                continue
    return flag

def transpose(lst):
    return [list(x) for x in zip(*lst)]

 

Can be tested with the following code :-

print(ascending([]))
#True
print(ascending([3,3,4]))
#True
print(ascending([7,18,17,19]))
#False

print("Valley")
print(valley([3,2,1,2,3]))
print(valley([3, 2, 1]))
print(valley([3,3,2,1,2])))
Categories: NPTEL solution

13 Comments

patil · August 17, 2017 at 7:24 PM

ascending([7])
True\n
File “test.py”, line 15\n
if lst[i] lst[i+1]:\n
^\n
SyntaxError: invalid syntax

    Mit Patel · August 17, 2017 at 7:27 PM

    Error fixed. You can check it out again.

sami · August 18, 2017 at 8:47 AM

Thank you !
Shukriya !
Dhanyawaad !
Merci !
Gracias !

Mayuresh · August 20, 2017 at 5:03 AM

Sir please upload week 4 programming assignment

Mayuresh · August 25, 2017 at 6:38 AM

sir week 4 programming assignment
plz tommorow is last date

sami · August 25, 2017 at 3:27 PM

Please upload the Week 4 Programming assigment. Tomorrow is the last date for submitting it.

visha · August 25, 2017 at 6:22 PM

kindly provide the solutions for week 4 programming test asap.Thanks.

Thejaswini · August 26, 2017 at 8:43 AM

Please post the week 4 assignment answers

Thejaswini · August 26, 2017 at 2:44 PM

Post week 4 assignment

A · August 29, 2017 at 3:35 PM

Post Week 5 Solution

    yashrs · September 2, 2017 at 2:55 PM

    We posted Week 5 solution for Python here

Mayuresh · September 2, 2017 at 12:07 PM

sir please upload week 5 assignmnet
today is last day

Nikhil P · September 2, 2017 at 2:03 PM

sir please upload week 5 programming assignment….

Leave a Reply

Your email address will not be published. Required fields are marked *