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))