Question :-

  1. A positive integer n is said to be perfect if the sum of the factors of n, other than n itself, add up to n. For instance 6 is perfect since the factors of 6 are {1,2,3,6} and 1+2+3=6. Likewise, 28 is perfect because the factors of 28 are {1,2,4,7,14,28} and 1+2+4+7+14=28.Write a Python function perfect(n) that takes a positive integer argument and returns True if the integer is perfect, and False otherwise.Here are some examples to show how your function should work.
    >>> perfect(6)
    True
    >>> perfect(12)
    False
    >>> perfect(28)
    True
    

    [quads id=1]


  2. For an expression with parentheses, we can define the nesting depth as the maximum number of parentheses that are open when reading the expression from left to right. For instance, the nesting depth of “(33+77)(44-12)” is 1, while the nesting depth of “(a(b+c)-d)(e+f)” is 2.Write a Python function depth(s) that takes a string containing an expression with parentheses and returns an integer, the nesting depth of s. You can assume that s is well-parenthesized: that is, that is, every “(” has a matching “)” after it and every “)” has a matching “(” before it.Here are some examples to show how your function should work.
    >>> depth("22")
    0
    >>> depth("(a+b)(a-b)")
    1
    >>> depth("(a(b+c)-d)(e+f)")
    2
    

    [quads id=1]

  3. Write a function sumsquares(l) that takes as input a list of integers and retuns the sum of all the perfect squares in l.Here are some examples to show how your function should work.

    Write three Python functions as specified.

    >>> sumsquares([1,4,9])
    14
    >>> sumsquares([10,11,12,15])
    0
    >>> sumsquares([16,20,25,30,625])
    666

[quads id=1]

Solution :-

def perfect(n):
    sum=0                 #initializing sum to 0
    for i in range(1, int(n/2) + 1): # for loop for finding factor
        if n % i ==0:     #if factor found, add it
            sum+=i
    if n==sum:
        return True      #if addition is equal to number return true.
    else:
        return False     #if addition is not equal to number return false.

def depth(S):
    current_max = 0
    max = 0
    n = len(S)

    for i in range(n):
        if S[i] == '(':
            current_max += 1
            if current_max > max:
                max = current_max
        elif S[i] == ')':
            if current_max > 0:
                current_max -= 1
            else:
                return -1

    if current_max != 0:
        return -1

    return max

def sumsquares(lst1):
    maxx = max(lst1)    #find max number from the list lst1
    sum = 0            #initializing sum to 0
    i=1
    while(i*i <= maxx): #Finding all numbers which are perfect squares.
        z = i*i
        if z in lst1:  #If the perfect square is in list, add it to sum
            sum+= z
        i+=1
    return sum         #returning the value we want!

[quads id=1]

Can be tested with the following code :-

print(perfect(10))
print(perfect(6))
print(perfect(28))
print(sumsquares([1,4,9]))
print(sumsquares([10,11,12,15]))
print(sumsquares([16,20,25,30,625]))

[quads id=1]