Questions :-

  1. A positive integer m is a sum of squares if it can be written as k + l where k > 0, l > 0 and both k and l are perfect squares.Write a Python function sumofsquares(m) that takes an integer m returns True if m is a sum of squares and False otherwise. (If m is not positive, your function should return False.)Here are some examples to show how your function should work.
    >>> sumofsquares(41)
    True
    >>> sumofsquares(30)
    False
    >>> sumofsquares(17)
    True
  2. A string with parentheses is well bracketed if all parentheses are matched: every opening bracket has a matching closing bracket and vice versa.Write a Python function wellbracketed(s) that takes a string s containing parentheses and returns True if s is well bracketed and False otherwise.Hint: Keep track of the nesting depth of brackets. Initially the depth is 0. The depth increases with each opening bracket and decreases with each closing bracket. What are the constraints on the value of the nesting depth for the string to be wellbracketed?Here are some examples to show how your function should work.
    >>> wellbracketed("22)")
    False
    >>> wellbracketed("(a+b)(a-b)")
    True
    >>> wellbracketed("(a(b+c)-d)((e+f)")
    False
  3. A list rotation consists of taking the last element and moving it to the front. For instance, if we rotate the list [1,2,3,4,5], we get [5,1,2,3,4]. If we rotate it again, we get [4,5,1,2,3].Write a Python function rotatelist(l,k) that takes a list l and a positive integer k and returns the list l after k rotations. If k is not positive, your function should return l unchanged. Note that your function should not change l itself, and should return the rotated list.Here are some examples to show how your function should work.
    >>> rotatelist([1,2,3,4,5],1)
    [5, 1, 2, 3, 4]
    >>> rotatelist([1,2,3,4,5],3)
    [3, 4, 5, 1, 2]
    >>> rotatelist([1,2,3,4,5],12)
    [4, 5, 1, 2, 3]

Download Python File :- Raw FileDownload (Please rename it to .py file once downloaded, for it to run)

def sumofsquares(n):
    if n<0:
        return False
    for i in range(1, int(n**0.5)  + 1 ):
        for j in range(1, int(n**0.5) + 1 ):
            m = i**2 + j**2
            if m == n:
                return True
            elif m > n:
                break
    return False

def wellbracketed(ss):
    depth=0
    for i in range(len(ss)):
        char = ss[i]
        if char=="(":
            depth+=1
        elif char==")":
            if depth>0:
                depth-=1
            else:
                return False

    return depth==0

def rotatelist(ll, shft):
    l = len(ll)
    shft = shft % l
    ans = ll[l-shft:] + ll[:l-shft]
    return ans

Can be tested with the following code (Test Cases) :-

print(sumofsquares(41))
print(sumofsquares(30))
print(sumofsquares(17))

print(wellbracketed("22)"))
print(wellbracketed("(a+b)(a-b)"))
print(wellbracketed("(a(b+c)-d)((e+f)"))

print(rotatelist([1,2,3,4,5],1))
print(rotatelist([1,2,3,4,5],3))
print(rotatelist([1,2,3,4,5],12))
Categories: NPTEL solution

16 Comments

Prerna · February 19, 2018 at 7:40 AM

Thank you !
Please post the solution for week 3 programming assignment.

    Yash Sodha · February 20, 2018 at 4:31 PM

    Hey! Posted! Keep supporting 🙂

Prerna · February 19, 2018 at 7:40 AM

Thank you !
Please post the solution for week 3 programming assignment.

    Yash Sodha · February 20, 2018 at 4:32 PM

    Hey! Posted! Please check! 🙂

kp · February 19, 2018 at 5:13 PM

pllz post python week 3 assignment answers

    Yash Sodha · February 20, 2018 at 4:30 PM

    Posted! Please check it! Thanks for supporting!

SHAHIM · February 23, 2018 at 4:52 PM

thanks

    Yash Sodha · February 27, 2018 at 4:35 PM

    You are welcome 🙂

SHAHIM · February 23, 2018 at 4:52 PM

thanks you

    Yash Sodha · February 27, 2018 at 4:35 PM

    You are welcome 🙂

sriram · February 24, 2018 at 3:54 PM

it is showing run time error

KRISHANU PATRA · February 27, 2018 at 8:57 PM

IN WEEK 3 PROGRAM.YOUR alternating(l) FUNCTION is showing syntax error on line number 8…please fix it

anand · March 7, 2018 at 4:49 PM

i write this code in nptel ans. it’s shows wrong ans.

    Yash Sodha · March 10, 2018 at 5:53 PM

    Please use RAW Code

      anand · March 11, 2018 at 6:10 AM

      I use the raw code than also its shows wrong answer.

Leave a Reply

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